死磕nginx系列--proxy_pass后端斜杠问题

假设nginx服务器的IP地址:192.168.56.11
当我们访问:http://192.168.56.11/test/test.html

如果proxy_pass 后端包含反斜线,后端服务器得到的URL为:192.168.56.12:8002/test.html
如果proxy_pass 后端不包含反斜线时,后端服务器得到的URL为:
192.168.56.12:8002/test/test.html

实例

proxy_pass 包含反斜线

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
worker_processes  1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream test {
server 192.168.56.12:8002 weight=1 max_fails=2 fail_timeout=10s;
}
server {
listen 80;
location /test {
proxy_pass http://test/;
}

后端的access访问日志

- - [07/Nov/2016:10:33:38 +0000] "GETHTTP/1.0" 404 571 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36" "-"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
```

> URI的`/test/`被切割了。

## proxy_pass 后端不包含反斜杠

```shell
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream test {
server 192.168.56.12:8002 weight=1 max_fails=2 fail_timeout=10s;
}
server {
listen 80;
location /test {
proxy_pass http://test;
}

后端的access访问日志

- - [07/Nov/2016:10:37:49 +0000] "GETHTTP/1.0" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36" "-"
1

URL 是完整得URI。