Nginx 指向错误的服务器名称

Nginx 指向错误的服务器名称

我在 DigitalOcean 上托管的 Node webapp 上尝试了许多事情,这就是 NGINX 的配置文件的样子。

server {
 server_name maindomain.xyz www.maindomain.xyz;

        location / {
                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_cache_bypass $http_upgrade;
        }

        #Another location here
}

server
{
        server_name *.maindomain.xyz; #server name for wildcard subdomains
        location /
        {
                proxy_pass http://localhost:3000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }
}

现在我购买了一个新域名,并且像购买主域名一样,我在 DigitalOcean 上创建了一条 A 记录,以便将 newdomain.xyz 定向到 Web 应用服务器。但奇怪的是,如果我尝试在浏览器上浏览新域名,我得到的不是 502 错误,而是从 maindomain.xyz 链接的 Web 应用主页。

正如你在 NGINX 上看到的,我从未为 newdomain.xyz 指定任何指令,但由于某种原因,它被定向到运行我的节点服务器的 5000 端口,该服务器通过以下代码块提供构建的 React Web 应用程序:

if(process.env.NODE_ENV === 'production')
{
    //Set static folder
    app.use(express.static('client/build'));

    app.get('*', (req,res) =>
    {
        res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html')); //Path of the webapp's root
    });
}

const PORT = process.env.PORT || 5000;

app.listen(PORT, () => console.log(`server ${PORT}`));

请注意,我是新手,我找不到任何解决方案来阻止此行为。谢谢大家。

编辑以进一步说明。现在发生的情况是这样的:如果我访问 maindomain.xyz,我会进入主页(这是所需的行为)

如果我访问 newdomain.xyz,即使我没有为 newdomain.xyz 指定服务器名称,我仍然会进入主页。这就是我想要解决的问题。

答案1

已修复,基本上我确实删除了默认服务器块

# Default server configuration
#
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # pass PHP scripts to FastCGI server
    #
    #location ~ \.php$ {
    #   include snippets/fastcgi-php.conf;
    #
    #   # With php-fpm (or other unix sockets):
    #   fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    #   # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
}

显然,如果没有它,当非指定域尝试连接时,NGIX 就会回退到第一个服务器。

相关内容