# 10. 部署
#### 1. 介紹
之前我們說過,有兩種方式可以運行在rails應用中運行websocket,一種是以`rack hijack`的方式嵌入到rails應用中,以路由的形式掛載,成為rails應用的一部分,這種方式的話,就不用另開一個進程,因為它是rails應用的一部分,而還有另一種式,就是以`Standalone`的方式,另開一個獨立于web進程的websocket進程,這個進程是專門處理websocket連接和請求的,這樣的話就把web和websocket進程分開了。
而今天我們要講的是websocket的部署。
這個部署分為兩部分,第一部分是websocket的進程的部署,另一部分是nginx的配置。
如果websocket是以`rack hijack`方式運行,就不用考慮進程的部署,只有當websocket是以`Standalone`方式運行的時候才要把那個進程部署起來,然而,不管是什么方式,nginx的配置都是需要的。
#### 2. 使用
當websocket是以`Standalone`方式運行時,在測試端是以下面的方式運行的。
```
bundle exec puma -p 28080 cable/config.ru
```
也就是說,我們要把這個指令產生的效果和nginx結合搬到服務器主機上。而且我們在每次部署自動控制這個服務器的重啟。
首先還是得先部署puma的服務,再來處理nginx。
##### 2.1 mina-puma的改造
剛開始會嘗試使用[mina-puma](https://github.com/sandelius/mina-puma)。
你會發現這個過程是失敗的。雖說mina-puma也是puma結合pumactl的指令來控制puma的啟動,重啟等。
但是查看下mina-puma就會知道,它也沒像上面那樣使用端口,也沒有指令配置文件。
所以我們需要結合我們自己的條件來改造。
在`config/deploy.rb`文件中添加下面的內容。
```
set :puma_cmd, -> { "#{bundle_prefix} puma" }
set :puma_pid, -> { "#{deploy_to}/#{shared_path}/pids/puma.pid" }
set :puma_state, -> { "#{deploy_to}/#{shared_path}/pids/puma.state" }
set :pumactl_cmd, -> { "#{bundle_prefix} pumactl" }
set :puma_env, -> { fetch(:rails_env, 'production') }
set :pumactl_socket, -> { "#{deploy_to}/#{shared_path}/tmp/sockets/pumactl.sock" }
set :puma_socket, -> { "#{deploy_to}/#{shared_path}/tmp/sockets/puma.sock" }
desc 'Start puma'
task :puma_start => :environment do
queue! %[
if [ -e '#{pumactl_socket}' ]; then
echo 'Puma is already running!';
else
cd #{deploy_to}/#{current_path} && #{puma_cmd} -q -d -e #{puma_env} -b 'unix://#{puma_socket}' -S #{puma_state} --pidfile #{puma_pid} --control 'unix://#{pumactl_socket}' #{deploy_to}/#{current_path}/cable/config.ru
fi
]
end
desc 'Stop puma'
task :puma_stop => :environment do
queue! %[
if [ -e '#{pumactl_socket}' ]; then
echo 'Puma is stopping!'
cd #{deploy_to}/#{current_path} && #{pumactl_cmd} -S #{puma_state} stop
rm -f '#{pumactl_socket}'
else
echo 'Puma is not running!';
fi
]
end
desc 'Restart puma'
task puma_restart: :environment do
invoke :'puma_stop'
invoke :'puma_start'
end
```
也可以把這些內容封裝成文件放到lib目錄,再來require,不過這不重要。
在mina部署重啟用應的地方引用就好了。
```
desc "Deploys the current version to the server."
task :deploy => :environment do
deploy do
invoke :'sidekiq:quiet'
...
to :launch do
...
invoke :'puma_restart'
...
end
end
end
```
上面的代碼顯示,還是用unix socket來監聽,而不用端口。所以接下來,對nginx的配置還是跟前面部署unicorn差不多,只是多了websocket的部分。
#### 2.2 nginx
nginx中的配置文件是這樣的。
```
upstream tt {
server unix:/tmp/unicorn_production.sock fail_timeout=0;
}
upstream ws {
server unix:///home/eason/tt_deploy/shared/tmp/sockets/puma.sock fail_timeout=0;
}
server {
server_name www.rails365.net;
root /home/eason/tt_deploy/current/public;
try_files $uri/index.html $uri @tt;
location @tt {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://tt;
}
location /ws/ {
proxy_pass http://ws;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
...
}
```
不重要的部分被我省略了,最重要的是`location /ws/`這部分。其實就多了三行關于websocket的配置,很簡單。
要測試是否配置成功。可以有兩種簡單的方法。
第一種是用chrome的開發者工具中的network部分查看是否有101狀態碼的請求。
第二種是在chrome的console里測試,比如`new WebSocket('ws://www.rails365.net/ws');`,如果沒報錯,返回正常 ,一般就沒問題的。有一點需要注意,假如用的是https,ws就得改成wss。
完結。