【问题标题】:Why does one domain shows another domain's html page after using Nginx in Ubuntu?为什么在Ubuntu中使用Nginx后一个域显示另一个域的html页面?
【发布时间】:2021-12-16 07:26:59
【问题描述】:

好的,我的 Ubuntu VPS 有一个不寻常的问题。访问一个域显示另一个域的静态 html 页面。让我们看一下congif。

假设我有一个域:site1.com。该域有 2 个应用程序在子域中完美运行:app1.site1.com 和 app2.site1.com。主页没有任何应用程序,只有一个简单的 html 页面,上面写着:

'Welcome to Site 1. This site is currently under maintenance!'.

这是我想要的完美运行,但是在我从 Namecheap 指向我的 VPS 的一个新域(我们称他为 site2.com)之后,问题就出现了。现在当我访问 site2.com 时,我可以看到:

'Welcome to Site 1. This site is currently under maintenance!'.

这就是真正的巫毒魔法。

site1 nginx 配置文件如下所示:

server {
        root /var/www/html/site1.com;

        index index.html index.htm index.nginx-debian.html;

        server_name site1.com www.site1.com;

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

server {
        root /var/www/app1.site1.com;

        index index.html index.htm index.nginx-debian.html;

        server_name app1.site1.com;

        location / {
                #try_files $uri $uri/ =404;
                proxy_pass http://localhost:4000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $http_x_forwarded_for;
                proxy_cache_bypass $http_upgrade;
        }
}

server {
        root /var/www/app2.site1.com;

        index index.html index.htm index.nginx-debian.html;

        server_name app2.site1.com;

        location / {
                #try_files $uri $uri/ =404;
                proxy_pass http://localhost:5000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $http_x_forwarded_for;
                proxy_cache_bypass $http_upgrade;
        }
}

html文件在/var/www/html/site1.com/index.html

魔法的原因是什么?

我也尝试过为新域创建一个新的 nginx 文件并创建一个新的服务器块并指向它自己的 html 文件,但它只是不显示该 html 页面,而是显示 site1 页面。

如果有人指出我正确的方向,那将非常有帮助。

【问题讨论】:

    标签: ubuntu nginx nginx-reverse-proxy nginx-config ubuntu-20.04


    【解决方案1】:

    这通常发生在 Nginx 无法找到显示另一个页面的域的服务器块时。所以 Nginx 会自动提供第一个可用的配置文件,在你的情况下是 site1 的页面。

    为避免这种情况,您可以使用此配置在nginx.conf 文件中设置默认服务器:

    server {
        listen 80 default_server;
        listen 443 ssl default_server;
        server_name _;
        ssl_certificate <path to cert>
        ssl_certificate_key <path to key>
        return 404;
    }
    

    当 Nginx 没有找到任何与你的域匹配的服务器块时,它会自动提供一个 404 页面。

    请按以下步骤操作:

    1. /var/www/html/site2.com中添加一个html页面。
    2. 为 site2 创建一个配置文件并将其指向服务器块中的 html 页面。
    3. 安装 SSL。
    4. 重启 Nginx。

    您的问题可能发生的另一个原因是 SSL。因此,请务必通过将 SSL 重新安装到 site2 再试一次。

    请参阅thisthis 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-30
      • 2013-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多