Nginx with Unicorn to serve your rails app

This is currently the most popular solution to serve rails apps and is used by many big companies like Github, tweet, 37signals and so on. Also, it is very easy to setup in this way.

Config unicorn

Install gem unicorn:

Put gem ‘unicorn’ in Gemfile
then bundle install

Edit unicorn config file

Vim config/unicorn.rb under your rails app root dir.
content:

# The ONLY two things you should change, if you don’t need any specialty.
app_name = "your_app_name"
number_of_app_instances = 2
# Set the working application directory. This should be your rails app root dir, not the public dir
app_root = File.expand_path(File.dirname(__FILE__) + '/..')
working_directory app_root

# File to store unicorn pid
# pid "/path/to/pids/unicorn.pid"
pid "#{app_root}/tmp/pids/unicorn.pid"

# Path to logs
# stderr_path "/path/to/log/unicorn.log"
# stdout_path "/path/to/log/unicorn.log"
stderr_path "#{app_root}/log/unicorn.log"
stdout_path "#{app_root}/log/unicorn.log"

# Unicorn socket
listen "/tmp/unicorn.#{app_name}.sock"

# Number of processes
worker_processes number_of_app_instances

# Time-out
timeout 30

Nginx config

Install nginx

sudo apt-get update
sudo apt-get install nginx

Default nginx config file would be located in /etc/nginx

Nginx config file

Add a file in sites_available and put content:

upstream app {
    # Path to Unicorn SOCK file, as defined previously
    server unix:/tmp/unicorn.your_app_name.sock fail_timeout=0;
}

# configure server for your app
server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    root /path/to/your/rails/root/public;

    location ^~ /assets/ {
        gzip_static on;
        expires max;
        add_header Cache-Control public;
    }

    try_files $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;
    }
}

Make a soft link in sites-enabled to the above nginx config file

Reload nginx

sudo service nginx restart

Start serving

Compile assets: rake assets:precompile

If you only want to server your app in development environment, you don't need this step.

Start unicorn in production

bundle exec unicorn -c config/unicorn.rb -E production

更多文章請(qǐng)?jiān)L問(wèn)個(gè)人博客
作者:鄒小創(chuàng)
Github
微博
郵件:happystriving@126.com

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Awesome Ruby Toolbox Awesome A collection of awesome Ruby...
    debbbbie閱讀 2,929評(píng)論 0 3
  • NO.1購(gòu)買域名并做域名解析 NO.2購(gòu)買主機(jī) NO.3測(cè)試主機(jī)聯(lián)通 ping x.x.x.x NO.4登錄到服務(wù)...
    youngiyang_打碼少年閱讀 1,163評(píng)論 0 16
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,923評(píng)論 18 139
  • [多安家學(xué)經(jīng)匯報(bào)] 學(xué)經(jīng)時(shí)間:2017年2月15日 星期三 正月十九 晴好 學(xué)經(jīng)人員:多媽、多多(六年級(jí),累積學(xué)...
    多安媽閱讀 154評(píng)論 0 1
  • 不要等到明天,明天太遙遠(yuǎn),今天就行動(dòng)。 須讀:看完該文章你能做什么? 數(shù)組中對(duì)象的內(nèi)存管理 學(xué)習(xí)前:你必須會(huì)什么?...
    liyuhong閱讀 188評(píng)論 0 0