0
0
RailsHow-ToBeginner · 4 min read

How to Use Nginx with Ruby on Rails: Setup and Configuration

To use nginx with a Ruby on Rails app, configure nginx as a reverse proxy that forwards requests to a Rails app server like Puma. Set up nginx to listen on port 80 and proxy requests to Puma running on a local port (e.g., 3000), enabling efficient handling of web traffic.
📐

Syntax

This is the basic nginx server block syntax to proxy requests to a Rails app running on Puma:

  • listen 80; — Nginx listens on HTTP port 80.
  • server_name example.com; — Your domain or IP.
  • location / { proxy_pass http://localhost:3000; } — Forward requests to Puma on port 3000.
  • proxy_set_header directives — Pass client info to Rails.
nginx
server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
💻

Example

This example shows a full nginx config to serve a Rails app with Puma on port 3000. It includes headers to forward client info and basic proxy settings.

Make sure Puma is running your Rails app on port 3000 before starting nginx.

nginx
server {
    listen 80;
    server_name myrailsapp.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_redirect off;
    }
}
Output
When you visit http://myrailsapp.com, nginx forwards requests to Puma on localhost:3000, serving your Rails app.
⚠️

Common Pitfalls

  • Not running Puma: Nginx proxies to Puma, so Puma must be running on the specified port.
  • Wrong port: Ensure Puma and nginx use the same port in config.
  • Missing headers: Without proxy_set_header, Rails may not get correct client info.
  • Firewall blocking ports: Make sure ports 80 (nginx) and Puma's port are open.
nginx
server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://localhost:3000;
        # Missing proxy_set_header lines can cause issues
    }
}

# Correct way includes proxy_set_header directives as shown in previous examples.
📊

Quick Reference

Tips for using nginx with Rails:

  • Run Puma as your Rails app server on a local port (default 3000).
  • Configure nginx to listen on port 80 and proxy requests to Puma.
  • Include proxy_set_header directives to forward client info.
  • Reload nginx after config changes with sudo nginx -s reload.
  • Use systemd or a process manager to keep Puma running in production.

Key Takeaways

Use nginx as a reverse proxy forwarding requests to Puma running your Rails app.
Always include proxy headers in nginx config to pass client info correctly.
Ensure Puma is running on the port nginx proxies to before starting nginx.
Reload nginx after any configuration changes to apply them.
Use a process manager to keep Puma running reliably in production.