Wednesday, July 12, 2017

Nginx virtualhost example for Passenger / Rails

For those of us coming from the Apache world, getting a working Nginx virtualhost configured for Rails can be quite the headache. It's actually pretty simple once you know what's going on. After installing nginx and passenger-phusion for nginx, all you need is the following (Note: the location portion of the config is for setting resource expiration to 1 month. You may need to adjust this based on your needs):

server {
listen 80;
listen [::]:80;
server_name your_domain.com www.your_domain.com;
root /var/www/html/app_name/public;
passenger_enabled on;
passenger_app_env production;
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 744h;
log_not_found off;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
}
view raw default hosted with ❤ by GitHub
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name your_domain.com www.your_domain.com;
root /var/www/html/app_name/public;
passenger_enabled on;
passenger_app_env production;
ssl_certificate /etc/letsencrypt/live/your_domain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/your_domain.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 744h;
log_not_found off;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
}
view raw ssl hosted with ❤ by GitHub