Skip to content

Instantly share code, notes, and snippets.

@hoyin258
Last active August 29, 2015 14:01
Show Gist options
  • Save hoyin258/4a961dd30ace6e97de3a to your computer and use it in GitHub Desktop.
Save hoyin258/4a961dd30ace6e97de3a to your computer and use it in GitHub Desktop.

#Nginx => Unicorn > Rails

##Nginx Setting

vim /etc/nginx/conf.d/default.conf
upstream app {
    # Path to Unicorn SOCK file, as defined previously
    server unix:/tmp/unicorn.myapp.sock fail_timeout=0;
}

server {


    listen 80;
    server_name localhost;

    # Application root, as defined previously
    root /root/my_app/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://app;
    }

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

###Add permission for logging

sudo chown -R www-data:www-data /var/log/nginx;
sudo chmod -R 755 /var/log/nginx;

##Add Unicorn to Rails

mkdir pids
vim config/unicorn.rb
# Set the working application directory
# working_directory "/path/to/your/app"
working_directory "/var/www/my_app"

# Unicorn PID file location
# pid "/path/to/pids/unicorn.pid"
pid "/var/www/my_app/pids/unicorn.pid"

# Path to logs
# stderr_path "/path/to/log/unicorn.log"
# stdout_path "/path/to/log/unicorn.log"
stderr_path "/var/www/my_app/log/unicorn.log"
stdout_path "/var/www/my_app/log/unicorn.log"

# Unicorn socket
listen "/tmp/unicorn.[app name].sock"
listen "/tmp/unicorn.myapp.sock"

# Number of processes
# worker_processes 4
worker_processes 2

# Time-out
timeout 30

##Run Unicorn

unicorn_rails -c config/unicorn.rb -D

##Rstart Nginx

sudo service nginx restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment