When using nginx as a proxy, you can simply forward requests to the next service without stopping them.
For example, to access abc.com/appv2/a/ B .html, you must forward the request 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, Setproxy_pass
You can. The request only replaces the domain name.
But in many cases, we need to forward to different services based on the prefix of the URL.
For example
Abc.com/user/profile.htmlresendUser ServiceLocalhost: 8089/profile.html
Abc.com/order/details.htmlresendOrder ServiceLocalhost: 8090/details.html
That is, the URL prefix is not required for downstream services unless the downstream service adds context-path, but we do not like to add this in many cases. If this prefix is removed during nginx forwarding.
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 matched prefix isuser
The end of proxy_pass is/
/user/*
The following path is directly spliced to the back to remove the user.
Another solution 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; }}
Note that proxy_pass does not end/
,rewrite
The URL is rewritten.
Rewrite
syntax: rewrite regex replacement [flag]Default: —Context: server, location, if
Nginx proxy Pass Configuration remove prefix