简体   繁体   English

example.com 将您重定向了太多次。 ERR_TOO_MANY_REDIRECTS

[英]example.com redirected you too many times. ERR_TOO_MANY_REDIRECTS

I was trying to Secure Nginx with Let's Encrypt on Ubuntu 16.04.我试图在 Ubuntu 16.04 上使用 Let's Encrypt 保护 Nginx。

example.conf file before obtaining an SSL Certificate获取SSL证书的example.conf文件

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/ is working fine. http://example.com/工作正常。

I try to Obtain an SSL Certificate by我尝试通过以下方式获得 SSL 证书

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

the result was结果是

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 file after obtaining an SSL Certificate获得SSL证书的example.conf文件

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/ is redirecting to https://example.com/ too many times http://example.com/重定向到https://example.com/的次数过多

example.com redirected you too many times. example.com 将您重定向了太多次。 ERR_TOO_MANY_REDIRECTS ERR_TOO_MANY_REDIRECTS

  1. Why is it redirecting too many times?为什么重定向太多次?

  2. what is the purpose of the second server block?第二个服务器块的目的是什么?

     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. How to make all redirects to https://www.example.com/ ?如何将所有重定向到https://www.example.com/

EDIT1编辑1

Moving the certibot managed code to second server block has stopped the too many redirects problem.将 certibot 托管代码移动到第二个服务器块已经解决了太多重定向问题。 But my website is back again directing to HTTP instead of https.但是我的网站再次指向HTTP而不是 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

        }

Try this:试试这个:

server {
    server_name example.com www.example.com ;
    listen 80;
    listen 443 ssl;

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

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

    # 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;

    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

}

This should redirect to www.example.com if used HTTP instead of HTTPS or host is example.com这应该重定向到www.example.com如果使用 HTTP 而不是 HTTPS 或者主机是example.com

regarding your questions:关于你的问题:

  1. I am not sure, maybe you also redirect in your ruby code.我不确定,也许您还重定向了 ruby 代码。 Your EDIT1 code example will redirect forever, since it redirects using the $host您的 EDIT1 代码示例将永远重定向,因为它使用 $host 重定向

  2. weird way to redirect traffic on port 80 (used for HTTP)在端口 80 上重定向流量的奇怪方式(用于 HTTP)

  3. In my example I fixed the redirect to www.example.com instead of $host在我的示例中,我将重定向修复为www.example.com而不是 $host

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM