简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM