Simplifying Nginx Configuration for Your Rails App

Installation

Installation

Begin by installing Nginx on your Ubuntu system using the following command:

$ sudo apt install nginx

This command not only installs Nginx but also generates default configurations in /etc/nginx/sites-available. These configurations can be modified to suit your needs.

Understanding Nginx Configurations

In Nginx, configurations are pivotal for directing web server behavior. Two directories, sites-available and sites-enabled, are automatically created upon Nginx installation. Draft configurations are stored in sites-available, while configurations to be published are either copied or symlinked to sites-enabled.

Default Configuration

The default Nginx configuration provides a starting point for your server.

# Default server configuration
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        try_files $uri $uri/ =404;
    }
}

Configuration for HTTP

For a simple HTTP configuration, create a configuration file such as /etc/nginx/sites-available/myrailsapp.conf:

server {
    listen 80;
    listen [::]:80;

    server_name myrailsapp.ideabreed.net;

    root /var/www/myrailsapp/public;

    try_files $uri/index.html $uri @app;

    location @app {
        proxy_pass http://localhost:40;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}

Make sure your Rails app is running on port 40 or a similar port.

Configuration for HTTPS with SSL

For a secure HTTPS configuration, you’ll need SSL certificates. Certbot can help automate this process.

server {
    server_name myrailsapp.ideabreed.net;

    root /var/www/production/myrailsapp/public;

    try_files $uri/index.html $uri @app;

    location @app {
        proxy_pass http://localhost:40;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto https;
        proxy_redirect off;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;

    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/ideabreed.net/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/ideabreed.net/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}

server {
    if ($host = myrailsapp.ideabreed.net) {
        return 301 https://$host$request_uri;
    }

    listen 80;
    server_name myrailsapp.ideabreed.net;
    return 404;
}

Publish the Configuration

Publish Configuration

Create a symlink of the configuration file in sites-available to sites-enabled:

ln -s /etc/nginx/sites-available/myrailsapp.conf /etc/nginx/sites-enabled/myrailsapp.conf -f

Then, restart the Nginx server:

sudo systemctl restart nginx

HTTPS Configuration with CertBot

CertBot

Visit Certbot and select your HTTP server (e.g., nginx) and OS (e.g., Ubuntu 18.04). The site will provide instructions on installing Certbot on your machine.

Auto Renewal Every Year

To enable auto-renewal, run the following command:

certbot --dry-run

These streamlined configurations will help you set up your Rails app with Nginx swiftly and securely. Feel free to adapt them according to your project requirements.