1

I have the following configuration in Apache:

RewriteEngine On


#APP
ProxyPass /abc/ http://remote.com/abc/
ProxyPassReverse /abc/   http://remote.com/abc/

#APP2
ProxyPass /efg/ http://remote.com/efg/
ProxyPassReverse /efg/   http://remote.com/efg/ 

I am trying to have the same configuration in nginx. After reading some links, this is what I have :

server {
      listen 8081;
      server_name  localhost;
      proxy_redirect http://localhost:8081/ http://remote.com/;

      location ^~/abc/ {
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
        proxy_pass http://remote.com/abc/;
      }

       location ^~/efg/ {
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
        proxy_pass http://remote.com/efg/;
      }
    }

I already have the following configuration:

server {
        listen       8080;
        server_name  localhost;


        location / {
            root   html;
            index  index.html index.htm;
        }
        location ^~/myAPP {
            alias   path/to/app;
            index main.html;
        }

        location ^~/myAPP/images {
            alias   another/path/to/images
            autoindex on;
        }
    }

The idea here is to overcome a same-origin-policy problem. The main pages are on localhost:8080 but we need ajax calls to http://remote.com/abc. Both domains are under my control.

Using the above configuration, the ajax calls either don't reach the remote server or get cut off because of the cross origin.

The above solution worked in Apache and isn't working in nginx, so I am assuming it's a configuration problem.

I think there is an implicit question here: should I have two server declarations or should I somehow merge them into one?

EDIT: Added some more information

EDIT2: I've moved all the proxy_pass configuration into the main server declaration and changed all the ajax calls to go through port 8080. I am now getting a new error: 502 Connection reset by peer. Wireshark shows packets going out to http://remote.com with a bad IP header checksum.

summerbulb
  • 143
  • 1
  • 2
  • 8

1 Answers1

2

Not working is not especially detailed, but let me try to guess what the problem may be. I would suggest trying the following configuration:

server {
  listen 8081;
  server_name  localhost;

  location /abc/ {
    proxy_pass http://remote.com/abc/;
    proxy_http_version 1.1;
    proxy_redirect http://localhost:8081/ http://remote.com/;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
  }

   location /efg/ {
    proxy_pass http://remote.com/efg/;
    proxy_http_version 1.1;
    proxy_redirect http://localhost:8081/ http://remote.com/;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
  }
}

What I really changed is the location blocks definitions and added a Host header, just in case the remote host uses name-based virtual hosts. All other changes are cosmetic - readability is improved when all corresponding directives are gathered together and ordered properly (this may be in order of importance, or by some other trait).

If the configuration above doesn't work as expected, please add some information what exactly doesn't work and how exactly it fails.

Edit: I added also a Host header to the above configuration, just in case the remote uses name-based virtual hosts.

Vladimir Blaskov
  • 6,183
  • 1
  • 27
  • 22
  • using your configuration, i am getting: "XMLHttpRequest cannot load http://localhost:8081/abc/... Origin http://localhost.com:8080 is not allowed by Access-Control-Allow-Origin." Note: localhost.com is set as an alias to localhost – summerbulb Jun 25 '12 at 11:16
  • Ok, so at least the proxy works fine. I edited my answer above to add the **proxy_http_version** directive, because nginx uses HTTP 1.0 by default - this should fix you origin problems. This directive is available on nginx >= 1.1.4. – Vladimir Blaskov Jun 25 '12 at 11:28
  • Still getting " Access-Control-Allow-Origin". Please refer to my edit above. – summerbulb Jun 25 '12 at 11:44
  • Unfortunately, I completely lost the idea. The configuration I gave you, matches 1:1 the Apache configuration you had, so I cannot imagine why it wouldn't work. I suspect you may have (or had) some additional configuration interfering with the same-origin policies. You can check this question (and its answers) to better understand same-origin policies: http://stackoverflow.com/questions/3595515/xmlhttprequest-error-origin-null-is-not-allowed-by-access-control-allow-origin – Vladimir Blaskov Jun 25 '12 at 13:27
  • Added EDIT2. configuration is now in one `server` declaration and I'm getting 502. – summerbulb Jun 25 '12 at 14:41
  • Can you please add your complete `server` declaration in the original question. – Vladimir Blaskov Jun 25 '12 at 16:21