When using Nginx as a reverse proxy, you can simply forward the request to the next service as it is. Set proxy_pass to only replace domain names. If you want to access different services based on different URL suffixes, you need to use the following method:

Add “/” server {

listen 8000; 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; ProxysetheaderX – Real – IPhost; proxy_set_header X-Real-IP host; ProxysetheaderX – Real – IPremote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-NginX-Proxy true;

proxy_pass http://user/;
Copy the code

}

location ^~/order/ { proxy_set_header Host host; ProxysetheaderX – Real – IPhost; proxy_set_header X-Real-IP host; ProxysetheaderX – Real – IPremote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-NginX-Proxy true;

proxy_pass http://order/;
Copy the code

}}

server {

listen 8000; 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; ProxysetheaderX – Real – IPhost; proxy_set_header X-Real-IP host; ProxysetheaderX – Real – IPremote_addr; proxy_set_header X-Forwarded-For proxy_set_header X-NginX-Proxy true;

proxy_pass http://user/;
Copy the code

}

location ^~/order/ { proxy_set_header Host host; ProxysetheaderX – Real – IPhost; proxy_set_header X-Real-IP host; ProxysetheaderX – Real – IPremote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-NginX-Proxy true;

proxy_pass http://order/;
Copy the code

}} ^~/user/ means to match the request whose prefix is user. If the end of proxy_pass has /, the mobile game path after /user/* will be directly joined to the end, that is, to remove user. If relative electron has more intuitive understanding, it can also refer to its format as follows:

www.diuxie.com

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; ProxysetheaderX – Real – IPhost; proxy_set_header X-Real-IP host; ProxysetheaderX – Real – IPremote_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;
Copy the code

}

location ^~/order/ { proxy_set_header Host host; ProxysetheaderX – Real – IPhost; proxy_set_header X-Real-IP host; ProxysetheaderX – Real – IPremote_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;
Copy the code

}}

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; ProxysetheaderX – Real – IPhost; proxy_set_header X-Real-IP host; ProxysetheaderX – Real – IPremote_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;
Copy the code

}

location ^~/order/ { proxy_set_header Host host; ProxysetheaderX – Real – IPhost; proxy_set_header X-Real-IP host; ProxysetheaderX – Real – IPremote_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;
Copy the code

}} proxy_pass ends with no /. Rewrite rewrites the URL.