Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

Unicorn + Nginx + CentOS 部署實作與伺服器配置

要把 dashboard 部署到伺服器上,除了前端本身,還需要一個能跑 Ruby 程式的應用伺服器,這裡選用 Unicorn。它的定位有點像部署 Python 時常見的 Gunicorn。

Unicorn 是什麼

Unicorn 是為 Rack 應用設計的 HTTP server,它善用 Unix 的進階特性,專為低延遲、高頻寬的連線情境打造。

安裝

gem install unicorn

撰寫 unicorn 設定檔

# -*- encoding: utf-8 -*-
root_path = File.expand_path '../', File.dirname(__FILE__)

# 日誌
log_file = root_path + '/log/unicorn.log'
err_log  = root_path + '/log/unicorn_error.log'

# 行程標識
pid_file = '/tmp/unicorn_padrino.pid'
old_pid  = pid_file + '.oldbin'

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

worker_processes 6
working_directory root_path
timeout 30

# 偵聽
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

這裡同時開了 TCP 連接埠 8080 與 Unix socket,兩者擇一即可;用 socket 的好處是少一層 TCP 堆疊開銷。worker_processes 依 CPU 核心數調整,timeout 則是單一請求的允許處理時間。

設定 Nginx 反向代理

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

server {
    listen 80;
    charset utf-8;
    server_name dashboard.example.com;
    keepalive_timeout 5;

    root /home/www/iot-dashboard;
    access_log /home/www/iot-dashboard/log/nginx_access.log;
    error_log  /home/www/iot-dashboard/log/nginx_error.log;
    rewrite_log on;

    location ~* ^/(images|javascripts|stylesheets|img)/ {
        access_log off;
        log_not_found off;
        expires max;
        break;
    }

    location / {
        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-Forward-For $proxy_add_x_forwarded_for;
        proxy_buffering on;
        proxy_redirect off;
        if (!-f $request_filename) {
            proxy_pass https://app_server;
            break;
        }
    }
}

靜態資源(圖片、JS、CSS)直接由 Nginx 送出並設定長時間快取,其餘請求才轉給 upstream,這是 Nginx 搭配 Unicorn 最常見的分工。upstream 指向的是 unicorn 設定檔裡那個 socket。

建立啟動腳本

#!/bin/sh
# rvm wrapper ruby-1.9.3-p194 bootup
UNICORN=unicorn
CONFIG_FILE=/home/www/iot-dashboard/config/unicorn.rb
APP_HOME=/home/www/iot-dashboard

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|stop|restart|force-reload}" >&2
        exit 3
        ;;
esac
:

三個訊號各有分工:QUIT 是優雅關閉,會等現有請求處理完;USR2 是熱重載,會先起新 master 再交接,配合設定檔裡的 before_fork 就能做到幾乎無感斷線;TERM 則是強制結束。記得給腳本執行權限:chmod +x,正式上線時再掛進 systemd 或 supervisor 讓它開機自啟。

Copyright © Linux教程網 All Rights Reserved