简体   繁体   English

Nginx忽略server_name

[英]Nginx ignores server_name

I want to set up a proxy server for two or more websites but it always show me the first website and ignoring server_name. 我想为两个或多个网站设置代理服务器,但它总是向我显示第一个网站,而忽略server_name。 I reviewed the documentation about miscellaneous names and set up everything according to the tips. 我查看了有关杂项名称的文档,并根据提示进行了设置。

My nginx config looks like the following: 我的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;
    }
}

When I open local.ex2site.com it shows me content from local.ex1site.com . 当我打开local.ex2site.com时,它显示了来自local.ex1site.com的内容。

Any suggestions would be greatly appreciated. 任何建议将不胜感激。

I was able to find the cause of my issue and it was related to network. 我能够找到导致问题的原因,并且与网络有关。 I have used the following commands 我使用了以下命令

# 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM