1

I have two machines, and they are served by a master machine. The master machine defines the first machine 10.10.0.59

server {
        listen 80;
        client_max_body_size 200M;
        server_name localhost 127.0.0.1;
        server_name_in_redirect off;

location / {
        proxy_pass http://10.10.0.59;
        proxy_redirect default;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location ^~ /dev/ {

        proxy_pass http://10.10.0.56;
        proxy_redirect default;
        proxy_set_header Host $http_host;
        rewrite /dev/(.*) /$1 break;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

The dev machine is 10.19.9.56 and I need to add (most likely) /dev/ to the url path, so that I can browse it by http://public_domain/dev/.....

Each of these machines has several apps running.

With this new rule, I can go on by http://public_domain/dev/app1/ but all the links on that page will point to, for example, http://public_domain/app1/login which is server#1.

What else is wrong? Thanks.

Should I setup a new server clause? If I do, do I need to open a different port because the these two location blocks are inside the same server clause and listening on 80.

Thanks.

2 Answers 2

3

The issue is the trailing / on your proxy_pass directives. That tells proxy_pass to replace the part of the url that matched the location (/dev) with the provided path (/). It doesn't make a difference in your location / because it's replacing / with /. If you just replace your current line with

proxy_pass http://10.10.0.59;

Then it will not process the url before passing it to the backend.

4
  • Thank you. I've just updated my post. Do I need a different server block? I have only one and I've tried to rewrite. Jun 29, 2012 at 17:29
  • Unless you can update your app to use relative uri paths everywhere or live under /dev/ on the backend, you probably will need to use a second server. The rewrite you're doing now is doing the same thing as the trailing / you originally had on the proxy_pass
    – kolbyjack
    Jun 29, 2012 at 18:06
  • You mean second physical server, or second server block? Thanks Jun 29, 2012 at 18:14
  • Second server block. server { server_name dev.domain.com; ... }
    – kolbyjack
    Jun 29, 2012 at 18:20
-1

Update: My answer is wrong. But, I'm leaving it here so that whoever sees this wouldn't make my mistake!

location / block should probably be the last block of any server. In your configuration, it comes first. That's probably the reason for the issue you described. So, please try the following...

server {
        server_name localhost 127.0.0.1;
        server_name_in_redirect off;

        location /dev {
                proxy_pass http://10.10.0.59/;
                    .........
        }

        location / {
                proxy_pass http://10.10.0.56/;
                     ........
2
  • The order of static location blocks does not matter. See wiki.nginx.org/r/location
    – kolbyjack
    Jun 27, 2012 at 12:11
  • @kolbyjack You're right. I just tested it myself on a demo setup. Thanks for sharing the right information. Jun 27, 2012 at 12:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .