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;
		}
相关推荐
FIT2CLOUD飞致云6 分钟前
操作教程丨通过1Panel快速安装Zabbix,搭建企业级监控系统
运维·服务器·开源·zabbix·监控·1panel
Ghost Face...41 分钟前
Docker实战:从安装到多容器编排指南
运维·docker·容器
此生只爱蛋1 小时前
【Linux】正/反向代理
linux·运维·服务器
qq_5470261791 小时前
Linux 基础
linux·运维·arm开发
废春啊1 小时前
前端工程化
运维·服务器·前端
我只会发热1 小时前
Ubuntu 20.04.6 根目录扩容(图文详解)
linux·运维·ubuntu
爱潜水的小L2 小时前
自学嵌入式day34,ipc进程间通信
linux·运维·服务器
zhuzewennamoamtf2 小时前
Linux I2C设备驱动
linux·运维·服务器
邂逅星河浪漫2 小时前
【域名解析+反向代理】配置与实现(步骤)-SwitchHosts-Nginx
linux·nginx·反向代理·域名解析·switchhosts
梅尔文.古2 小时前
RaspberryPi-如何启用看门狗
linux·运维·服务器