#NGINX : Config - Access to XMLHttpRequest at 'https://XXX1.fr/api/v1/thematics.json' from origin 'https://XX2.net' has been blocked by CORS policy

Viewed 15

I would like to authorize some adress in NGINX config to allow CORS requests

But I've got this error : Access to XMLHttpRequest at 'https://XXX1.fr/api/v1/thematics.json' from origin 'https://XX2.net' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

This is my config file :

    server {
      listen 443 ssl;
      server_name XXX1.fr;
      ssl on;
    
      ssl_certificate     /etc/ssl/certs/XXX1.fr.crt;
      ssl_certificate_key /etc/ssl/private/XXX1.fr.key;
      client_max_body_size 8M;
      root /home/user/rails_app/app/public;
      location / {

           # begin new block
           add_header 'Access-Control-Allow-Origin' * always;
           add_header 'Access-Control-Allow-Credentials' 'true' always;
           add_header 'Access-Control-Allow-Headers' "Authorization, Origin, X-Requested-With, Content-Type, webwag-api-key, Accept" always;
           add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE, PATCH';
           # end new block

           proxy_pass http://localhost:9000/;
           proxy_set_header X-Real-IP  $remote_addr;
           proxy_set_header X-Forwarded-For $remote_addr;
           proxy_set_header Host $host;
           proxy_read_timeout 1000;
           proxy_set_header  X-Forwarded-Proto $scheme;
    
           # begin new block
           if ($request_method = 'OPTIONS') {
               add_header 'Access-Control-Max-Age' 1728000;
               add_header 'Content-Type' 'text/plain; charset=utf-8';
               add_header 'Content-Length' 0;
               return 204;
           }
           # end new block
      }
    
      location ~ ^/assets/ {
        expires 1y;
        add_header Cache-Control public;
    
        add_header ETag "";
        break;
      }
    
        access_log /var/log/nginx/app_access.log;
        error_log /var/log/nginx/app_errors.log;
    }

Have you got an idea ?

0 Answers
Related