3

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...

2

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).

| improve this answer | |
  • I have no configuration for 444. You have my full vhost configurations in the question. In which conf file should I look for it? There is no occurences of it in any of my conf files in /etc/nginx nor in any subdrectories. – Lasticoteur Oct 27 '15 at 19:05
0

Are you trying to post from a local dev box? Chrome has a bug that does not support localhost for CORS requests, change it to something like "mylocalbox.dev" and it should work.

| improve this answer | |
  • Thanks for your answer but I am still getting the same error. XMLHttpRequest cannot load http://wss-pulse.dev:8080/api/checkin. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://wss-mobile.org:3000' is therefore not allowed access. The response had HTTP status code 405. – Lasticoteur Jun 10 '15 at 21:21
  • Try doing it explicitly in the nginx config; change your first add_header line to this: add_header 'Access-Control-Allow-Origin' 'http://wss-mobile.org:3000' – RitterKnight Jun 10 '15 at 21:41
  • Tried that too this morning but I've got the exact same error. Actually it was one of the first things I tried but I tried again just in case. – Lasticoteur Jun 11 '15 at 13:56
  • I tried with firefox and safari to see of it was chrome's fault but I add the same problem with them too. (using localhost, 127.0.0.1 and hostname setted in /etc/hosts). – Lasticoteur Jun 11 '15 at 14:00
  • If you do a curl -I http://wss-pulse.dev:8080/api/checkin" you should be getting the Access-Control-Allow-Origin header. If you are getting that, it's probably a problem with your script. – RitterKnight Jun 11 '15 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.