Skip to content

Instantly share code, notes, and snippets.

@davidcelis
Last active June 15, 2018 13:11
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save davidcelis/69eb6c707b6f4a6c1391 to your computer and use it in GitHub Desktop.
Save davidcelis/69eb6c707b6f4a6c1391 to your computer and use it in GitHub Desktop.
Nginx configuration to redirect HTTP traffic to HTTPS (Puma)
upstream puma {
server unix:///var/www/app/shared/tmp/sockets/puma.sock fail_timeout=0;
}
server {
listen 80 default deferred;
server_name example.com;
rewrite ^/(.+) https://example.com/$1 permanent;
}
server {
listen 443 ssl spdy;
server_name example.com;
ssl_certificate /etc/ssl/example.com-unified.crt;
ssl_certificate_key /etc/ssl/example.com.key;
root /var/www/example.com/current/public;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
proxy_redirect off;
if (-f $request_filename) {
break;
}
proxy_pass http://puma;
}
client_max_body_size 4G;
keepalive_timeout 10;
}
@salmanasiddiqui
Copy link

rewrite ^/(.+) https://example.com/$1 permanent;

better approach than the above would be,

return 301 https://$host$request_uri;

@pconerly
Copy link

@salmanasiddiqui cool, is the benefit that we don't have to specify the domain again?

@nicolasembleton
Copy link

Also cleaner and less prone to mistakes/corner-cases, since handled by nginx directly ootb.

@BlakeB415
Copy link

i made a mistake how do i fix the rewrite

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment