使用Rails 4,NGinx和puma获得无限重定向循环

时间:2015-11-13 20:34:31

标签: ruby-on-rails ssl nginx

我为我的Rails 4应用程序实现了SSL证书,包括NGinx和puma。我使用了以下 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://网址时效果很好。但该应用的链接不使用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),但我并不知道我错过了哪一行。有什么帮助吗?

1 个答案:

答案 0 :(得分:4)

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