1

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.

1 Answers1

1

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;
    }
    ....
}