Skip to content

Instantly share code, notes, and snippets.

@rkjha
Last active March 25, 2021 15:47
Show Gist options
  • Save rkjha/057fd084e65651326ee8 to your computer and use it in GitHub Desktop.
Save rkjha/057fd084e65651326ee8 to your computer and use it in GitHub Desktop.
Nginx/Passenger config when using SSL with a Ruby/Rails Application.
# for redirecting hhtp traffic to https version of the site
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
}
# for redirecting to non-www version of the site
server {
listen 80;
server_name www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
server_name example.com;
passenger_enabled on;
rails_env production;
root /home/user/example_user/current/public;
ssl on;
ssl_certificate /home/example_user/.certs/ssl-bundle.crt;
ssl_certificate_key /home/example_user/.certs/example.com.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
@robfuller
Copy link

This was so helpful - thank you for sharing it.

@syndbg
Copy link

syndbg commented Dec 6, 2016

Just a small hint how you can shorten your config with no functionality loss

# for redirecting http traffic to https version of the site
server {
  listen 80;
  server_name example.com www.example.com;
  return 301 https://example.com$request_uri;
}

Instead of

# for redirecting hhtp traffic to https version of the site
server {
  listen 80;
  server_name example.com;
  return 301 https://$server_name$request_uri;
}

# for redirecting to non-www version of the site
server {
    listen  80;
    server_name  www.example.com;
    return 301 https://example.com$request_uri;
}

@an-nasir
Copy link

Hi, I am new here, if you can specify where should I put this file? and how can I refer this from nginx.conf? Or can I put the content of this file directly in nginx.conf?

Thanks

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