http を https にリダイレクトしようとすると NGINX 無限ループが発生する

http を https にリダイレクトしようとすると NGINX 無限ループが発生する

ウェブサイト全体を http から https に移行しています。ドメインがいくつかあるため、ウェブサイトの https バージョンにリダイレクトする必要があります。

問題は、元のドメインを http から https にリダイレクトしようとすると、nginx で無限ループが発生することです。

皆さん助けてくれませんか?

これが私の設定です

server {
    listen       80;
    server_name  www.domain.com.br domain.com.br w.domain.com.br  ww.domain.com.br wwww.domain.com.br domain1.com.br www.domain1.com.br domain.com www.domain.com domain.net.br www.domain.net.br;
    return 301 https://www.domain.com.br$request_uri;
}

server {
    listen       443;
    server_name  domain.com.br w.domain.com.br  ww.domain.com.br wwww.domain.com.br domain1.com.br www.domain1.com.br domain.com www.domain.com domain.net.br www.domain.net.br;
    ssl                  on;
    ssl_certificate      /home/ssl/ssl-bundle.crt;
    ssl_certificate_key  /home/ssl/myserver.key;
    return 301 https://www.domain.com.br$request_uri;
}

server {
    listen       443;
    ssl                  on;
    ssl_certificate      /home/ssl/ssl-bundle.crt;
    ssl_certificate_key  /home/ssl/myserver.key;
    #ssl_session_timeout  5m;
    #ssl_protocols  SSLv2 SSLv3 TLSv1;
    server_name  www.domain.com.br;
    root   /usr/share/nginx/html2;


    location / {   
        index  index.php;
        if ($request_filename !~* \.(php|gif|html|jpe?    g|png|ico|js|css|flv|swf|pdf|xml)$ ) { rewrite ^ /index.php; }
    }

    location ~ \.php$ {        
        fastcgi_pass   unix:/var/run/php-fpm/php-fpm.socket;
        fastcgi_index  index.php;
        # fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

答え1

server {
    listen  80 default_server;
    return  301 https://www.domain.com.br$request_uri;
}

server {
    listen  443 ssl http2   default_server;
    ssl on;
    ssl_certificate /home/ssl/ssl-bundle.crt;
    ssl_certificate_key /home/ssl/myserver.key;
    #ssl_session_timeout  5m;
    #ssl_protocols  SSLv2 SSLv3 TLSv1;
    server_name www.domain.com.br;
    root    /usr/share/nginx/html2;

    location / {   
        index  index.php;
        if ($request_filename !~* \.(php|gif|html|jpe?    g|png|ico|js|css|flv|swf|pdf|xml)$ ) { rewrite ^ /index.php; }
    }

    location ~ \.php$ {
        fastcgi_pass    unix:/var/run/php-fpm/php-fpm.socket;
        fastcgi_index   index.php;
        # fastcgi_param SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

関連情報