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 相关文章推荐
Nginx的rewrite模块详解
Mar 31 Servers
Nginx 负载均衡是什么以及该如何配置
Mar 31 Servers
nginx对http请求处理的各个阶段详析
Mar 31 Servers
nginx配置proxy_pass中url末尾带/与不带/的区别详解
Mar 31 Servers
Nginx安装完成没有生成sbin目录的解决方法
Mar 31 Servers
使用goaccess分析nginx日志的详细方法
Jul 09 Servers
NGINX 权限控制文件预览和下载的实现原理
Jan 18 Servers
iSCSI服务器CHAP双向认证配置
Apr 01 Servers
安装Windows Server 2012 R2企业版操作系统并设置好相关参数
Apr 29 Servers
nginx 添加http_stub_status_module模块
May 25 Servers
Nginx文件已经存在全局反向代理问题排查记录
Jul 15 Servers
Tomcat安装使用及部署Web项目的3种方法汇总
Aug 14 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学习教程之第2天
2008/06/15 PHP
php json转换成数组形式代码分享
2014/11/10 PHP
ThinkPHP3.2.3框架Memcache缓存使用方法实例总结
2019/04/15 PHP
javascript offsetX与layerX区别
2010/03/12 Javascript
javascript之典型高阶函数应用介绍二
2013/01/10 Javascript
jQuery实现带滚动导航效果的全屏滚动相册实例
2015/06/19 Javascript
浅析JS中的 map, filter, some, every, forEach, for in, for of 用法总结
2017/03/29 Javascript
xmlplus组件设计系列之网格(DataGrid)(10)
2017/05/05 Javascript
原生JS实现自定义滚动条效果
2020/10/27 Javascript
详谈表单重复提交的三种情况及解决方法
2017/08/16 Javascript
gulp教程_从入门到项目中快速上手使用方法
2017/09/14 Javascript
vue-router权限控制(简单方式)
2018/10/29 Javascript
通过实例了解js函数中参数的传递
2019/06/15 Javascript
从零搭一个自用的前端脚手架的方法步骤
2019/09/23 Javascript
详解一些适用于Node.js的命名约定
2019/12/08 Javascript
vue 组件销毁并重置的实现
2020/01/13 Javascript
vue v-for 点击当前行,获取当前行数据及event当前事件对象的操作
2020/09/10 Javascript
JavaScript实现4位随机验证码的生成
2021/01/28 Javascript
python中的多重继承实例讲解
2014/09/28 Python
python检测某个变量是否有定义的方法
2015/05/20 Python
解密Python中的描述符(descriptor)
2015/06/03 Python
[原创]使用豆瓣提供的国内pypi源
2017/07/02 Python
Python字典实现简单的三级菜单(实例讲解)
2017/07/31 Python
pyspark 读取csv文件创建DataFrame的两种方法
2018/06/07 Python
在IPython中执行Python程序文件的示例
2018/11/01 Python
Python实现图片批量加入水印代码实例
2019/11/30 Python
python模式 工厂模式原理及实例详解
2020/02/11 Python
利用Python制作动态排名图的实现代码
2020/04/09 Python
英国美发和美容产品商城:HQhair
2019/02/08 全球购物
银河香水:Galaxy Perfume
2019/03/25 全球购物
奖学金自我鉴定范文
2013/10/03 职场文书
运动会通讯稿200字
2014/02/16 职场文书
2014年保卫工作总结
2014/12/05 职场文书
社区母亲节活动总结
2015/02/10 职场文书
入党介绍人意见2015
2015/06/01 职场文书
Nginx中使用Lua脚本与图片的缩略图处理的实现
2022/03/18 Servers