【问题标题】:Multiple domains on one server points to wrong sites一台服务器上的多个域指向错误的站点
【发布时间】:2014-02-21 00:49:51
【问题描述】:

使用 Nginx,我为一台服务器创建了一个多域设置,该服务器由四个单独的站点组成。当我启动 Nginx 时,我收到一条错误消息,并且站点似乎混淆了,因为输入一个 url 会导致另一个站点之一。

显示的错误信息 -

Restarting nginx: nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
nginx.

我已经在 /sites-available 下的各自文件中以类似的方式设置了所有四个域 -

server {
        listen   80;

        root /var/www/example.com/public_html;
        index index.php index.html index.htm;

        server_name example.com www.example.com;

        location / {
                try_files $uri $uri/ /index.html;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;

        }

}

我已经检查过,/sites-enabled 中没有默认文件。猜测 Nginx 主配置中可能存在错误设置,但不确定要查找的内容。

【问题讨论】:

  • 也许您在主配置文件 (nginx.conf) 中定义了 server
  • 不,只有 http 和事件选项。有什么特别值得我寻找的吗?
  • 我问是因为默认的nginx.conf 有一堆示例server 块,也许你忘了删除它们。您的主配置文件中有什么?你能把它发布到 pastebin 或类似的东西吗?

标签: nginx multiple-domains server-configuration


【解决方案1】:

如果有任何临时文件,例如~default,请查看/etc/nginx/sites-enabled/ 目录。删除它,问题就解决了。

信用:@OmarIthawi nginx error "conflicting server name" ignored

【讨论】:

    【解决方案2】:

    您的 nginx.conf 从包含指令中的路径加载其外部服务器文件。

    如果您在 include /etc/nginx/conf.d/*.conf; 中有一个文件,并且它的符号链接到 include /etc/nginx/sites-enabled,它将加载该文件两次,这将导致该错误。

    【讨论】:

    • 谢谢@JClarke。这为我解决了这个问题。我刚刚评论了“包括/etc/nginx/conf.d/*.conf;”我的 nginx.conf 文件中的一行,错误消息消失了!
    【解决方案3】:

    在我的情况下,没有启用站点也没有双重包含 ....

    解决方案是避免对“listen 80”和“server_name”引用进行多个引用(如果您将所有 conf.d 文件视为一个整体)...

    在我的例子中,default.conf 和 kibana.conf 都包含对这些人的引用...我评论了默认的那个,问题解决了!

    我的 2 美分 ....

    【讨论】:

      【解决方案4】:

      我在本地机器上的 Ubuntu/nginx/gunicorn/django 1.9 站点遇到了同样的问题。我的 /etc/nginx/sites-enabled 中有两个 nginx 文件。删除任何一个允许剩余站点工作。将这两个文件放入最终总是会转到两个站点之一。我不确定它是如何选择的。

      所以在查看了几个堆栈溢出问题但没有找到解决方案后,我来到这里:http://nginx.org/en/docs/http/request_processing.html

      最终你可以在一个启用站点的文件中拥有多个服务器,所以我改为:

      server {
          listen 80;
          server_name joelgoldstick.com.local;
          error_log    /var/log/nginx/joelgoldstick.com.error.log debug;
          location / {
              proxy_pass http://127.0.0.1:8002;
          }
          location /static/ {
              autoindex on;
              alias /home/jcg/code/python/venvs/jg18/blog/collect_static/;
          }
      }
      
      server {
          listen 80;
          server_name cc-baseballstats.info.local;
          error_log    /var/log/nginx/baseballstats.info.error.log debug;
          location / {
              proxy_pass http://127.0.0.1:8001;
          }
          location /static/ {
              autoindex on;
              alias /home/jcg/code/python/venvs/baseball/baseball_stats/collect_static/;
          }
      }
      

      我现在可以在本地访问我的两个网站

      【讨论】:

        猜你喜欢
        • 2023-03-29
        • 2013-12-12
        • 1970-01-01
        • 2015-06-21
        • 1970-01-01
        • 2011-11-25
        • 1970-01-01
        • 2020-03-03
        • 1970-01-01
        相关资源
        最近更新 更多