Nginx location 和 proxy_pass路径配置问题小结


Posted in Servers onSeptember 04, 2021

本文是基于 location 的匹配末尾是否配置 / 和 proxy_pass 末尾是否配置 / ,进行测试,完全还原了整个测试过程。帮助了解具体的情况。

一、Nginx location 基本配置

1.1、Nginx 配置文件

upstream test1{
server 127.0.0.1:8000;
}
upstream test2{
server 127.0.0.1:8000;
}
server{
	server_name  test.com;
	listen 80;
        access_log /usr/local/openresty/nginx/logs/test.com_access.log latest;
        error_log  /usr/local/openresty/nginx/logs/test.com.log error;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_connect_timeout   3s;
        proxy_read_timeout 120s;
        proxy_send_timeout 120s;
        proxy_next_upstream error timeout invalid_header http_404 http_502 http_504 http_500;
	
        location /user/ {
			proxy_set_header Connection "";
        	proxy_http_version 1.1;
			proxy_pass http://test1/;
		}
        location / {
                proxy_set_header Connection "";
                proxy_http_version 1.1;
                proxy_pass http://test2/;
        }
}

1.2 、Python 脚本

python2 可以运行

该脚本用于获取请求内容。 这个作为后端,也就是 proxy_pass 代理的后端。

#!/usr/bin/env python

import SimpleHTTPServer
import SocketServer

PORT = 8000

class GetHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        print(self.headers)
        self.send_response(200, "")
    def do_POST(self):
        print(self.headers)
        content_length = self.headers.getheaders('content-length')
        length = int(content_length[0]) if content_length else 0
        print(self.rfile.read(length))
        self.send_response(200, "")

Handler = GetHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
httpd.serve_forever()

二、测试

2.1、测试 location

末尾存在 / 和 proxy_pass末尾存在 /

nginx配置如下

location /user/ {
			proxy_set_header Connection "";
        	proxy_http_version 1.1;
			proxy_pass http://test1/;
		}

请求url

test.com/user/test.html

后端内容

打印的内容:

Host: test1
Content-Length: 0
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Postman-Token: f2bfe770-4f44-4ee9-91c4-060f59dfb26c
Accept-Encoding: gzip, deflate, br


127.0.0.1 - - [10/Apr/2021 16:54:26] "POST /test.html HTTP/1.1" 200 -

小结论:proxy_pass 地址加了 / 的话, 请求 test.com/user/test.html 实际请求是 http://test1/test.html

2.2、测试 location

末尾存在 / 和 proxy_pass末尾不存在 /

nginx配置如下

location /user/ {
			proxy_set_header Connection "";
        	proxy_http_version 1.1;
			proxy_pass http://test1;
		}

请求url

test.com/user/test.html

后端内容

打印的内容:

Host: test1
Content-Length: 0
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Postman-Token: e33d0a2c-1965-4152-b87c-94fca50f2899
Accept-Encoding: gzip, deflate, br

127.0.0.1 - - [10/Apr/2021 16:57:18] "POST /user/test.html HTTP/1.1" 200 -

小结论: proxy_pass 地址不加了 / 的话, 请求 test.com/user/test.html 实际请求是 http://test1/user/test.html

2.3、测试三 location

不加末尾 / 且 proxy_pass 不加 末尾 /

nginx配置如下

location /user {
			proxy_set_header Connection "";
        	proxy_http_version 1.1;
			proxy_pass http://test1;
		}

请求url

test.com/user/test.html

后端内容

打印的内容:

Host: test1
Content-Length: 0
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Postman-Token: 31cd33c6-4c95-41b5-a095-28cdc7113dcd
Accept-Encoding: gzip, deflate, br

127.0.0.1 - - [10/Apr/2021 16:59:34] "POST /user/test.html HTTP/1.1" 200 -

请求 test.com/user/test.html 实际请求是 http://test1/user/test.html

2.4、location 不加

末尾 / 且 proxy_pass 加 末尾 /

nginx配置如下

location /user {
			proxy_set_header Connection "";
        	proxy_http_version 1.1;
			proxy_pass http://test1/;
		}

请求url

test.com/user/test.html

后端内容

打印的内容:

Host: test1
Content-Length: 0
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Postman-Token: d0f4b83f-6482-41ba-8a01-c059eececc2d
Accept-Encoding: gzip, deflate, br

127.0.0.1 - - [10/Apr/2021 17:00:21] "POST //test.html HTTP/1.1" 200 -

请求 test.com/user/test.html 实际请求是 http://test1//test.html

2.5、location 末尾

/ proxy_pass 末尾其他有路径,且末尾加 /

nginx配置如下

location /user/ {
			proxy_set_header Connection "";
        	proxy_http_version 1.1;
			proxy_pass http://test1/haha/;
		}

请求url

test.com/user/test.html

后端内容

打印的内容:

Host: test1
Content-Length: 0
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Postman-Token: 6447cf0b-5988-4f96-81a4-2b621fe32604
Accept-Encoding: gzip, deflate, br

127.0.0.1 - - [10/Apr/2021 17:03:27] "POST /haha/test.html HTTP/1.1" 200 -

请求 test.com/user/test.html 实际请求是 http://test1/haha/test.html

2.6、 location 末尾

/ proxy_pass 末尾其他有路径,且末尾不加 /

nginx配置如下

location /user/ {
			proxy_set_header Connection "";
        	proxy_http_version 1.1;
			proxy_pass http://test1/haha;
		}

请求url

test.com/user/test.html

后端内容

打印的内容:

Host: test1
Content-Length: 0
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Postman-Token: 32fb2a50-1e7c-4131-9804-1828e21ca841
Accept-Encoding: gzip, deflate, br

