Nginx 位置和代理路径配置详情
-
一、Nginx location 基本配置
- 1.1、Nginx 配置文件
- 1.2 、Python 脚本
-
二、测试
-
2.1、测试 location 末尾存在 / 和 proxy_pass末尾存在 /
- nginx配置如下
- 请求url
- 后端内容
-
2.2、测试 location 末尾存在 / 和 proxy_pass末尾不存在 /
- nginx配置如下
- 请求url
- 后端内容
- 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配置如下
- 请求url
- 后端内容
-
2.1、测试 location 末尾存在 / 和 proxy_pass末尾存在 /
- 三、总结
本文是基于 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
中匹配的路径部分代理走; - 如果没有
/
,则会把匹配的路径部分也给代理走。
上一篇: 如何解决 uniapp 开发中按需注入启用组件的问题 微信小程序 - 问题解决方案
下一篇: nginx中proxy_set_header Host $host的作用以及$proxy_host、$host和$http_host之间的区别
推荐阅读
-
Nginx URL 重写(rewrite)配置和信息详情
-
Nginx 代理参数配置中的 proxy_pass 方法详情
-
Nginx 配置]使用上游和代理通行证实现反向代理和负载平衡
-
Nginx 位置和代理路径配置详情
-
nginx上游和proxy_pass反向代理配置详情
-
nginx配置proxy_pass在url末尾带/和不带/的区别详情
-
nginx反向代理位置和proxy_pass斜线踩坑-3:位置末尾无斜线 proxy_pass无斜线的情况下
-
Nginx 路由转发和反向代理位置配置 "收藏本页"。
-
Java 类加载器的作用 - 简介:类加载器是 Java™ 中一个非常重要的概念。类加载器负责将 Java 类的字节码加载到 Java 虚拟机中。本文首先详细介绍了 Java 类加载器的基本概念,包括代理模型、加载类的具体过程和线程上下文类加载器等。然后介绍了如何开发自己的类加载器,最后介绍了类加载器在 Web 容器和 OSGi™ 中的应用。 类加载器是 Java 语言的一项创新,也是 Java 语言广受欢迎的重要原因之一。它允许将 Java 类动态加载到 Java 虚拟机中并执行。类加载器从 JDK 1.0 开始出现,最初是为了满足 Java Applets 的需求而开发的,Java Applets 需要从远程位置下载 Java 类文件并在浏览器中执行。现在,类加载器已广泛应用于网络容器和 OSGi。一般来说,Java 应用程序的开发人员不需要直接与类加载器交互;Java 虚拟机的默认行为足以应对大多数情况。但是,如果遇到需要与类加载器交互的情况,而您又不太了解类加载器的机制,就很容易花费大量时间调试异常,如 ClassNotFoundException 和 NoClassDefFoundError。本文将详细介绍 Java 的类加载器,帮助读者深入理解 Java 语言中的这一重要概念。下面先介绍一些基本概念。 类加载器的基本概念 顾名思义,类加载器用于将 Java 类加载到 Java 虚拟机中。一般来说,Java 虚拟机以如下方式使用 Java 类:Java 源程序(.java 文件)经 Java 编译器编译后转换为 Java 字节代码(.class 文件)。类加载器负责读取 Java 字节代码并将其转换为 java.lang 实例。每个实例都用来表示一个 Java 类。通过该实例的 newInstance 方法创建该类的对象。实际情况可能更加复杂,例如,Java 字节代码可能是由工具动态生成或通过网络下载的。 基本上,所有类加载器都是 java.lang.ClassLoader 类的实例。下面将详细介绍这个 Java 类。 java.lang.ClassLoader 类简介 java.lang.ClassLoader 类的基本职责是根据给定类的名称为其查找或生成相应的字节码,然后根据这些字节码定义一个 Java 类,即 java.lang.Class 类的实例。除此之外,ClassLoader 还负责加载 Java 应用程序所需的资源,如图像文件和配置文件。不过,本文只讨论它加载类的功能。为了履行加载类的职责,ClassLoader 提供了许多方法,其中比较重要的方法如表 1 所示。下文将详细介绍这些方法。 表 1.与加载类相关的 ClassLoader 方法
-
nginx反向代理和负载均衡配置