Use nginx and unicorn to deploy multiple Rails applications

12 Jan, 2015nginx, Rails

nginx It’s a common practice to do reverse proxy. unicron is the more popular Ruby language HTTP server. The two combine to deploy multiple [Rails][Rails ] The program is also a common application scenario. This article will introduce how to configure and start the service.

1. unicorn configuration file

Create a new file in the Rails application directory


And add in the unicorn.rb file

# Set the working application directory
# working_directory "/path/to/your/app"
working_directory "/home/todos"

# Unicorn PID file location
# pid "/path/to/pids/"
pid "/home/todos/pids/"

# Path to logs
# stderr_path "/path/to/log/unicorn.log"
# stdout_path "/path/to/log/unicorn.log"
stderr_path "/home/todos/log/unicorn.log"
stdout_path "/home/todos/log/unicorn.log"

# Unicorn socket
listen "/tmp/unicorn.todos.sock"

# Number of processes
# worker_processes 4
worker_processes 1

# Time-out
timeout 30
  • Replace todos in the above code with your own Rails application name

2. nginx configuration file

open a file


Add in http configuration

upstream rails_todos {
    server unix:/tmp/unicorn.todos.sock fail_timeout=0;

server {
    listen 80;
    root /home/todos/public;
    try_files $uri/index.html $uri @app;
    location @app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://rails_todos;
  • Replace the content of upstream with the sock file of the listen item in the unicorn configuration above
  • Replace server_name of server with the domain name to access this Rails application
  • Replace the root of the server with /public in the directory of this Rails application

3. Start unicorn service

Go to the Rails application directory and run the command

unicorn_rails -c config/unicorn.rb -D

4. Restart nginx service

service nginx restart

Powered by Gatsby. Theme inspired by end2end.

© 2014-2021. Made withby mdluo.