3

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

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

and no errors in serv er1 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 ?

4
  • I'm pretty sure, you didn't post full config. Provide full config of server2 Commented Jun 5, 2014 at 5:57
  • Nothing else apart from browser caching like location ~* \.(css|js)$ { expires 365d; } Commented Jun 5, 2014 at 6:18
  • Exactly this location matches your /app/css/app.css and prevent proxing to server1 Commented Jun 5, 2014 at 6:20
  • thanks alexey that was the problem plz post it as an answer i ll accept it :) Commented Jun 5, 2014 at 6:43

1 Answer 1

6

You have regexp location that matches your request /app/css/app.css and intercepts request from proxy. That's how regexp locations works. To prevent this use ^~ modifier for your app location:

location ^~ /app/ {
    proxy_pass ...;
}

This will prevent regexp location from matching.

Documentation: http://nginx.org/r/location

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.