example.com からのリダイレクト回数が多すぎます。ERR_TOO_MANY_REDIRECTS

example.com からのリダイレクト回数が多すぎます。ERR_TOO_MANY_REDIRECTS

Ubuntu 16.04 で Let's Encrypt を使用して Nginx を保護しようとしていました。

example.conf ファイル前にSSL証明書の取得

server {
    server_name example.com www.example.com ;
    # Tell Nginx and Passenger where your app's 'public' directory is
    root /var/www/backup/mycode/public;
    # Turn on Passenger
    passenger_enabled on;
    rails_env development;
    passenger_ruby /usr/local/rvm/gems/ruby-2.5.6/wrappers/ruby;

}

http://example.com/正常に動作しています。

SSL証明書を取得しようとすると

sudo certbot --nginx -d example.com -d www.example.com

結果は

Your existing certificate has been successfully renewed, and the new certificate
has been installed.

The new certificate covers the following domains: https://example.com and
https://www.example.com

example.conf ファイルSSL証明書の取得

server {
    server_name example.com www.example.com ;
    # Tell Nginx and Passenger where your app's 'public' directory is
    root /var/www/backup/example.com/public;
    # Turn on Passenger
    passenger_enabled on;
    rails_env development;
    passenger_ruby /usr/local/rvm/gems/ruby-2.5.6/wrappers/ruby;




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


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


    server_name example.com www.example.com ;
    listen 80;
    return 404; # managed by Certbot

}

http://example.com/リダイレクト先 https://example.com/何度も

example.com redirected you too many times.
ERR_TOO_MANY_REDIRECTS
  1. なぜ何度もリダイレクトされるのでしょうか?

  2. 2 番目のサーバー ブロックの目的は何ですか?

    server {
    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
    
    
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
    
    
    server_name example.com www.example.com ;
    listen 80;
    return 404; # managed by Certbot
    
     }
    
  3. すべてのリダイレクトをhttps://www.example.com/?

編集1

certibotの管理コードを2番目のサーバーブロックに移動すると、リダイレクトが多すぎる問題はなくなりました。しかし、私のウェブサイトは再びウェブhttps の代わりに。

server {
            server_name example.com www.example.com ;
            # Tell Nginx and Passenger where your app's 'public' directory is
            root /var/www/backup/example.com/public;
            # Turn on Passenger
            passenger_enabled on;
            rails_env development;
            passenger_ruby /usr/local/rvm/gems/ruby-2.5.6/wrappers/ruby;

        }
        server {

            listen 443 ssl; # managed by Certbot
            ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
            ssl_certificate_key /etc/letsencrypt/live/example.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
            if ($host = www.example.com) {
                return 301 https://$host$request_uri;
            } # managed by Certbot


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


            server_name example.com www.example.com ;
            listen 80;
            return 404; # managed by Certbot

        }

答え1

2 番目のサーバー ブロックの目的は何ですか?

HTTP をリッスンし、HTTP リクエストを HTTPS にリダイレクトします。

なぜ何度もリダイレクトされるのでしょうか?

そうすべきではない、ない限りWeb サイト自体は HTTPS を使用して呼び出されることを好まないため、再度リダイレクトを実行します。Nginx の構成は問題ないようです。

How to make all redirects to https://www.example.com/?

変化

if ($host = example.com) {
    return 301 https://$host$request_uri;
}

if ($host = example.com) {
    return 301 https://www.$host$request_uri;
}

別のリダイレクトを追加することもできますhttps://example.comhttps://www.example.com(最初のサーバー ブロック、HTTPS をリッスンするサーバー ブロック内)。これにより、先頭に「www.」がない HTTPS 要求がリダイレクトされます。

答え2

1. リダイレクトが何度も行われるのはなぜですか?

アプリケーションはリクエストが SSL 経由で届いたかどうかを認識できません。サーバー ブロックに次の行を追加すると、この問題が解決されます。

passenger_set_header X-Forwarded-Proto $scheme;

関連情報