127.0.0.1 - - [10/Apr/2021 17:05:03] "POST /hahatest.html HTTP/1.1" 200 -

请求 test.com/user/test.html 实际请求是 http://test1/hahatest.html

三、总结

 

序号 访问URL location配置 proxy_pass配置 后端接收的请求 备注
1 test.com/user/test.html /user/ http://test1/ /test.html  
2 test.com/user/test.html /user/ http://test1 /user/test.html  
3 test.com/user/test.html /user http://test1 /user/test.html  
4 test.com/user/test.html /user http://test1/ //test.html  
5 test.com/user/test.html /user/ http://test1/haha/ /haha/test.html  
6 test.com/user/test.html /user/ http://test1/haha /hahatest.html


注意上表格中的后端是指 python 脚本对应的web服务。

在日常的web网站部署中,经常会用到 nginxproxy_pass 反向代理,有一个配置需要弄清楚:配置 proxy_pass 时,

  • 当在后面的 upstram_name 后面出现了 /,相当于是绝对根路径,则 nginx 不会把 location 中匹配的路径部分代理走;
  • 如果没有 /,则会把匹配的路径部分也给代理走。

到此这篇关于Nginx location 和 proxy_pass路径配置详解的文章就介绍到这了,更多相关Nginx location 和 proxy_pass路径配置内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Servers 相关文章推荐
Linux中Nginx的防盗链和优化的实现代码
Jun 20 Servers
在Docker容器中部署SQL Server
Apr 11 Servers
nginx location 带斜杠【 / 】与不带的区别
Apr 13 Servers
阿里云ECS云服务器快照的概念以及如何使用
Apr 21 Servers
nginx日志格式分析和修改
Apr 28 Servers
Ubuntu Server 安装Tomcat并配置systemctl
Apr 28 Servers
Windows server 2012搭建FTP服务器
Apr 29 Servers
如何开启Apache,Nginx和IIS服务器的GZIP压缩功能
Apr 29 Servers
Nginx安装配置详解
Jun 25 Servers
Docker部署Mysql8的实现步骤
Jul 07 Servers
在windows server 2012 r2中安装mysql的详细步骤
Jul 23 Servers
keepalived + nginx 实现高可用方案
Dec 24 Servers
Nginx使用Lua模块实现WAF的原理解析
Nginx部署vue项目和配置代理的问题解析
centos8安装nginx1.9.1的详细过程
Aug 02 #Servers
Nginx反向代理至go-fastdfs案例讲解
Aug 02 #Servers
Nginx配置之实现多台服务器负载均衡
Aug 02 #Servers
nginx服务器的下载安装与使用详解
Aug 02 #Servers
nginx反向代理配置去除前缀案例教程
Jul 26 #Servers
You might like
php eval函数用法总结
2012/10/31 PHP
php过滤HTML标签、属性等正则表达式汇总
2014/09/22 PHP
php实现读取和写入tab分割的文件
2015/06/01 PHP
PHP引用的调用方法分析
2016/04/25 PHP
javascript学习笔记(十) js对象 继承
2012/06/19 Javascript
js实现幻灯片效果(基于jquery插件)
2013/11/05 Javascript
javascript原型链继承用法实例分析
2015/01/28 Javascript
javascript实现了照片拖拽点击置顶的照片墙代码
2015/04/03 Javascript
CSS+JS实现点击文字弹出定时自动关闭DIV层菜单的方法
2015/05/12 Javascript
玩转NODE.JS(四)-搭建简单的聊天室的代码
2016/11/11 Javascript
jQuery实现限制文本框的输入长度
2017/01/11 Javascript
javascript 作用于作用域链的详解
2017/09/27 Javascript
小程序实现多选框功能
2018/10/30 Javascript
实例讲解vue源码架构
2019/01/24 Javascript
vue和better-scroll实现列表左右联动效果详解
2019/04/29 Javascript
ES6中字符串的使用方法扩展
2019/06/04 Javascript
node使用request请求的方法
2019/12/20 Javascript
[00:37]食人魔魔法师轮盘吉兆顺应全新至宝将拥有额外款式
2019/12/19 DOTA
[01:16:16]DOTA2-DPC中国联赛定级赛 RNG vs Phoenix BO3第二场 1月8日
2021/03/11 DOTA
Python 编码处理-str与Unicode的区别
2016/09/06 Python
python将ansible配置转为json格式实例代码
2017/05/15 Python
python使用openpyxl库修改excel表格数据方法
2018/05/03 Python
Selenium鼠标与键盘事件常用操作方法示例
2018/08/13 Python
Python装饰器简单用法实例小结
2018/12/03 Python
Python语言检测模块langid和langdetect的使用实例
2019/02/19 Python
python简单实现矩阵的乘,加,转置和逆运算示例
2019/07/10 Python
Python 合并拼接字符串的方法
2020/07/28 Python
Selenium alert 弹窗处理的示例代码
2020/08/06 Python
五分钟带你搞懂python 迭代器与生成器
2020/08/30 Python
Django缓存Cache使用详解
2020/11/30 Python
Python实现PS滤镜中的USM锐化效果
2020/12/04 Python
CSS3 icon font完全指南(CSS3 font 会取代icon图标)
2013/01/06 HTML / CSS
HTML5制作3D爱心动画教程 献给女友浪漫的礼物
2014/11/05 HTML / CSS
澳大利亚家具和家居用品购物网站:Zanui
2018/12/29 全球购物
MySql 8.0及对应驱动包匹配的注意点说明
2021/06/23 MySQL
详解Python中下划线的5种含义
2021/07/15 Python