资讯专栏INFORMATION COLUMN

Nginx with Unicorn to serve your rails app

fasss / 2843人阅读

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

更多文章请访问个人博客
作者:邹小创
Github
微博
邮件:happystriving@126.com

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/39211.html

相关文章

  • 在阿里云 (aliyun) 服务器上搭建Ruby On Rails生产环境

    摘要:最近总是在配置阿里云的服务器,遇到不少问题,现小结一下,供大家参考阿里云的一键安装全环境下载一键安装全环境压缩包上传至服务器,解压执行脚本,具体步骤详见这里任意选择一种方法执行脚本方法一方法二安装与指定的版本安装与指定的版本注意安 最近总是在配置阿里云的服务器,遇到不少问题,现小结一下,供大家参考~~ 1、阿里云的一键安装web全环境 下载一键安装web全环境 sh.zip 压缩...

    Snailclimb 评论0 收藏0
  • centos6.2安装gitlab6.4

    摘要:环境准备版本版本版本版本版本因系列的版本是的,已经支持,所以不必升级版本。 环境准备 python版本2.6 git版本 1.8.4.1 ruby版本ruby-2.0.0-p353 gitlab-shell版本 v1.8.0 gitlab版本6.4.3 因centos6系列的python版本是2.6的,已经支持,所以不必升级python版本。 在centos5下面需要升级pyt...

    selfimpr 评论0 收藏0
  • 使用passenger在Centos7部署nginx+Ruby on Rails

    摘要:是一个能快速搭建环境的工具,它能快速的将和部署到你的服务器中,是部署环境就如同环境那样简单快速,让人愉悦。在上跑一般只有在生产环境下才会使用,因而默认下就是环境设置为生产环境,而初始化时默认没有对生产环境进行密钥配置。 passenger passenger是一个能快速搭建web环境的工具,它能快速的将nginx和passenger部署到你的服务器中,是部署ruby环境就如同php环...

    UsherChen 评论0 收藏0
  • Rails が production 環境で真っ白、SECRET_KEY_BASE 設定忘れが原因で

    摘要:環境確認確認用見出。環境変数設定見。生成値設定正常動作確認。環境変数渡場合定番。 production 環境で、なぜか Rails アプリケーションの画面が真っ白になってしまった。 — 環境 — Rails 4.1 Unicorn エラーログを確認 unicorn のエラーログを確認しますと… $ tail -f log/unicorn-stderr.log E, [2014-...

    MRZYD 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<