Nginx upstream 注意配置头部

Posted by Sunday on 2019-09-24

环境说明

域名 IP 服务
im.sundayle.com 192.168.1.92 Nginx反向代理
37.im.sundayle.com 192.168.1.37 Nginx Web
47.im.sundayle.com 192.168.1.47 Nginx Web

Nginx反向代理

注意配置
proxy_set_header Host im.sundayle.com; #这里的域名是37、47的,若没配置对应后端域名,返回内容是 如192.168.1.37:80的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
upstream im.upserver {
server 192.168.1.37:80 weight=1 max_fails=2 fail_timeout=10s;
server 192.168.1.47:80 weight=1 max_fails=2 fail_timeout=10s;
}
server {
listen 80;
listen 443 ssl http2;
server_name im.sundayle.com;
index index.html;

location / {
proxy_pass http://im.upserver;
proxy_set_header Host im.sundayle.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}

Nginx Web37

1
2
3
4
5
6
7
8
9
10
server {
listen 80;
server_name im.sundayle.com *.im.sundayle.com;
index index.html;

location / {
default_type text/html;
return 200 'hello web37!!!';
}
}

Nginx Web47

1
2
3
4
5
6
7
8
9
10
server {
listen 80;
server_name im.sundayle.com *.im.sundayle.com;
index index.html;

location / {
default_type text/html;
return 200 'hello web47!!!';
}
}