Nginx 虚拟主机未正确重定向,两个站点都重定向到启用站点的配置中第一个列出的服务器

Nginx Virtual Host not redirecting properly, both sites redirect to first listed server in sites-enabled configuration

我在让 Nginx 识别和重定向两个不同的域名(特别是 www.webapptest1.tkwww.webapptest2.tk 托管在 Freenom 上时遇到问题,将它们分开index.html 个不同文件夹中的文件。

这是我的文件结构和 Nginx sites-enabled 配置:

Webapptest1 HTML

目录:/var/www/webapptest1.tk/html/index.html/

<html>
<head>
    <title>indexTest</title>
</head>
<body>
    <center><h1>Welcome to webapptest1</h1></center>

    <center><p>This webpage domain name is redirecting to the IP address: xx.xxx.xxx.xxx</p></center>

    <center>Webapptest1 successfully redirected!</center>
</body>
</html>

Webapptest2 HTML

目录:/var/www/webapptest2.tk/html/index.html/

<html>
<head>
    <title>indexTest</title>
</head>
<body>
    <center><h1>Welcome to webapptest2</h1></center>

    <center><p>This webpage domain name is redirecting to the IP address: xx.xxx.xxx.xxx</p></center>

    <center>Webapptest2 successfully redirected, and is NOT serving the same file as Webapptest1!</center>
</body>
</html>

启用 Nginx 站点的虚拟主机配置

注意:出于测试目的,我没有从 sites-available

链接文件
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.


server {
    listen 80;

    server_name webapptest1.tk;

    root /var/www/webapptest1.tk/html;
    index index.html index.htm;

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

server {
    listen 80;

    server_name webapptest2.tk;

    root /var/www/webapptest2.tk/html;
    index index.html index.htm;

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

在 Freenom(我正在使用的域提供商)上,我将两个域都设置为 URL 通过 IP 地址转发到 Nginx 服务器,如下所示:

Webapptest1 URL Forwarding(类似于 Webapptest2)

现在,访问 www.webapptest1.tk 或 www.webapptest2.tk,我得到 Webapptest1 的 index.html 页面。

事实是,我已经通过修改我的本地 hosts 文件进行了测试,以包含 Nginx 服务器 IP 以及两个域名。在这种情况下,两个链接都被重定向到各自的 index.html 页面。没有 hosts 文件修改,两个链接仅显示 Webapptest1 的 index.html 页面。

我认为问题出在 Freenom 端及其重定向 URL 的方式,而不是我的 Nginx 服务器?

通过像您一样配置 URL 转发,浏览器中显示的页面就是您转到 http://35.192.230.223/ 时看到的页面(这是您的 nginx 配置中的第一个虚拟主机) . 您只需要配置简单的 A 条记录:

webapptest2.tk       A    35.192.230.223
www.webapptest2.tk   A    35.192.230.223
webapptest1.tk       A    35.192.230.223
www.webapptest1.tk   A    35.192.230.223