Nginx – Apache proxy pass in nginx

apache-2.2mod-rewritenginxreverse-proxy

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.

Best Answer

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.

Related Topic