Raj kumar
Raj kumar

Reputation: 171

Nginx rewrite to proxy_pass server/path

Is it possible to use Nginx proxy_pass to rewrite URL as below:

location /foo {
    proxy_pass http://external-server-IP:8080/some/path/;
}

Upvotes: 0

Views: 1757

Answers (1)

Just in case someone still needs this, the easy way to do this:

    location ~ ^/foo/.* {
        rewrite ^/foo(.*) /$1 break;
        proxy_pass https://external-server:8080/remote-path/;
    }

    rewrite ^/foo$ /foo/ redirect;

What this does is that it sends the request to the external server, masquerading it under your own domain.

rewrite ^/foo(.*) /$1 break; The first rewrite is just to remove the added URL path (the remote server is not expecting that.

rewrite ^/foo$ /foo/ redirect; And the 2nd rewrite is just in case you want to use the index page, so that it actually goes to the remote index page as well.

Upvotes: 1

Related Questions