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 多个location转发任意请求或访问静态资源文件的实现
Mar 31 Servers
Nginx配置https原理及实现过程详解
Mar 31 Servers
Nginx进程管理和重载原理详解
Apr 22 Servers
uwsgi+nginx代理Django无法访问静态资源的解决
May 10 Servers
Kubernetes部署实例并配置Deployment、网络映射、副本集
Apr 01 Servers
从零开始在Centos7上部署SpringBoot项目
Apr 07 Servers
Nginx动静分离配置实现与说明
Apr 07 Servers
Windows server 2012 NTP时间同步的实现
Jun 25 Servers
Nginx报错104:Connection reset by peer问题的解决及分析
Jul 23 Servers
Win10系统搭建ftp文件服务器详细教程
Aug 05 Servers
Nginx如何配置多个服务域名解析共用80端口详解
Sep 23 Servers
Elasticsearch6.2服务器升配后的bug(避坑指南)
Sep 23 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
Syphon 虹吸式咖啡壶冲煮–拨动法
2021/03/03 冲泡冲煮
十天学会php之第四天
2006/10/09 PHP
NOT NULL 和NULL
2007/01/15 PHP
WindowsXP中快速配置Apache+PHP5+Mysql
2008/06/05 PHP
为PHP安装imagick时出现Cannot locate header file MagickWand.h错误的解决方法
2014/11/03 PHP
PHP表单验证内容是否为空的实现代码
2016/11/14 PHP
php脚本守护进程原理与实现方法详解
2017/07/20 PHP
使用composer安装使用thinkphp6.0框架问题【视频教程】
2019/10/01 PHP
JavaScript性能优化 创建文档碎片(document.createDocumentFragment)
2010/07/13 Javascript
基于jquery的固定表头和列头的代码
2012/05/03 Javascript
JSONP 跨域访问代理API-yahooapis实现代码
2012/12/02 Javascript
轻松创建nodejs服务器(6):作出响应
2014/12/18 NodeJs
详解JavaScript中shift()方法的使用
2015/06/09 Javascript
JavaScript中的splice()方法使用详解
2015/06/09 Javascript
跟我学习javascript的var预解析与函数声明提升
2015/11/16 Javascript
javascript bom是什么及bom和dom的区别
2015/11/26 Javascript
jQuery实现可兼容IE6的滚动监听功能
2017/09/20 jQuery
使用vue的v-for生成table并给table加上序号的实例代码
2017/10/27 Javascript
判断滚动条滑到底部触发事件(实例讲解)
2017/11/15 Javascript
vue 自定义提示框(Toast)组件的实现代码
2018/08/17 Javascript
jQuery实现的卷帘门滑入滑出效果【案例】
2019/02/18 jQuery
详解如何修改 node_modules 里的文件
2020/05/22 Javascript
Javascript中的奇葩知识,你知道吗?
2021/01/25 Javascript
Python实现周期性抓取网页内容的方法
2015/11/04 Python
python中将字典形式的数据循环插入Excel
2018/01/16 Python
python放大图片和画方格实现算法
2018/03/30 Python
英国最出名高街品牌:Forever Unique
2018/02/24 全球购物
物流仓储实习自我鉴定
2013/09/25 职场文书
违反校纪校规检讨书
2014/02/15 职场文书
给学校建议书范文
2014/05/13 职场文书
学术会议通知范文
2015/04/15 职场文书
清明扫墓感想
2015/08/11 职场文书
2019最新版火锅店的创业计划书 !
2019/07/12 职场文书
2019年幼儿园家长接送责任书
2019/10/29 职场文书
MySQL系列之十 MySQL事务隔离实现并发控制
2021/07/02 MySQL
Win11绿屏怎么办?Win11绿屏死机的解决方法
2021/11/21 数码科技