Nginx,错误的网站服务

我有我的网站上的IP 1.2.3.4

在我的域名提供商,我有onesite.com和anothersite.com指向1.2.3.4

使用Nginx,我configuration了两个站点:

server { listen 1.2.3.4:80; server_name www.oneserver.com; rewrite ^(.*) http://onserver.com$1 permanent; } server { listen 1.2.3.4:80; server_name onserver.com; location / { fastcgi_pass 127.0.0.1:8878; [..] 

和:

 server { listen 1.2.3.4:80; server_name myapp.anotherserver.com; location / { fastcgi_pass unix:/tmp/myapp.sock; [..] 

当我访问myapp.anotherserver.com时,我被redirect到oneserver.com

任何帮助?

正如user186340指出的那样,你的configuration片段看起来不错,访问端口80 myapp.anotherserver.com应该在你提供的第三个块中。 如果它不像你所描述的那样工作,那可能是因为它没有被加载。

  1. 确保你在这里显示的所有configuration都是由nginxfind/加载的
  2. 使用nginx -t来validation你的configuration
  3. 当您将HUP信号发送到nginx主进程时,监视您的错误日志,以发现任何popup的错误消息
  4. 如果您使用的是nginx v1.9.2 +,您可能希望使用nginx -T将加载的configuration转储到标准输出

如果以下所有内容都是正确的,那么您可能会将显示的configuration与您实际使用的显示configuration进行比较。

问题是Nginxselect默认的服务器,并将其用于没有明确定义服务器的任何请求。 我的解决方法是定义一个什么都不做的默认服务器。

 # This just prevents Nginx picking a random default server if it doesn't know which # server block to send a request to server { # You can add 443/ssl if you need to listen 80 default_server; server_name _; access_log off; log_not_found off; # "I'm a teapot", effectively "go away" https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error # Code 403 (forbidden), 410 (gone) or 501 (not implemented) is probably a better choice https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error return 418; }