Nginx忽略server_name

时间:2018-11-14 21:19:12

标签: nginx jwilder-nginx-proxy

我想为两个或多个网站设置代理服务器,但它总是向我显示第一个网站,而忽略server_name。我查看了有关miscellaneous names的文档,并根据提示进行了设置。

我的nginx配置如下所示:

map $http_x_forwarded_proto $proxy_x_forwarded_proto {
  default $http_x_forwarded_proto;
  ''      $scheme;
}
map $http_x_forwarded_port $proxy_x_forwarded_port {
  default $http_x_forwarded_port;
  ''      $server_port;
}
map $http_upgrade $proxy_connection {
  default upgrade;
  '' close;
}
map $scheme $proxy_x_forwarded_ssl {
  default off;
  https on;
}
server_names_hash_bucket_size 128;
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $proxy_connection;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;
proxy_read_timeout 600;
proxy_connect_timeout 600;
proxy_set_header Proxy "";

server {
    server_name _; # This is just an invalid value which will never trigger on a real hostname.
    listen 80;
    return 503;
}
upstream local.ex1site.com {
    server 172.18.0.4:80;
    server 127.0.0.1 down;
}
server {
    server_name local.ex1site.com;
    listen 80 ;
    location / {
        proxy_pass http://local.ex1site.com;
    }
}
upstream local.ex2site.com {
    server 172.18.0.6:80;
    server 127.0.0.1 down;
}
server {
    server_name local.ex2site.com;
    listen 80 ;
    location / {
        proxy_pass http://local.ex2site.com;
    }
}

当我打开 local.ex2site.com 时,它会显示 local.ex1site.com 中的内容。

任何建议将不胜感激。

1 个答案:

答案 0 :(得分:0)

我能够找到问题的原因,并且与网络有关。我使用了以下命令

# netstat -aNp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:sunrpc          0.0.0.0:*               LISTEN      634/rpcbind         
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN      1029/sshd           
tcp        0      0 docker-vagrant:ssh      gateway:52186           ESTABLISHED 6306/sshd: root@pts 
tcp6       0      0 [::]:ca-audit-da        [::]:*                  LISTEN      3341/docker-proxy   
tcp6       0      0 [::]:https              [::]:*                  LISTEN      3353/docker-proxy   
tcp6       0      0 [::]:cslistener         [::]:*                  LISTEN      3380/docker-proxy   
tcp6       0      0 [::]:mysql              [::]:*                  LISTEN      3411/docker-proxy   
tcp6       0      0 [::]:sunrpc             [::]:*                  LISTEN      634/rpcbind         
tcp6       0      0 [::]:http               [::]:*                  LISTEN      3366/docker-proxy   
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN      1029/sshd           
udp        0      0 docker-vagrant:323      0.0.0.0:*                           649/chronyd         
udp        0      0 0.0.0.0:wpages          0.0.0.0:*                           -                   
udp        0      0 0.0.0.0:omirr           0.0.0.0:*                           634/rpcbind         
udp        0      0 0.0.0.0:bootpc          0.0.0.0:*                           2335/dhclient       
udp        0      0 0.0.0.0:sunrpc          0.0.0.0:*                           634/rpcbind         
udp6       0      0 localhost:323           [::]:*                              649/chronyd         
udp6       0      0 [::]:omirr              [::]:*                              634/rpcbind         
udp6       0      0 [::]:sunrpc             [::]:*                              634/rpcbind         
raw6       0      0 [::]:ipv6-icmp          [::]:*                  7           2311/NetworkManager 

# curl -I 172.18.0.4
HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Thu, 15 Nov 2018 12:31:34 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/5.6.36