ngix代理-- 摘抄

时间:2023-03-09 03:15:14
ngix代理-- 摘抄

有两个网站,http://192.168.1.3:80和http://192.168.1.3:81,

通过nginx的配置实现只用本机作为唯一的代理服务器,只用一个端口,

实现这样的效果:请求http://localhost:8088/siteA来访问http://192.168.1.3:80,

请求http://localhost:8088/siteA来访问http://192.168.1.3:81;

# http://localhost:8088/siteA  -->  http://192.168.1.3:80 
location /siteA/ { 
proxy_pass http://192.168.1.3:80/ 

# http://localhost:8088/siteB --> http://192.168.1.3:81 
location /siteB/ { 
proxy_pass http://192.168.1.3:81/ 
}

在nginx中配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,

则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走。

下面四种情况分别用http://192.168.1.4/proxy/test.html 进行访问:

location /proxy/ {
     proxy_pass http://127.0.0.1:81/;
}会被代理到http://127.0.0.1:81/test.html 这个url
location /proxy/ {
     proxy_pass http://127.0.0.1:81;
}会被代理到http://127.0.0.1:81/proxy/test.html 这个url
location /proxy/ {
     proxy_pass http://127.0.0.1:81/ftlynx/;
}会被代理到http://127.0.0.1:81/ftlynx/test.html 这个url。
location /proxy/ {
     proxy_pass http://127.0.0.1:81/ftlynx;
}会被代理到http://127.0.0.1:81/ftlynxtest.html 这个url