反向代理

  • proxy_pass 包含 URI 路径(如 /api):

    无论末尾是否有斜杠,均删除 location 匹配部分,拼接剩余路径。

    例:location /webapp + proxy_pass http://xxx/api → /api/剩余路径。

  • proxy_pass 不包含 URI 路径(如 http://xxx):

    末尾无斜杠 → 直接拼接完整请求 URI(http://xxx/完整路径)。

    末尾有斜杠 → 替换 location 匹配部分,拼接剩余路径(http://xxx/剩余路径)。

简单反向代理

把本机 80 端口的请求转发到其他的服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
http {
include mime.types;
default_type application/octet-stream;

server {
listen 80;
server_name xxx.xxx.xxx.xxx;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root xxx.xxx.xxx.xxx:port;
index index.html index.htm;
}

}
}

通过正则匹配路径转发

根据路径中不同字段转,把 80 端口的请求,转发到不同的服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
http {
include mime.types;
default_type application/octet-stream;

server {
listen 80;
server_name xxx.xxx.xxx.xxx;

location ~ /api/ {
proxy_pass xxx.xxx.xxx.xxx:port;
}
location ~ /server/ {
proxy_pass xxx.xxx.xxx.xxx:port;
}

}
}
打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2015-2025 SunZhiqi

此时无声胜有声!

支付宝
微信