nginx多域名共用80端口

服务器一个IP映射了多个域名,他们共用80端口的配置。

其实非常简单,server_name设成不同的就行了。

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
30
31
32
33
34
35
36
#应用一
server {
listen 80;
server_name app1.domain.com;
location / {
proxy_pass http://app1/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
#应用二
server {
listen 80;
server_name app2.domain.com;
location / {
proxy_pass http://app2/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

#负载均衡节点配置
upstream app1 {
server 192.168.1.1;
server 192.168.1.2;
ip_hash;
}
upstream app2 {
server 192.168.1.3;
server 192.168.1.4;
ip_hash;
}