Nginx location 和 proxy_pass路径配置详解
- 一、Nginx location 基本配置
- 二、测试
- 2.1、测试 location 末尾存在 / 和 proxy_pass末尾存在 /
- 2.2、测试 location 末尾存在 / 和 proxy_pass末尾不存在 /
- 2.3、测试三 location 不加末尾 / 且 proxy_pass 不加 末尾 /
- nginx配置如下
- 请求url
- 2.4、location 不加末尾 / 且 proxy_pass 加 末尾 /
- nginx配置如下
- 请求url
- 2.5、location 末尾有 / proxy_pass 末尾其他有路径,且末尾加 /
- nginx配置如下
- 请求url
- 2.6、 location 末尾有 / proxy_pass 末尾其他有路径,且末尾不加 /
- nginx配置如下
- 三、总结
本文是基于 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网站部署中,经常会用到 nginx的 proxy_pass 反向代理,有一个配置需要弄清楚:配置 proxy_pass 时,
- 当在后面的
upstram_name后面出现了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走; - 如果没有
/,则会把匹配的路径部分也给代理走。
Nginx location 和 proxy_pass路径配置详解的更多相关文章
- Nginx+Tomcat的服务器端环境配置详解
这篇文章主要介绍了Nginx+Tomcat的服务器端环境配置详解,包括Nginx与Tomcat的监控开启方法,需要的朋友可以参考下 Nginx+tomcat是目前主流的Javaweb架构,如何让ngi ...
- Windows下Nginx Virtual Host多站点配置详解
Windows下Nginx Virtual Host多站点配置详解 此教程适用于Windows系统已经配置好Nginx+Php+Mysql环境的同学. 如果您还未搭建WNMP环境,请查看 window ...
- 正向代理/反向代理理解、Nginx概述、安装及配置详解
一.Nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP服务器进行网站的发布处理, ...
- Nginx概述、安装及配置详解
nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP服务器进行网站的发布处理,另外 ...
- nginx反向代理原理及配置详解
nginx概述nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP服务器进行网站的发布处理,另外n ...
- Nginx干货(二)配置详解
此篇就不矫情了.直接上个配置吧.以后若有更新,继续修补 /usr/local/nginx/conf目录下面的nginx.conf文件 以用户nginx的身份来运行 user nginx; 启动进程,通 ...
- CentOS6.5环境使用keepalived实现nginx服务的高可用性及配置详解
keepalived基础概念 Keepalived是一个基于VRRP协议来实现的WEB服务高可用方案,可以利用其来避免单点故障.一个WEB服务至少会有2台服务器运行Keepalived,一台为主 ...
- Nginx Location指令URI匹配规则详解
server { listen 80; server_name ss.test *.ss.test; root "D:/Project/PHP/admin-h5/dist/"; s ...
- Nginx location配置详解
上一篇博客Nginx配置详解已经说过了nginx 的基本配置情况,今天来详细讲述一下nginx的location的配置原则, location是根据Uri来进行不同的定位,location可以把网站的 ...
随机推荐
- PHP的图片转base64,base64图片转换为图片并保存代码
打卡记录 1. 图片转base64代码 /*图片转换为 base64格式编码*/ $img = 'images/avatar.jpg'; $base64_img = base64EncodeImage ...
- AT2304 Cleaning
AT2304 Cleaning 题意 一个树上每个节点有一些石子,每次只能选取两个叶子节点并将路径间的所有点上的石子数量减1,问是否能将所有石子取完. 思路 设 \(f_x\) 表示从 \(x\) 节 ...
- 爬取千千小说 -- xpath
今天以其中一本小说为例,讲一下下载小说的主体部分,了解正常的爬取步骤,用到的是request和xpath. 爬取数据三步走:访问url -->爬取数据 -->保存数据 一.访问千千小说网址 ...
- docker exec 参数详解
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...] Run a command in a running container Options ...
- 云服务器是什么?ECS、BCC、CVM...
什么是云服务器?云服务器有哪些优势?能用来干什么? 很多人不太了解云服务器的定义和用途. 云服务器是一种简单高效.处理能力可弹性伸缩的计算服务,帮助用户快速构建更稳定.安全的应用,提升运维效率,降低 ...
- Mybatis学习笔记-日志
日志工厂 如果一个数据库操作出现异常,在排错时,则需要日志 SLF4J Apache Commons Logging(COMMONS_LOGGING) LOG4J LOG4J2 JDK logging ...
- Java互联网架构师系统进阶课程学习 (3)【享学】
3.原子操作CAS Atom(不可分割) 什么是原子操作?如何实现原子操作? syn基于阻塞的锁的机制,1.被阻塞的线程优先级很高,2.拿到锁的线程一直不释放锁怎么办?3.大量的竞争,消耗cpu,同时 ...
- 从net到java:java快速入门
学习java那是不可能的,到为什么不学习一下呢.仅为总结.希望自己在不久的将来能书写优美的java程序.加油!奥利给 1.注释 注释的重要性不言而喻,我们不管写什么代码注释必不可少,那么java的注释 ...
- 小白学习vue第五天-第二弹(全局局部、父子、注册语法糖,script/template抽离模板)
全局组件: 就是注册的位置在实例对象的外面 并且可以多个实例对象使用 而局部: 就是在实例对象的内部注册 父组件和子组件的关系 子组件就是在另一个组件里面注册的组件 组件注册语法糖: 就不用Vue.e ...
- JS基础-数据类型判断typeof、instanceof、Object.prototype.toString
typeof用在基本数据类型和函数时,返回其对应类型的描述,对于引用类型都返回为object. instanceof无法判断基本数据类型,对于引用类型数据,返回其其对应类型. Object.proto ...