【问题标题】:example.com redirected you too many times. ERR_TOO_MANY_REDIRECTSexample.com 重定向您的次数过多。 ERR_TOO_MANY_REDIRECTS
【发布时间】:2020-08-08 06:29:23
【问题描述】:

我试图在 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 重定向您的次数过多。 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 托管代码移动到第二个服务器块已经停止了重定向过多的问题。但是我的网站又回来了,指向 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

        }

【问题讨论】:

    标签: ruby-on-rails ubuntu ssl nginx lets-encrypt


    【解决方案1】:

    试试这个:

    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
    
    }
    

    如果使用 HTTP 而不是 HTTPS 或主机是 example.com,这应该重定向到 www.example.com

    关于您的问题:

    1. 我不确定,也许你也在你的 ruby​​ 代码中重定向。您的 EDIT1 代码示例将永远重定向,因为它使用 $host

      重定向
    2. 在端口 80 上重定向流量的奇怪方法(用于 HTTP)

    3. 在我的示例中,我将重定向固定为 www.example.com 而不是 $host

    【讨论】:

      猜你喜欢
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 2020-08-22
      • 2020-01-03
      • 1970-01-01
      • 2019-05-07
      • 1970-01-01
      • 2016-10-15
      相关资源
      最近更新 更多