【问题标题】:Server block not routing correctly #nginxhelp服务器块未正确路由#nginxhelp
【发布时间】:2020-09-23 07:12:18
【问题描述】:

我已经设置了一个带有多个服务器块的 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;
    #}
}

【问题讨论】:

    标签: nginx nginx-location nginx-config


    【解决方案1】:

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

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

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

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

    让我知道它是否有效。

    【讨论】:

    • 刚刚测试过,它可以工作。那是完美的。解决方案非常简单,但没有人像你那样解释它。非常感谢。
    猜你喜欢
    • 2021-02-13
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-19
    • 2021-04-27
    • 2012-05-01
    相关资源
    最近更新 更多