共计 2584 个字符,预计需要花费 7 分钟才能阅读完成。
应用 nginx 做反向代理的时候,能够简略的间接把申请一成不变的转发给下一个服务。设置 proxy_pass 申请只会替换域名,如果要依据不同的 url 后缀来拜访不同的服务,则须要通过如下办法:
办法一:加 ”/”
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;
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/;
}
}
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;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For www.diuxie.com$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/ 示意匹配前缀是 user 的申请,proxy_pass 的结尾有 /,则会把 /user/* 前面的手游门路间接拼接到前面,即移除 user。
办法二: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;
}
}
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;
}
}
proxy_pass 结尾没有 /,rewrite 重写了 url。
正文完