gpt4 book ai didi

ruby-on-rails - example.com 重定向您的次数过多。 ERR_TOO_MANY_REDIRECTS

转载 作者:行者123 更新时间:2023-12-04 18:37:28 25 4
gpt4 key购买 nike

我试图在 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


  • 为什么它重定向太多次?
  • 第二个服务器 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

    }
  • 如何使所有重定向到 https://www.example.com/ ?

  • 编辑1
    将 certibot 托管代码移动到第二个服务器 block 已经停止了过多的重定向问题。但是我的网站又回来了,指向 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

    }

    最佳答案

    尝试这个:

    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

    }
    这应该重定向到 www.example.com如果使用 HTTP 而不是 HTTPS 或主机是 example.com关于您的问题:
  • 我不确定,也许你也在你的 ruby​​ 代码中重定向。您的 EDIT1 代码示例将永远重定向,因为它使用 $host 重定向
  • 在端口 80 上重定向流量的奇怪方式(用于 HTTP)
  • 在我的示例中,我将重定向固定为 www.example.com而不是 $host
  • 关于ruby-on-rails - example.com 重定向您的次数过多。 ERR_TOO_MANY_REDIRECTS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61412501/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com