Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

We have 3 servers. One is front-end proxy, and then there are the internal VMs.

front-end/site-enabled/default

server {
        listen 80;
        rewrite ^(.*) https://$host$1 permanent;

        location / {
                proxy_pass              http://10.10.0.56;
                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 /beta {
                proxy_pass              http://10.10.0.63;
                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;
        }

}

Now the vm 10.10.0.63 is new. We want to run two systems in parallel.

We have this kind of old rule in the new vm.

10.10.0.63/site-enabled/default

server {
    listen 80; 

    client_max_body_size 100M;
    server_name localhost 127.0.0.1;
    server_name_in_redirect off;

    location /api {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_pass http://localhost:5050;
        proxy_redirect default;
    }   

The thing is, is there a way to visit http://example.com/beta/api without having us to change the rules in 10.10.0.63/site-enabled/default? That is, without having us to write location /beta/api in the rule from the original location /api.

Can we do that with nginx? We will get rid of the old system in several weeks and it seems silly to go through all the troubles to change url (we might have to edit many places in source code!).

Thanks.


I still don't have a good workaround, but I took a peek at this How to quick and easy remove part of an URL in Nginx with HttpRewriteModule?

I stripped out beta. It looks like it's working.

share|improve this question
    
Please add your solution below and mark it as accepted. This will benefit other users. –  Akber Choudhry Mar 4 '13 at 20:23
    
@AkberChoudhry sure I will do that later today. Thanks for taking a lot. –  user1730053 Mar 4 '13 at 21:15
add comment

1 Answer 1

up vote 1 down vote accepted

Basically I told the advie here: How to quick and easy remove part of an URL in Nginx with HttpRewriteModule?

Suppose the old url is http://example.com/api and you want to keep the same configuration in nginx, but the new vm use http://example.com/beta/api then you can reuse the same configuration, except, adding a rewrite.

So in sites-enabled, I have this

server {
    listen 80;

    rewrite ^/beta(.*)$ $1 last; # strip out the beta
    .....
    location /api {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_pass http://localhost:5050;
        proxy_redirect default;
    }
    ....
}  
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.