Nginx on ubuntu 12.04 keep refusing OPTIONS requests

corsnginx

I am trying do do cross domain Ajax calls inside an angular service in an ionic application tested in chrome.
I am trying to do a POST on my API and nginx keep refusing my OPTIONS.

XMLHttpRequest cannot load http://wss.dev:8080/api/checkin. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 405.

I tried different configuration but none of seems to work.

My conf is a follow :

server {
   listen                *:8080;

 server_name           wss.dev www.wss.dev;
 client_max_body_size 200m;

 index  index.html index.htm index.php;

 access_log            /var/log/nginx/wss.dev.access.log;
 error_log             /var/log/nginx/wss.dev.error.log;
 add_header 'Access-Control-Allow-Origin' $http_origin;
 add_header 'Access-Control-Allow-Credentials' 'true';
 add_header 'Access-Control-Allow-Methods' 'OPTIONS, GET, POST, PUT, DELETE' ;
 add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Mx-ReqToken,X-Requested-With,XMLHttpRequest';

 location ~ .php$ {

     root  /var/www/public;
     try_files $uri $uri/ /index.php /index.php$is_args$args$is_args$args;
     index  index.html index.htm index.php;
     fastcgi_index index.php;
     fastcgi_param SCRIPT_FILENAME $request_filename;
     fastcgi_param APPLICATION_ENV dev;
     fastcgi_pass 127.0.0.1:9000;
     fastcgi_split_path_info ^(.+\.php)(/.*)$;
     include fastcgi_params;
 }
 location / {

     root  /var/www/public;
     try_files $uri $uri/ index.php /index.php$is_args$args;
 }
 sendfile off;
}

I tried like in this example :
https://michielkalkman.com/snippets/nginx-cors-open-configuration.html

I tried different configurations and I tried to change the my alls are made in angular but nothing works…

Best Answer

Getting no response might be caused by configuration similar to this:

if ($request_method !~ ^(GET|HEAD|POST|PUT|DELETE)$ ) {
    return 444;
}

Which means that nginx will close the connection without a proper response, that's why this is so difficult to debug.

Search your configuration for the 444 return code. If you find something like the above, just add OPTIONS to the list).