服务器块未正确路由#nginxhelp

Server block not routing correctly #nginxhelp

我已经设置了一个具有多个服务器块的 NGINX 服务器。我在同一台服务器上配置了 2 个简单的网站 运行。它们指向各自服务器块文件中指定的两个不同的根路径。

我已经更新了两个域的 DNS 详细信息:

名称服务器:根据注册商更新了详细信息

A 记录:更新了相同的静态 IP 地址

但是,当我在浏览器中使用 domain2.com 进行测试时,它会重定向到 domain1.com。我究竟做错了什么?我已经使用以下命令重新启动了 nginx 服务器:

sudo systemctl restart nginx

这是我的服务器块的样子:

# Default server configuration
#
server {

    root /var/www/domain1.com/html;

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

    server_name domain1.com www.domain1.com;

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

    # 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.2-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;
    #}

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain1.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain1.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}

server {
    if ($host = www.domain1.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = domain1.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    listen [::]:80;

    server_name domain1.com www.domain1.com;
    return 404; # managed by Certbot

}

这是我的第二个服务器块文件:

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

    root /var/www/domain2.com/html;

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

    server_name domain2.com www.domain2.com;

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

    # 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.2-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;
    #}
}

你的服务器块配置文件是完美的。不要改变任何东西。让我解释一下发生了什么。您的服务器块表明您安装了 certbot,可能是为了部署 domain1.com 的 Lets Encrypt SSL 证书,而 domain2.com 的服务器块还没有任何由 certbot 管理的条目。这可能意味着您正在为 domain1.com 使用 ssl 证书,而不是为 domain2.com.

在为 domain1.com 配置 SSL 证书时,您可能选择了将所有流量重定向到 SSL 的选项。发生的事情是,当 dns 服务器向您的 Web 服务器发送 domain2.com 请求时,它需要该域的 SSL 证书,但发现 none。然后它将所有流量重定向到安装了有效 SSL 证书的现有域。

但是,证书上的域名 (domain1.com) 与 domain2.com 的 DNS 服务器传递的信息不匹配。因此,它很可能会抛出有关无效 SSL 证书的错误或警告消息。

解决方案: 为 domain2.com 安装新的 SSL 证书,就像为 domain1.com 所做的那样,一切都应该正常。

如果有效请告诉我。