Nginx – Access-Control-Allow-Origin with nginx reverse proxy

nginxreverse-proxy

I have a server using nginx as a reverse proxy which is passing requests for foo.example.com to 10.0.0.210 the port 80 forwarding is working perfectly fine but I also need to forward port 9292 for a javascript messaging service.

My server{} entry for the 9292 server is:

server{
    listen 9292;
    server_name foo.example.com;
    location / {
            add_header Access-Control-Allow-Origin http://foo.example.com;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass http://10.0.0.210:9292;
    }
}

But when I go to the application my chrome console gets this error:

Unexpected response code: 400 

And then loads of this error:

XMLHttpRequest cannot load http://foo.example.com:9292/faye. Origin http://foo.example.com is not allowed by Access-Control-Allow-Origin.

It is able to request the initial javascript from the server at http://foo.example.com:9292/faye.js but after that I just get this error.

Everything I'm reading implies that this should be working fine so what am I missing?

Best Answer

After a bit of twaeking I managed to solve the Access-Control-Allow-Origin Issue but the Unexpected Response Code: 400 was because web sockets don't work through reverse proxies yet.

See: https://github.com/faye/faye/issues/24#issuecomment-391508