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服务器添加Systemd自定义服务过程解析
Mar 31 Servers
Nginx解决403 forbidden的完整步骤
Apr 01 Servers
Nginx URL重写rewrite机制原理及使用实例
Apr 01 Servers
使用nginx配置访问wgcloud的方法
Jun 26 Servers
使用goaccess分析nginx日志的详细方法
Jul 09 Servers
nginx配置虚拟主机的详细步骤
Jul 21 Servers
centos8安装nginx1.9.1的详细过程
Aug 02 Servers
Nginx stream 配置代理(Nginx TCP/UDP 负载均衡)
Nov 17 Servers
Nginx+Windows搭建域名访问环境的操作方法
Mar 17 Servers
Docker 镜像介绍以及commit相关操作
Apr 13 Servers
Mac电脑OS系统下安装Nginx的详细教程
Apr 14 Servers
nginx七层负载均衡配置详解
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
mysql数据库差异比较的PHP代码
2012/02/05 PHP
ThinkPHP中的关联模型注意点
2014/06/16 PHP
PHP上传图片到数据库并显示的实例代码
2019/12/20 PHP
cnblogs csdn 代码运行框实现代码
2009/11/02 Javascript
关于COOKIE个数与大小的问题
2011/01/17 Javascript
js与jquery获取父级元素,子级元素,兄弟元素的实现方法
2014/01/09 Javascript
Javascript基础教程之if条件语句
2015/01/18 Javascript
JavaScript框架是什么?怎样才能叫做框架?
2015/07/01 Javascript
详解javascript实现瀑布流列式布局
2016/01/29 Javascript
jquery弹出遮掩层效果【附实例代码】
2016/04/28 Javascript
微信小程序开发经验总结(推荐)
2017/01/11 Javascript
基于jQuery实现数字滚动效果
2017/01/16 Javascript
JavaScript实现事件的中断传播和行为阻止方法示例
2017/01/20 Javascript
jQuery实现简单复制json对象和json对象集合操作示例
2018/07/09 jQuery
[00:56]PWL开团时刻DAY8——追追追追追!
2020/11/09 DOTA
[05:09]DOTA2-DPC中国联赛2月22日Recap集锦
2021/03/11 DOTA
centos 下面安装python2.7 +pip +mysqld
2014/11/18 Python
详解Python中的条件判断语句
2015/05/14 Python
python读写LMDB文件的方法
2018/07/02 Python
利用python numpy+matplotlib绘制股票k线图的方法
2019/06/26 Python
Django如何使用第三方服务发送电子邮件
2019/08/14 Python
flask框架自定义url转换器操作详解
2020/01/25 Python
python离线安装外部依赖包的实现
2020/02/13 Python
Python线程threading模块用法详解
2020/02/26 Python
python Timer 类使用介绍
2020/12/28 Python
利用CSS3实现圆角的outline效果的教程
2015/06/05 HTML / CSS
AmazeUI在模态框中嵌入表单形成模态输入框
2020/08/20 HTML / CSS
德国家具在线:Fashion For Home
2017/03/11 全球购物
奥地利网上书店:Weltbild
2017/07/14 全球购物
飞利浦美国官网:Philips美国
2020/02/28 全球购物
矫正人员思想汇报
2014/01/08 职场文书
个人简历自我评价怎么写
2015/03/10 职场文书
2016中学教师读书心得体会
2016/01/13 职场文书
python缺失值的解决方法总结
2021/06/09 Python
MySQL批量更新不同表中的数据
2022/05/11 MySQL
CSS中使用grid布局实现一套模板多种布局
2022/07/15 HTML / CSS