Only receiving an empty response with 200 status code when using CORS on Nginx 1.14

krionz nginx-forum at forum.nginx.org
Wed Dec 26 10:40:29 UTC 2018


I'm trying to use wide-open CORS configuration for Nginx. I found this
configuration: https://enable-cors.org/server_nginx.html. Since i started to
use this configuration Nginx only serves an empty response with 200 status
code. I'm using Nginx with PHP-FPM. I have already checked and there is
nothing wrong with the code itself i can say that because i have put some
HTTP responses on text files. On the text files i have the correct HTTP
responses but they are not sent to the client by Nginx.

Here is the configuration:

    location / {
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*'  always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' 
always;
            #
            # Custom headers and headers various browsers *should* be OK
with but aren't
            #
            add_header 'Access-Control-Allow-Headers'
'x-authorization,Filename,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,content-type,Range'
 always;
            #
            # Tell client that this pre-flight info is valid for 20 days
            #
            add_header 'Access-Control-Max-Age' 1728000  always;
            add_header 'Content-Type' 'text/plain; charset=utf-8'  always;
            add_header 'Content-Length' 0  always;
            return 204;
        }
        
        if ($request_method = 'GET') {
            add_header 'Access-Control-Allow-Origin' '*'  always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' 
always;
            add_header 'Access-Control-Allow-Headers'
'x-authorization,Filename,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,content-type,Range'
always;
            add_header 'Access-Control-Expose-Headers'
'Content-Length,Content-Range' always;
        }

        include /etc/nginx/fastcgi_params;

        fastcgi_param SCRIPT_FILENAME
$realpath_root/app_dev.php$is_args$args;
        fastcgi_param DOCUMENT_ROOT $realpath_root;

        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_pass php-fpm:9000;
    }

I'm testing the configs just for GET requests at the moment.

I have tried to make HTTP requests with google chrome and the Insomnia REST
client and with both clients the same behaviour ocurr. I would like to make
it clear that the HTTP requests are getting in to the code correctly and the
correct HTTP responses are being computed, but the HTTP responses are not
being sent to the client by the Nginx.

Thanks for your attention.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,282474,282474#msg-282474



More information about the nginx mailing list