Nginx Unitを試してみた話
Nginx Unitってなに?
・NGINX社製 軽量アプリケーションサーバ。
・RESTful APIやJSON経由でサービス無停止でリアルタイムに設定変更が出来
る。
・1つのサーバに複数の言語の実行系や同一言語の複数バージョンの実行系を搭
載出来る。
・4月に正式版リリース
・最新バージョンは1.1
Nginx Unitってなに?
Nginx Unitってなに?
Nginx Unitってなに?
Nginx Unitってなに?
今後の予定
・SSL/TLSとHTTP/2のサポート
・Node.jsのサポート
・Javaサポート
CentOS: yum install unit
インストール
※その他詳細はWebで!
Ubuntu: apt-get install unit
Source Code: ./configure
make
make install
起動方法
ubuntu / centos : systemctl start unitd
source : unitd --log /var/log/unitd.log --pid /run/unitd.pid
設定方法
curl -X PUT -d @php_config.json --unix-socket control.unit.sock http://localhost/
{
"listeners": {
"*:80": {
"application": "hello_php"
}
},
"applications": {
"hello_php": {
"type": "php",
"processes": 10,
"root": "/var/www/php/",
"index": "index.php"
}
}
}
設定方法
※その他詳細はWebで!
設定確認:curl --unix-socket control.unit.sock http://localhost/
設定削除:
curl -X DELETE --unix-socket control.unit.sock 'http://localhost/config/listeners/*:80'
curl -X DELETE --unix-socket control.unit.sock 'http://localhost/config/applications/hello_php'
デモやります
ベンチマークとってみた
ベンチマーク
従来の方式(Nginx+php-fpm、Nginx+wsgi等)とNginx Unit
でどれくらい性能差があるか確認してみた。
ベンチマーク
・実行環境
macbook + vagrant + virtualboxで立てたUbuntu16.04
CPU2コア、メモリ1GB
・同じmac上で立てたCentOS7からApacheBenchを実行
ab -n <総リクエスト数> -c <同時リクエスト数><URL>
実行したコマンド:ab -n 100000 -c 100 http://xxxxxx
見る値:Requests per second(1秒で処理できるリクエスト数)(数値が高い方が好ましい)
Time per request(1リクエストの平均処理時間)(数値が低い方が好ましい)
NginxとNgnix Unitのプロセス数
Nginx : worker_processes 2;
Nginx Unit : "processes": 2
ベンチマークとる組み合わせ
・従来方式(Nginx+php-fpmなど)
・Nginx + Nignx Unit (Nginxをフロントでプロキシ)
・Nginx Unit単体
PHP
従来方式:Nginx + php-fpm
PHPコード
<?php
echo 'PHP Hello,World!';
?>
PHP ベンチ結果
Nginx + php-fpm Nginx + Nginx Unit Nginx Unit
Requests per second 4889.25 [#/sec] 3700.67 [#/sec] 5051.71 [#/sec]
Time per request 0.205 [ms] 0.270 [ms] 0.198 [ms]
Python
従来方式:Nginx + uWSGI
Pythonコード
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Python Hello World"]
Python ベンチ結果
Nginx + uWSGI Nginx + Nginx Unit Nginx Unit
Requests per second 4435.80 [#/sec] 4109.40 [#/sec] 5293.54 [#/sec]
Time per request 0.225 [ms] 0.243 [ms] 0.189 [ms]
Ruby
従来方式:Nginx + unicorn + rails
Rubyコード
require 'rubygems'
require 'sinatra/base'
class HelloApp < Sinatra::Base
get '/hello' do
'Ruby Hello World!'
end
end
run HelloApp
Ruby ベンチ結果
Nginx + unicorn+rails Nginx + Nginx Unit Nginx Unit
Requests per second 2497.55 [#/sec] 2538.71 [#/sec] 3604.32 [#/sec]
Time per request 0.400 [ms] 0.394 [ms] 0.277 [ms]
Go
従来
package main
import (
"fmt"
"net"
"net/http"
"net/http/fcgi"
)
func handler(w http.ResponseWriter, r
*http.Request) {
fmt.Fprintf(w, "Go Hello, World!")
}
func main() {
l, err := net.Listen("tcp", "127.0.0.1:9000")
if err != nil {
return
}
http.HandleFunc("/go", handler)
fcgi.Serve(l, nil)
}
package main
import (
"fmt"
"net/http"
"nginx/unit"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r
*http.Request) {
fmt.Fprintf(w, "Go Hello World")
})
unit.ListenAndServe(":9000", nil)
}
Nginx Unit
Go ベンチ結果
Nginx + go Nginx + Nginx Unit Nginx Unit
Requests per second 2565.68 [#/sec] 3133.33 [#/sec] 5255.99 [#/sec]
Time per request 0.390 [ms] 0.319 [ms] 0.190 [ms]
Requests per second
Time per request
まとめ
・環境構築が簡単。
・複数言語、複数バージョンをテストする環境
を作りたいときにはいいかも。
・性能はそこそこいいみたいだけど、安定性が
気になるところ。

Nginx Unitを試してみた話