Hey! If you love Linux as much as I do and want to learn more about it, or possibly get some work,let's connect on LinkedIn. I talk about this stuff all the time!

Mastering NGINX with Ruby on Rails

Learn how to configure and optimize NGINX for your Ruby on Rails applications, ensuring high performance, scalability, and security. …


Updated September 20, 2024

Learn how to configure and optimize NGINX for your Ruby on Rails applications, ensuring high performance, scalability, and security.

As a Ruby on Rails developer, you’re likely familiar with the concept of using a reverse proxy server to sit in front of your application. One popular choice for this is NGINX. In this article, we’ll explore what NGINX is, its importance, and how to configure it for use with Ruby on Rails.

What is NGINX?

NGINX (pronounced “engine-x”) is a free, open-source web server software that can also act as a reverse proxy, load balancer, and HTTP cache. Its primary function is to handle incoming requests from clients, such as web browsers, and forward them to the appropriate backend server.

Why Use NGINX with Ruby on Rails?

Using NGINX in front of your Ruby on Rails application provides several benefits:

  1. Improved Performance: By serving static assets directly from NGINX, you reduce the load on your Rails application.
  2. Increased Security: NGINX can act as a buffer between the internet and your Rails application, protecting it from potential attacks.
  3. Scalability: With NGINX handling requests and distributing them across multiple backend servers, your application becomes more scalable.

Step-by-Step Configuration

To get started with NGINX and Ruby on Rails, follow these steps:

Step 1: Install NGINX

You can install NGINX using the package manager of your choice. For example, on Ubuntu or Debian-based systems:

sudo apt-get update && sudo apt-get install nginx

On Red Hat-based systems (e.g., CentOS):

sudo yum install nginx

Step 2: Configure NGINX

Create a new configuration file in the /etc/nginx/conf.d/ directory, e.g., rails_app.conf. In this file, add the following basic configuration:

server {
    listen 80;
    server_name your_rails_app.com;

    location / {
        proxy_pass http://localhost:3000; # assuming Rails app runs on port 3000
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    location ~* \.(jpg|jpeg|png|gif)$ {
        root /path/to/your/app/public/assets/images/;
        expires 1w; # cache images for 1 week
    }
}

This configuration:

  • Listens on port 80 (HTTP)
  • Proxies incoming requests to the Rails application running on localhost:3000
  • Caches static image files in the /public/assets/images/ directory

Step 3: Update Rails Application Configuration

In your Rails application’s config/environments/production.rb file, update the following settings:

Rails.application.configure do
  # ...
  config.action_controller.asset_host = 'http://your_rails_app.com' # NGINX server name
  # ...
end

This sets the asset host to point to your NGINX server.

Step 4: Restart and Verify

Restart both NGINX and your Rails application:

sudo service nginx restart && sudo rails s -e production

Verify that NGINX is serving static assets correctly by accessing your Rails app through the NGINX server. Check for proper caching headers in the HTTP responses.

Advanced Configuration Options

  • SSL Termination: Configure NGINX to handle SSL termination and encryption/decryption.
  • Load Balancing: Use NGINX as a load balancer across multiple backend servers.
  • Caching: Implement more advanced caching strategies, such as fragment caching or page caching.

Conclusion

By following these steps and configuring NGINX for your Ruby on Rails application, you’ll improve performance, scalability, and security. Remember to monitor and optimize your setup regularly to ensure optimal results.

Stay up to date on the latest in Linux with AI and Data Science

Intuit Mailchimp