nginx代理配置,搞定proxy_pass配置
容易晕的proxy_pass 后面 url带不带”/“的问题
(1)配置 proxy_pass 时,当在后面的 url 加上了 /,相当于是绝对路径,则 Nginx 不会把 location 中匹配的路径部分加入代理 uri,即url直接替换为代理的地址。
(2)如果配置 proxy_pass 时,后面没有 /,Nginx 则会把匹配的路径部分加入代理 uri,即匹配的路径部分替换后代理的地址后,再加上请求的地址。
代理配置举例
下面几种情况的代理配置
1.请求地址:http://127.0.0.1/test -> http://xiaofei.site/dir/test
#代理配置
location /test {
proxy_pass http://xiaofei.site/dir ;
}
2.请求地址:http://127.0.0.1/test/ -> http://xiaofei.site/dir
location /test {
proxy_pass http://xiaofei.site/dir/ ;
}
3.请求地址:http://127.0.0.1/test/ -> http://127.0.0.1/dir/test/
location /test/ {
proxy_pass http://xiaofei.site/dir;
}
4.请求地址:http://127.0.0.1/test/ -> http://xiaofei.site/dir/
location /test/ {
proxy_pass http://xiaofei.site/dir/ ;
}
常用代理配置
前端页面代理
location /page {
alias E:/softinstall/projects/page/;
index index.html;
charset utf-8;
add_header Cache-Control no-cache;
add_header Pragma no-cache;
add_header Expires 0;
}
前端请求代理
location /xxx/ {
proxy_pass http://127.0.0.1:9000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Cookie $http_cookie;
proxy_connect_timeout 600s;
proxy_send_timeout 600s;
proxy_read_timeout 600s;
client_max_body_size 1024m;
}
后端请求代理
location /xxx/ {
proxy_pass http://localhost:8000/xxx/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 600s;
proxy_send_timeout 600s;
proxy_read_timeout 600s;
client_max_body_size 1024m;
}
原文地址:https://blog.csdn.net/qq_41666142/article/details/142919028
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!