nginx代理配置,搞定proxy_pass配置

nginx代理配置

容易晕的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/ ;
}

常用代理配置

前端页面代理
conf 复制代码
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;
		}
相关推荐
Neolnfra3 分钟前
Xshell SSH 连接故障排查
运维·服务器·网络·ssh·xshell·运程连接
MonkeyKing_sunyuhua12 分钟前
ubuntu22.04安装nginx
运维·windows·nginx
Joren的学习记录38 分钟前
【Linux运维大神系列】Docker详解(二)
linux·运维·docker
Fortune_yangyang1 小时前
Docker 入门指南:从 “容器小白” 到快速上手
运维·docker·容器
HIT_Weston2 小时前
68、【Ubuntu】【Hugo】搭建私人博客:方案分析(二)
linux·运维·ubuntu
cws2004012 小时前
HeidiSQL 使用操作说明书
运维·数据库·windows·mysql·heidisql
prettyxian3 小时前
【linux】进程概念(1)PCB、系统调用与 proc 目录全解析
linux·运维·服务器
乾元3 小时前
用 AI 做联动:当应用层出现问题,网络如何被“自动拉入决策回路”
运维·开发语言·网络·人工智能·ci/cd·自动化
youxiao_903 小时前
Docker 容器(一)
运维·docker·容器
小尧嵌入式3 小时前
Linux进程线程与进程间通信
linux·运维·服务器·c语言·开发语言·数据结构·microsoft