monsur / enable-cors.org

Advocacy for CORS
http://enable-cors.org/
208 stars 98 forks source link

Nginx not passing through response headers? #93

Closed MichaelJCole closed 9 years ago

MichaelJCole commented 9 years ago

Hi, I'm using Dokku to host my app at DigitalOcean. Dokku runs nginx 1.6 to proxy Docker apps simulating a Heroku-like environment. The app's all share similar default configs like below.

My Node.js server uses CORS middleware to tell the browser to allow www.myapp.com to make calls to api.myapp.com:

This works fine on my local computer. When I deploy it, I'm getting a CORS error in the browser:

XMLHttpRequest cannot load https://api.myapp.com/r_u_up. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://www.myapp.com' is therefore not allowed access. The response had HTTP status code 502.

I found your nginx CORS config but I'd prefer a simpler config that just passes the Nodejs response headers through. My app doesn't need nginx to intercept them. How can I configure that?

Thanks!

App nginx.conf's:

upstream www { server 172.17.0.135:5000; }
server {
  listen      [::]:80;
  listen      80;
  server_name www.myapp.com ;
  return 301 https://www.myapp.com$request_uri;
}

server {
  listen      [::]:443 ssl spdy;
  listen      443 ssl spdy;
  server_name www.myapp.com;

  keepalive_timeout   70;
  add_header          Alternate-Protocol  443:npn-spdy/2;
  location    / {
    proxy_pass  http://www;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection upgrade;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Forwarded-Port $server_port;
    proxy_set_header X-Request-Start $msec;
  }
  include /home/dokku/www/nginx.conf.d/*.conf;
}
monsur commented 9 years ago

Hi there. I recommend posting your question on StackOverflow, where you will find a lot more nginx and CORS experts who can help. If you discover any improvements that can be made to the code on the site, feel free to open a new issue or submit a patch. Thank you!

MichaelJCole commented 9 years ago

http://serverfault.com/questions/670219/nginx-proxying-nodejs-dokku-cors-response-headers-not-passing-through