Welcome! Log In Create A New Profile

Advanced

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

December 26, 2018 05:40AM
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.
Subject Author Posted

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

krionz December 26, 2018 05:40AM

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

krionz April 04, 2019 02:10PM

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

Francis Daly April 04, 2019 07:18PM

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

krionz April 14, 2019 11:36AM

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

krionz April 14, 2019 11:37AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 284
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready