现在的位置: 首页 > 综合 > 正文

Nginx + Unicorn 部署 Rails 完整配置

2018年09月08日 ⁄ 综合 ⁄ 共 3611字 ⁄ 字号 评论关闭

nginx+unicorn 完整配置,主要的是,nginx.conf 和 unicorn.rb 这两个配置文件

nginx 的源码安装 指定passenger目录

源码下载地址  http://nginx.org/en/download.html

编译源码  

./configure --prefix=/srv/nginx/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --add-module=/usr/local/rvm/gems/ruby-2.0.0-p598/gems/passenger-5.0.4/ext/nginx/


make  && make install

1.配置nginx文件          sudo vim /opt/nginx/conf/nginx.conf

#=== CPU ====
user menxu menxu;
worker_processes 4;
worker_cpu_affinity 00000001 00000010 00000100 00001000;
worker_rlimit_nofile 204800;


events {
  use epoll;
  worker_connections 204800;
}


# === HTTP ===


http{
  include      mime.types;
  default_type application/octet-stream;


  charset utf-8;


  server_names_hash_bucket_size 128;
  client_header_buffer_size     2k;
  large_client_header_buffers   4 4k;
  client_max_body_size          6m; # 单次请求数据最大允许上传尺寸


  sendfile          on;
  tcp_nopush        on;
  tcp_nodelay       on;
  keepalive_timeout 120;
  send_timeout      120;


  gzip              on;
  gzip_buffers      4 32k;
  gzip_min_length   1k;
  gzip_comp_level   2;
  gzip_http_version 1.1;
  gzip_types        text/plain application/x-javascript text/css application/xml;


  # ===== App Server
  upstream app_server{
      server unix:/tmp/unicorn_padrino.sock fail_timeout=0;
  }


  # /web/ruby/blogs
  server{
  listen          80;
  charset         utf-8;
  server_name     menxu.com;
  keepalive_timeout 5;
  
  root            /home/menxu/web/ruby/blogs/public;
  access_log      /home/menxu/web/ruby/blogs/log/nginx_access.log;
  error_log       /home/menxu/web/ruby/blogs/log/nginx_error.log;
  rewrite_log     on;
  
  try_files       $uri/index.html $uri.html $uri @app_server;
  
  location ~* ^/(images|javascripts|stylesheets|img)/{
  access_log      off;
  log_not_found   off;
  expires         max;
  break;
  }
  
  location @app_server {
  proxy_set_header Host $host;
  proxy_set_header X-Forwarded-Host $host;
  proxy_set_header X-Forwarded-Server $host;
  proxy_set_header X-Real-IP        $remote_addr;
  proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
  proxy_buffering  on;
  proxy_redirect  off;
  
      proxy_pass http://app_server;
  }
  }


}

2.创建 config/unicorn.rb 并 配置unicorn 文件    sudo vim /home/menxu/web/ruby/blogs/config/unicorn.rb 

# -*- encoding: utf-8 -*-
user("menxu","menxu")


root_path = File.expand_path '../', File.dirname(__FILE__)
#log
log_file = root_path + '/log/unicorn.log'
err_log  = root_path + '/log/unicorn_error.log'


# process
pid_file = '/tmp/unicorn_padrino.pid'
old_pid = pid_file + '.oldbin'


#thron
socket_file = '/tmp/unicorn_padrino.sock'


worker_processes 2
working_directory root_path
timeout 30


#listen
listen 8080, tcp_nopush: false
listen socket_file, backlog: 1024


pid pid_file
stderr_path err_log
stdout_path log_file


preload_app true


before_exec do |server|
  ENV['BUNDLE_GEMFILE'] = root_path + '/Gemfile'
end


before_fork do |server, worker|
  if File.exists?(old_pid) && server.pid != old_pid
    begin
      Process.kill('QUIT', File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
      puts "Send 'QUIT' signal to unicorn error!"
    end
  end
end

3.编写启动脚本文件  sudo vim ./blog_server.sh

1>安装wrapper 创建带环境的unicorn运行命令

rvm wrapper ruby-2.0.0-p195@blog2 bootup unicorn

2> 编辑启动脚本

#! /bin/sh
# rvm wrapper ruby-2.0.0-p195@blog2 bootup unicorn
UNICORN=/home/menxu/.rvm/bin/bootup_unicorn
CONFIG_FILE=/home/menxu/web/ruby/blogs/config/unicorn.rb
APP_HOME=/home/menxu/web/ruby/blogs


case "$1" in
  start)
  $UNICORN -c $CONFIG_FILE -E production -D
  ;;
  stop)
  kill -QUIT 'cat /tmp/unicorn_padrino.pid'
  ;;
  restart|force-reload)
  kill -USR2 'cat /tmp/unicorn_padrino.pid'
  ;;
  *)
  echo "Usage: $SCRIPTNAME{start|top|restart|force-reload" >&2
  exit 3
  ;;
esac

4.启动命令

sudo ln -s ~/nginx.conf /etc/nginx/conf.d/unicorn.conf # 关联配置
chmod +x ./one.sh # 执行权限
./blog_server.sh # 启动unicorn
sudo service nginx start


5.网站优化搜集

关于Nginx的一些优化(突破十万并发)

http://www.howtocn.org/nginx:%E9%85%8D%E7%BD%AE%E4%B8%80%E4%B8%AA%E9%AB%98%E6%80%A7%E8%83%BD%E7%9A%84nginx_fastcgi%E6%9C%8D%E5%8A%A1%E5%99%A8

6.其他的配置

Nginx一个IP配置多个主机    http://blog.csdn.net/ygrx/article/details/9295673

7. 书籍  深入理解Nginx (模块开发与架构设计)

http://www.ppurl.com/pdfpreview/?skey=UFUHNgUrUyZXMQdsA19TPAUnAGQNYAdjVTUCNgQ8AGc%3D&page=0

抱歉!评论已关闭.