当两个域指向同一服务器时,HTTPS Nginx重定向过多

时间:2020-03-01 18:50:27

标签: nginx server virtual-server

这是我的conf文件

    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]。重定向了您太多 时间。

为什么?该如何解决?

1 个答案:

答案 0 :(得分:0)

  1. 您的配置永远无法用于[ANOTHER-SITENAME] .com,因为http 已重定向到https并且https不起作用,因为SSL证书是 仅适用于[SITENAME] .com。
  2. 如果有两个使用相同文件的站点,请 为他们分开的块-将更易于管理和 解决问题。
  3. 简单点,助自己一臂之力...例如:

[SITENAME] .com

microsoftTeams.getTabInstances

[ANOTHER-SITENAME] .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;
            }
    }
相关问题