nginx 反向代理时,任何端口的虚拟路径都匹配到后端服务器的同一端口的应用

nginx 配置如下:

server {
listen 8080;
server_name 192.168.223.136;
location / {
root "/www/html";
index index.html;
#auth_basic "required auth";
#auth_basic_user_file "/usr/local/nginx/users/.htpasswd";
error_page 404 /404.html;
}
location /images/ {
root "/www";
rewrite ^/images/bbs/(.*\.jpeg)$ /images/$1 break;
rewrite ^/images/www/(.*)$ http://192.168.223.136/$1 redirect;
}
location /basic_status {
stub_status;
}
location ^~/proxy_path/ {
root "/www/html";
index index.html;
proxy_pass http://192.168.223.137/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

location ^~/proxy_path/ {
root "/www/html";
index index.html;
proxy_pass http://192.168.223.137/;

将左侧匹配到的/proxy_path/开头的url全部转发到后端服务器192.168.223.137

现在一一测试各个proxy_set_header设置的变量的内容
使用“~”符号,意思是所有匹配到的/proxy_path/开头的url都会转发到后端服务器上的一个应用
路径转发后对原始路径没有做任何修改,因此服务中Rest接口定义路径的时候会保留 /user/、/product/、/cert/这样的前缀。如:
location ^~ /user/ {
    proxy_pass http://xx.xx.xx.xx:xxxx;
}
location ^~ /product/ {
    proxy_pass http://xx.xx.xx.xx:xxxx;
}
location ^~ /cert/ {
    proxy_pass http://xx.xx.xx.xx:xxxx;
}
...
@RestController
@RequestMapping("/user/")   // 保留服务模块定位前缀
public class XXXController {
    // code
}

这和zuul中stripPrefix设置为false的情形是一样的,即在路由转发过程中不替换path前缀


zuul:
  routes:
    api-user:
      path: /user/**
      service-id: CASH-USER-CENTER-SERVICE
      strip-prefix: false

参考地址:
https://www.jianshu.com/p/8e78c0716365

 


posted @ 2019-04-27 19:16  八方鱼  阅读(1481)  评论(0)    收藏  举报