当前位置:首页 > 站长知识 > 服务器 > 正文内容

Nginx location和proxy_pass配置详解

2024-12-02服务器35

概述

Nginx 配置中 location 和 proxy_pass 指令的不同组合方式及其对请求转发路径的影响。

配置效果

1. location 和 proxy_pass 都带斜杠 /

1
2
3
location /api/ {
    proxy_pass http://127.0.0.1:8080/;
}
  • 访问地址www.hw.com/api/upload

  • 转发地址http://127.0.0.1:8080/upload

转发地址不带 location 匹配目录 /api/

2. location 不带斜杠,proxy_pass 带斜杠 /

1
2
3
location /api {
    proxy_pass http://127.0.0.1:8080/;
}
  • 访问地址www.hw.com/api/upload

  • 转发地址http://127.0.0.1:8080//upload

转发地址会多带 /

3. location 带斜杠,proxy_pass 不带斜杠

1
2
3
location /api/ {
    proxy_pass http://127.0.0.1:8080;
}
  • 访问地址www.hw.com/api/upload

  • 转发地址http://127.0.0.1:8080/api/upload

转发地址会带 location 匹配目录 /api/

4. location 和 proxy_pass 都不带斜杠

1
2
3
location /api {
    proxy_pass http://127.0.0.1:8080;
}
  • 访问地址www.hw.com/api/upload

  • 转发地址http://127.0.0.1:8080/api/upload

转发地址会带 location 匹配目录 /api/

5. location 和 proxy_pass 都带斜杠 /,但 proxy_pass 带地址

1
2
3
location /api/ {
    proxy_pass http://127.0.0.1:8080/server/;
}
  • 访问地址www.hw.com/api/upload

  • 转发地址http://127.0.0.1:8080/server/upload

转发地址不带 location 匹配目录 /api/

6. location 不带斜杠,proxy_pass 带斜杠 /,但 proxy_pass 带地址

1
2
3
location /api {
    proxy_pass http://127.0.0.1:8080/server/;
}
  • 访问地址www.hw.com/api/upload

  • 转发地址http://127.0.0.1:8080/server//upload

转发地址不带 location 匹配目录 /api/ ,会多带 /

7. location 带斜杠,proxy_pass 不带斜杠,但 proxy_pass 带地址

1
2
3
location /api/ {
    proxy_pass http://127.0.0.1:8080/server;
}
  • 访问地址www.hw.com/api/upload

  • 转发地址http://127.0.0.1:8080/serverupload

转发地址不带 location 匹配目录 /api/ 直接进行了替换

8. location 和 proxy_pass 都不带斜杠,但 proxy_pass 带地址

1
2
3
location /api {
    proxy_pass http://127.0.0.1:8080/server;
}
  • 访问地址www.hw.com/api/upload

  • 转发地址http://127.0.0.1:8080/server/upload

转发地址不带 location 匹配目录 /api

总结

  • 当 proxy_pass 代理地址端口后有目录(包括 /),转发后地址为:代理地址 + 访问 URL 目录部分去除 location 匹配目录。

  • 当 proxy_pass 代理地址端口后无任何内容,转发后地址为:代理地址 + 访问 URL 目录部分(包括 location 地址)。

场景示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
upstream backend_name_hw {
  server 10.10.10.10:32323 max_fails=2 fail_timeout=2;
}
 
server {
    listen      80;
    server_name hw.test.com;
 
    client_max_body_size 1024m;
    client_body_timeout 12;
    client_header_timeout 12;
    keepalive_timeout 15;
    send_timeout 10;
 
    location / {
      proxy_pass http://backend_name_hw;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
     
    location /hello {
      proxy_pass http://backend_name_hw/hello;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

1
2
3
4
5
6
location /hw/ {
    proxy_pass http://hw-nginx/index.html;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

1
2
3
4
5
6
location /hwhw/  {
    proxy_pass http://hw-nginx/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

1
2
3
4
5
6
location /hw/hi/ {
    proxy_pass http://hw-nginx/hello/index.html;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

1
2
3
4
5
6
location /hello/index.html {
    proxy_pass http://hw-nginx;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}