今天在配置nginx.conf时发现proxy_pass指令有几点需要注意的地方。

以下为nginx的配置文件:

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
29
#分布式图片服务器:可进行负载均衡
upstream img_server_pool {
server linux.humg.top:8888 weight=10;
server linux2.humg.top:8888 weight=10;
}

server {
listen 80;
server_name xuecheng.com;
ssi on;
ssi_silent_errors on;
location / {
alias D:/JavaProject/xcEdu/UI/xc-ui-pc-static-portal/;
index index.html;
}

location /cms/preview/ {
proxy_pass http://cms_server_pool/cms/preview/;
}
}

server {
listen 80;
server_name img.xuecheng.com;

location /group1/ {
proxy_pass http://img_server_pool;
}
}
  • 在nginx中配置proxy_pass代理转发时,如果在proxy_pass后面的url加/,表示绝对根路径;

    • xuecheng.com/cms/preview/test.html的转发地址为http://cms_server_pool/cms/preview/test.html
  • 如果在proxy_pass后面没有/,表示相对路径。location指令后匹配的路径部分也会添加到代理地址中。

    • img.xuecheng.com/group1/xxxx.png的转发地址为http://img_server_pool/group1/xxxx.png(经过负载均衡发送请求给不同的服务器)