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实现发布静态资源的方法
Mar 31 Servers
解决Nginx 配置 proxy_pass 后 返回404问题
Mar 31 Servers
Nginx 502 Bad Gateway错误原因及解决方案
Mar 31 Servers
nginx部署多前端项目的几种方法
May 25 Servers
nginx作grpc的反向代理踩坑总结
Jul 07 Servers
Linux下使用C语言代码搭建一个简单的HTTP服务器
Apr 13 Servers
微信告警的zabbix监控系统 监控整个NGINX集群
Apr 18 Servers
WinServer2012搭建DNS服务器的方法步骤
Jun 10 Servers
openEuler 搭建java开发环境的详细过程
Jun 10 Servers
Zabbix对Kafka topic积压数据监控的解决方案
Jul 07 Servers
本地搭建minio文件服务器(使用bat脚本启动)的方法
Jul 15 Servers
彻底卸载VMware虚拟机的超详细步骤记录
Jul 15 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 excel reader读取excel内容存入数据库实现代码
2012/12/06 PHP
php递归使用示例(php递归函数)
2014/02/14 PHP
php 算法之实现相对路径的实例
2017/10/17 PHP
jQuery 行级解析读取XML文件(附源码)
2009/10/12 Javascript
js弹出的对话窗口永远保持居中显示
2012/12/15 Javascript
JS辨别访问浏览器判断是android还是ios系统
2014/08/19 Javascript
JS清除选择内容的方法
2015/01/29 Javascript
JavaScript常用的弹出广告及背投广告实现方法
2015/02/06 Javascript
JavaScript实现按照指定长度为数字前面补零输出的方法
2015/03/19 Javascript
JQuery中attr属性和jQuery.data()学习笔记【必看】
2016/05/18 Javascript
jQuery 获取遍历获取table中每一个tr中的第一个td的方法
2016/10/05 Javascript
基于cookie实现zTree树刷新后展开状态不变
2017/02/28 Javascript
JavaScript实现打印星型金字塔功能实例分析
2017/09/27 Javascript
从vue源码解析Vue.set()和this.$set()
2018/08/30 Javascript
js实现内置计时器
2019/12/16 Javascript
python在多玩图片上下载妹子图的实现代码
2013/08/13 Python
Python中使用PyQt把网页转换成PDF操作代码实例
2015/04/23 Python
黑科技 Python脚本帮你找出微信上删除你好友的人
2016/01/07 Python
使用Python简单的实现树莓派的WEB控制
2016/02/18 Python
Python自定义进程池实例分析【生产者、消费者模型问题】
2016/09/19 Python
python三大神器之fabric使用教程
2019/06/10 Python
基于TensorBoard中graph模块图结构分析
2020/02/15 Python
python time.strptime格式化实例详解
2021/02/03 Python
PyCharm常用配置和常用插件(小结)
2021/02/06 Python
CSS3五个技巧给你的网站带来出色的效果
2009/04/02 HTML / CSS
css 如何让背景图片拉伸填充避免重复显示
2013/07/11 HTML / CSS
C#里面可以避免一个类被其他类继承么?如何?
2013/09/26 面试题
能源工程专业应届生求职信
2014/03/01 职场文书
幼儿园教师岗位职责
2014/03/17 职场文书
服务标兵事迹材料
2014/05/04 职场文书
致地震灾区的慰问信
2015/03/23 职场文书
投标单位介绍信
2015/05/05 职场文书
安全知识竞赛主持词
2015/06/30 职场文书
《刷子李》教学反思
2016/02/20 职场文书
吃通javascript正则表达式
2021/04/21 Javascript
CSS三大特性继承性、层叠性和优先级详解
2022/01/18 HTML / CSS