2

I implemented an SSL certificate for my Rails 4 app, with NGinx and puma. I used the following nginx.conf file:

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

And it works fine when accessing https:// URLs. But the links of the app don't use the HTTPS protocol so I added config.force_ssl = true in config/environments/production.rb :

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

But then get:

This webpage has a redirect loop

From my browser! I looked for many sources (including https://www.digitalocean.com/community/tutorials/how-to-create-an-ssl-certificate-on-nginx-for-ubuntu-14-04 and https://gist.github.com/rkjha/d898e225266f6bbe75d8) but don't know exactly which line I missed. Any help?

mikej
  • 65,295
  • 17
  • 152
  • 131
Hassen
  • 6,966
  • 13
  • 45
  • 65

1 Answers1

4

You'll need to set a header so that rails knows it's getting the traffic through https. Checkout this answer

Community
  • 1
  • 1
voxobscuro
  • 2,132
  • 1
  • 21
  • 45
  • 1
    You're right @voxobscuro, I added `proxy_set_header X-Forwarded-Proto https;` to the nginx script, and it worked fine. Thanks – Hassen Nov 13 '15 at 21:14
  • 1
    this was really hard to hunt down, can't thank this q&a enough. Rails5 puma/nginx/deployment incoming! – tw airball Jan 22 '16 at 09:32