10

I have an Nginx server hosting a web app which works fine when directly accessed. Its config is below

server {
    listen 8000 default_server;

    listen [::]:8000 default_server ipv6only=on;

    root /data/www/ ;
    server_name server1.com;

    location / {
            try_files $uri $uri/ =404;
        }

    location /app/ {

    }    
}

Now i have to serve this app from another Nginx server So i setup the reverse proxy like below

server {
        listen 80 default_server;

        listen [::]:80 default_server ipv6only=on;

        root /data/www/ ;
        server_name server2.com;

        location / {
                try_files $uri $uri/ =404;
            }

        location /app/ {
                proxy_pass http://server1.com:8000/app/;
        }    

When i access the app from server2 i am getting errors like below for example when i am accessing http://server2.com/app/css/app.css (sorry no rep to post links)

[error] 6601#0: *1 open() "/data/www/app/css/app.css" failed (2: No such file or directory)

and no errors in server1 logs. Why is nginx looking for static files in server2 when i have set it to reverse proxy to server1 same setup works fine in apache with

ProxyPass /app/ http:server1:8000/app/

ProxyPassReverse /app/ http:server1:8000/app/

What am i missing ?

sanre6
  • 201
  • 1
  • 2
  • 4

3 Answers3

4

You generally don't need to add the path to nginx!

The path from the location gets added automatically

so this

location /app/ {
      proxy_pass http://server1.com:8000/app/;
}

should really be:

 location /app/ {
      proxy_pass http://server1.com:8000;
}
steven
  • 151
  • 1
3

The problem is your second nginx instance that's trying to serve files locally:

   location / {
           try_files $uri $uri/ =404;
      }

Just remove that whole thing if you want to serve using http://server2.com/app/. If you want to use http://server2.com/ instead, update the config to reflect that:

  listen [::]:80 default_server ipv6only=on;

   server_name server2.com;

   location / {
          proxy_pass http://server1.com:8000/app/;
  }  
John Keates
  • 681
  • 4
  • 9
0

Try putting the more specific /app/ location in your proxy before/above the more generic / location, as nginx may be handling the location rules in a way you didn't intend.

Matt Beckman
  • 1,502
  • 18
  • 33
  • 1
    that did not help but the config in server2 location ~* \.(css|js)$ { expires 365d; } for browser caching was causing the issue thanks for your response – sanre6 Jun 05 '14 at 06:45