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. 두 번째 서버 블록의 목적은 무엇입니까?

    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 관리 코드를 두 번째 서버 블록으로 이동하면 너무 많은 리디렉션 문제가 중지되었습니다. 하지만 내 웹사이트가 다시 다음으로 연결됩니다.HTTPhttps 대신.

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

두 번째 서버 블록의 목적은 무엇입니까?

HTTP를 수신하고 HTTP 요청을 HTTPS로 리디렉션합니다.

너무 자주 리디렉션되는 이유는 무엇입니까?

그러면 안 된다,~하지 않는 한웹 사이트 자체는 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.com에게https://www.example.com(첫 번째 서버 블록에서는 HTTPS를 수신하는 서버 블록) 이렇게 하면 "www" 없이 HTTPS 요청을 리디렉션할 수 있습니다. 처음에는.

답변2

1. 리디렉션이 너무 자주 발생하는 이유는 무엇입니까?

요청이 SSL을 통해 수신되었는지 여부를 애플리케이션에서 인식하지 못하므로 서버 블록에 다음 줄을 추가하면 문제가 해결됩니다.

passenger_set_header X-Forwarded-Proto $scheme;

관련 정보