【问题标题】:HTTPS Nginx too many redirects when 2 domains pointing same server当 2 个域指向同一服务器时,HTTPS Nginx 重定向过多
【发布时间】:2020-06-14 03:26:36
【问题描述】:

这里是我的配置文件

    server {
        root /var/www/[NAME]/latest;
        index index.php index.html index.htm index.nginx-debian.html;
        server_name [SITENAME].com www.[SITENAME].com [ANOTHER-SITENAME].com www.[ANOTHER-SITENAME].com;



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

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }


    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/[SITENAME].com-0001/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/[SITENAME].com-0001/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.[ANOTHER-SITENAME].com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = [ANOTHER-SITENAME].com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


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


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


        listen 80;
        server_name [SITENAME].com www.[SITENAME].com [ANOTHER-SITENAME].com www.[ANOTHER-SITENAME].com;
    return 404; # managed by Certbot

}

当我打开 [SITENAME].com 时,一切正常

但是当我打开 [ANOTHER-SITENAME].com 时 我收到一条错误消息

此页面无法正常工作 [ANOTHER-SITENAME].com 重定向您的次数过多 次。

为什么?如何解决这个问题?

【问题讨论】:

    标签: nginx server virtual-server


    【解决方案1】:
    1. 您的配置永远不会适用于 [ANOTHER-SITENAME].com,因为 http 重定向到 https 和 https 不起作用,因为 SSL 证书是 仅适用于 [SITENAME].com。
    2. 如果有两个使用相同文件的站点,请 为他们单独的块 - 这将更容易管理和 排查问题。
    3. 化繁为简,自助...例如:

    [站点名称].com

        server
        {
                listen 80;
                server_name www.[SITENAME].com [SITENAME].com;
                return 301 https://[SITENAME].com$request_uri;
        }
        server
        {
                listen 443 ssl;
                server_name www.[SITENAME];
                ssl_certificate /etc/letsencrypt/live/www.[SITENAME].com/fullchain.pem;
                ssl_certificate_key /etc/letsencrypt/live/www.[SITENAME].com/privkey.pem;
                return 301 https://[SITENAME].com$request_uri;
        }
        server
        {
                listen 443 ssl;
                server_name [SITENAME].com;
                root /var/www/[NAME]/latest;
                index index.php index.html index.htm index.nginx-debian.html;
                location / {
                        try_files $uri $uri/ =404;
                }
                location ~ \.php$ {
                        include snippets/fastcgi-php.conf;
                        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
                }
                location ~ /\.ht {
                        deny all;
                }
        }
    

    [ANOTHER-SITENAME].com

        server
        {
                listen 80;
                server_name www.[ANOTHER-SITENAME].com [ANOTHER-SITENAME].com;
                return 301 https://[ANOTHER-SITENAME].com$request_uri;
        }
        server
        {
                listen 443 ssl;
                server_name www.[ANOTHER-SITENAME].com;
                ssl_certificate /etc/letsencrypt/live/www.[ANOTHER-SITENAME].com/fullchain.pem;
                ssl_certificate_key /etc/letsencrypt/live/www.[ANOTHER-SITENAME].com/privkey.pem;
                return 301 https://[ANOTHER-SITENAME].com$request_uri;
        }
        server
        {
                listen 443 ssl;
                server_name [ANOTHER-SITENAME].com;
                root /var/www/[NAME]/latest;
                index index.php index.html index.htm index.nginx-debian.html;               
                location / {
                        try_files $uri $uri/ =404;
                }
                location ~ \.php$ {
                        include snippets/fastcgi-php.conf;
                        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
                }
                location ~ /\.ht {
                        deny all;
                }
        }
    

    【讨论】:

      猜你喜欢
      • 2020-02-12
      • 1970-01-01
      • 2011-12-12
      • 2014-03-19
      • 2016-04-20
      • 2011-11-22
      • 1970-01-01
      • 2017-05-25
      • 2018-10-07
      相关资源
      最近更新 更多