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;
		}
相关推荐
小天源几秒前
Cacti在Debian/Ubuntu中安装及其使用
运维·ubuntu·debian·cacti
Trouvaille ~23 分钟前
【Linux】TCP Socket编程实战(一):API详解与单连接Echo Server
linux·运维·服务器·网络·c++·tcp/ip·socket
芷栀夏30 分钟前
深度解析 CANN 异构计算架构:基于 ACL API 的算子调用实战
运维·人工智能·开源·cann
全栈工程师修炼指南39 分钟前
Nginx | stream 四层反向代理:SSL、PREREAD 阶段模块指令浅析与实践
运维·网络·网络协议·nginx·ssl
威迪斯特2 小时前
CentOS图形化操作界面:理论解析与实践指南
linux·运维·centos·组件·图形化·桌面·xserver
一方热衷.2 小时前
在线安装对应版本NVIDIA驱动
linux·运维·服务器
独自归家的兔2 小时前
ubuntu系统安装dbswitch教程 - 备份本地数据到远程服务器
linux·运维·ubuntu
ONE_SIX_MIX2 小时前
ubuntu 24.04 用rdp连接,桌面黑屏问题,解决
linux·运维·ubuntu
龙飞052 小时前
Systemd -systemctl - journalctl 速查表:服务管理 + 日志排障
linux·运维·前端·chrome·systemctl·journalctl
春日见2 小时前
如何创建一个PR
运维·开发语言·windows·git·docker·容器