【问题标题】:Nginx multiple domainsNginx 多域
【发布时间】:2017-05-23 05:42:23
【问题描述】:

我花了几天时间试图弄清楚如何使用 nginx 在我的 Ubuntu 14.04 服务器上设置多个域。

在我的 /etc/nginx/sites-available/default 文件中:

#Domain + IP one.
server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    root /var/www/alive.gg/html;
    index index.php index.html index.htm;
    #149.202.86.66 IP ONE
    server_name alive.gg www.alive.gg;

    location / {
      try_files $uri $uri/ =404;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
      root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }  
}

#Domain + IP two
server {
    listen 80;
    listen [::]:80;
    root /var/www/blazeplay.com/html;
    index index.php index.html index.htm;
    #213.186.35.171 IP TWO
    server_name blazeplay.com www.blazeplay.com;

    location / {
      try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

我的 /etc/network/interfaces 文件

auto eth0
iface eth0 inet static
    address 149.202.86.66
    netmask 255.255.255.0
    network 149.202.86.0
    broadcast 149.202.86.255
    gateway 149.202.86.254
    post-up /sbin/ifconfig eth0:0 213.186.35.147 netmask 255.255.255.255 br$
    post-down /sbin/ifconfig eth0:0 down
    post-up /sbin/ifconfig eth0:1 213.186.35.167 netmask 255.255.255.255 br$
    post-down /sbin/ifconfig eth0:1 down
    post-up /sbin/ifconfig eth0:2 213.186.35.171 netmask 255.255.255.255 br$
    post-down /sbin/ifconfig eth0:2 down

最后一个是我尝试使用的第二个 IP。 (213.186.35.171)

我已经重定向了我的域 DNS,例如 Blazeplay.com:

A   @    213.186.35.171

当我访问 Blazeplay.com 时会发生什么: 我可以查看正确的页面 /var/www/blazeplay.com/html - 但是它无法加载 CSS IMG 等,它会引发 404 错误。如果我访问我的 wordpress 登录站点 /wp-admin,它会将我重定向到正确的 IP,但是当我按 Enter 时,它会再次将我重定向到 alive.gg 域。又名default_server,所以自从它转向default_server 后,我一定设置错了。这就是我迷失在做什么的地方。因为,如果我将server_name blazeplay.com www.blazeplay.com; 替换为 IP 地址 213.186.35.171,而不是域,所有内容都可以正常加载,没有任何问题。

请随意尝试上述方法,所有设置仍然相同,因此如果您前往 blazeplay.com/wp-admin 域并尝试登录,您应该也会遇到同样的情况。

我不是 Ubuntu 或 nginx 专家,我是否错过了配置多个域的一些小步骤,或者我可以做一些故障排除来定位我的问题吗?非常感谢任何帮助!

如果您需要任何其他信息,请告诉我。 :-)

感谢您的阅读。

【问题讨论】:

    标签: ubuntu nginx dns ubuntu-14.04 multiple-domains


    【解决方案1】:

    根据this article,在 virtual.conf (/etc/nginx/conf.d/virtual.conf) 中为您的 URL 创建服务器块:

    server {
        listen 80;
           root /opt/htdocs/bestflare;
        index index.html index.htm;
           server_name bestflare.com;
           location / {
               try_files $uri $uri/ =404;
           }
        }
    server {
           listen 80;
           root /opt/htdocs/usefulread;
           index index.html index.htm;
           server_name usefulread.com;
           location / {
               try_files $uri $uri/ =404;
           }
        }
    

    根据您的详细信息更改 root、server_name 的值。希望这会有所帮助!

    【讨论】:

    • 不幸的是,/etc/nginx/conf.d/virtual.conf 曾经包含站点可用/启用站点的配置文件的骨架,然后您可以复制并在其中编辑. 所以这不用于任何“实际”块。
    【解决方案2】:

    天哪,我花了这么多时间,我错过的只是听:ip:port;在每个服务器块中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-31
      • 1970-01-01
      • 2018-05-21
      • 2018-10-07
      • 2014-10-13
      • 2011-02-22
      • 2015-02-12
      • 2020-01-06
      相关资源
      最近更新 更多