【问题标题】:Get infinite redirect loop with Rails 4, NGinx and puma [duplicate]使用 Rails 4、NGinx 和 puma 获取无限重定向循环 [重复]
【发布时间】:2016-02-15 12:57:43
【问题描述】:

我使用 NGinx 和 puma 为我的 Rails 4 应用程序实现了 SSL 证书。我使用了以下 nginx.conf 文件:

upstream puma {
  server unix:///home/deploy/apps/bestpark/shared/tmp/sockets/bestpark-puma.sock;
}

server {
  listen 80;
  server_name lebonparking.fr;
  rewrite ^/(.*) https://lebonparking.fr/$1 permanent;
}

server {
  listen 443;
  server_name lebonparking.fr;

  ssl on;
  ssl_certificate /home/deploy/apps/bestpark/current/certificates/lebonparking.fr.crt;
  ssl_certificate_key /home/deploy/apps/bestpark/current/certificates/lebonparking.fr.key;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL;

  root /home/deploy/apps/bestpark/current/public;
  access_log /home/deploy/apps/bestpark/current/log/nginx.access.log;
  error_log /home/deploy/apps/bestpark/current/log/nginx.error.log info;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @puma;
  location @puma {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://puma;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 10M;
  keepalive_timeout 10;
}

在访问https:// URL 时它工作正常。但是应用程序的链接不使用 HTTPS 协议,所以我在 config/environments/production.rb 中添加了config.force_ssl = true

Rails.application.configure do
  ...
  config.force_ssl = true
  ...
end

然后得到:

This webpage has a redirect loop

来自我的浏览器!我查找了许多来源(包括https://www.digitalocean.com/community/tutorials/how-to-create-an-ssl-certificate-on-nginx-for-ubuntu-14-04https://gist.github.com/rkjha/d898e225266f6bbe75d8),但不知道我错过了哪一行。有什么帮助吗?

【问题讨论】:

    标签: ruby-on-rails ssl nginx


    【解决方案1】:

    您需要设置一个标头,以便 rails 知道它通过 https 获取流量。 Checkout this answer

    【讨论】:

    • 你说得对@voxobscuro,我在nginx脚本中添加了proxy_set_header X-Forwarded-Proto https;,它运行良好。谢谢
    • 这真的很难找到,非常感谢这个问答。 Rails5 puma/nginx/部署进来了!
    猜你喜欢
    • 2023-03-19
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 2021-10-21
    • 2011-03-29
    • 2020-09-22
    • 1970-01-01
    相关资源
    最近更新 更多