Implementation of prefix removal in Nginx proxy proxy pass configuration
Catalogue
One solution is to add the root path / after proxy_pass. Another option is to use rewrite
When using Nginx as a proxy, you can simply forward the request to the next service intact.
For example, to access abc.com/appv2/a/b.html, request to forward to localhost:8088/appv2/a/b.html
The simple configuration is as follows:
Upstream one {server localhost:8088 weight=5;} server {listen 80; server_name abc.com; access_log "pipe:rollback / data/log/nginx/access.log interval=1d baknum=7 maxsize=1G" main; location / {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_set_header X-NginX-Proxy true; proxy_pass http://one; }}
That is, you can set proxy_pass. Request will only replace the domain name.
But in many cases, we need to forward to different services according to the prefix of url.
such as
Abc.com/user/profile.html is forwarded to user service localhost:8089/profile.html
Abc.com/order/details.html forwarded to order Service localhost:8090/details.html
That is, the prefix of url is not needed for downstream services, unless downstream services add context-path, but most of the time we don't like it. It would be nice to remove this prefix when Nginx retweets.
One solution is to add the root path / after proxy_pass.
Server {listen 80; server_name abc.com; access_log "pipe:rollback / data/log/nginx/access.log interval=1d baknum=7 maxsize=1G" main; location ^ ~ / user/ {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_set_header X-NginX-Proxy true; proxy_pass http://user/; } location ^ ~ / order/ {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_set_header X-NginX-Proxy true; proxy_pass http://order/;}}
^ ~ / user/ indicates that the prefix is user. If the proxy_pass ends with /, the path after / user/* will be directly joined to the end, that is, the user will be removed.
Another option is to use rewrite
Upstream user {server localhost:8089 weight=5;} upstream order {server localhost:8090 weight=5;} server {listen 80; server_name abc.com; access_log "pipe:rollback / data/log/nginx/access.log interval=1d baknum=7 maxsize=1G" main; location ^ ~ / user/ {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_set_header X-NginX-Proxy true; rewrite ^ / user/ (. *) $/ $1 break; proxy_pass http://user;} location ^ ~ / order/ {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_set_header X-NginX-Proxy true; rewrite ^ / order/ (.*) $/ $1 break Proxy_pass http://order;}}
Notice that there is no / at the end of proxy_pass, and rewrite rewrites url.
About rewrite
Syntax: rewrite regex replacement [flag] Default:-Context: server, location, if
The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.