LoginSignup
2
2

More than 5 years have passed since last update.

NGINX Unit触ってみた

Last updated at Posted at 2017-09-08

面白そうだったから触ってみた

目次

  • 環境
  • 触ってみる
  • 自分で設定してみる
  • nginxと繋ぎこむ
  • その他
  • curlで設定変えてみたり(20170909追記)

環境

  • OS: CentOS Linux release 7.3.1611 (Core)

触ってみる

リポジトリ設定

/etc/yum.repos.d/unit.repo
[unit]
name=unit repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1

インストール

# yum install unit

とりあえず動かしてみる

パッケージ入れた時に↓みたいなのが出てくるので一度叩いてみて動作確認すると良さげ

----------------------------------------------------------------------

Thank you for installing NGINX Unit!

You may want to load demo configuration and check a couple of apps:

# service unitd start
# service unitd restoreconfig /usr/share/doc/unit/examples/example.config
# curl http://localhost:8300/
# curl http://localhost:8400/

Online documentation is available at http://unit.nginx.org/

----------------------------------------------------------------------

自分で設定してみる

unitの設定

/etc/unit/sample.conf
{
    "listeners": {
        "*:8300": {
            "application": "php-sample"
        },
        "*:8400": {
            "application": "go-sample"
        },
        "*:8500": {
            "application": "python-sample"
        }
    },
    "applications": {
        "php-sample": {
            "type": "php",
            "workers": 20,
            "root": "/var/www/php-app",
            "index": "sample.php"
        },
        "go-sample": {
            "type": "go",
            "executable": "/var/www/go-app/sample",
        },
        "python-sample": {
            "type": "python",
            "workers": 20,
            "path": "/var/www/python-app",
            "module": "sample"
        }
    }
}

適当なphp

/var/www/php-app/index.php
<?php
echo "Nginx-Unit PHP Test\n";

適当なgolang(うまく動かなかったので勉強しときます)

/var/www/go-app/sample.go

適当なpython

/var/www/python-app/sample.py
def application(environ, start_response):
    output = "Nginx-Unit Python Test\n"
    start_response('200 OK', [('Content-type', 'text/plain')])
    return [output]

unitd動かして設定を反映させる

# service unitd start
# service unitd restoreconfig /etc/unit/sample.config

動作確認

# curl localhost:8300
Nginx-Unit PHP Test

# curl localhost:8400
Read header timeout

# curl localhost:8500
Nginx-Unit Python Test

nginxと繋ぎこむ

普通にリバプロするだけなので分かる人は読み飛ばして下さい

http内にupstream追加

/etc/nginx/nginx.conf
http {

    upstream unit_php {
        server 127.0.0.1:8300;
    }
}

virtualhost設定

/etc/nginx/conf.d/sample.conf
server {
    listen 80;
    server_name localhost;

    location / {
        proxy_pass http://unit_php/;
        proxy_set_header Host $host;
    }
}

動作確認

# curl localhost
Nginx-Unit PHP Test

その他

設定のお手軽感ある分、細かい設定項目少なかったので、これからのバージョンアップが楽しみ
あと今回はやってないけどcurlでも設定とかできそうだったのでそのうち試したい
golangは別途勉強しておきます

以下追記(20170909

curlで設定変えてみたり

適当なPHPを用意

<?php
echo "hogehoge\n";

動作確認

# curl localhost
Nginx-Unit PHP Test

# curl -X PUT -d '"sample2.php"' --unix-socket /var/run/control.unit.sock http://localhost/applications/php-sample/index
{
        "success": "Reconfiguration done."
}

# curl localhost
hogehoge

リモートで設定変えたり

nginxにAPI用confを追加

server {
    listen 8080;

    location / {
        proxy_pass http://unix:/var/run/control.unit.sock;
    }
}

※テストなのでこの設定にしたけど本来であればドキュメントにもあるように
 クライアント証明書だったりいれないとセキュリティ的にやばめ

socketファイルの権限変更

chmod 666 /var/run/control.unit.sock

※この方法は最高にイケてないのでphp-fpmlisten.user的なものが欲しい
 unitdそのもののconf見かけてないけどどっかにないかな…?

動作確認(どこかしら別の環境から)

# curl http://xxx.xxx.xxx.xxx/
hogehoge

λ curl h-X PUT -d '"python-sample"' http://210.129.18.150:8080/listeners/*:8300/application
{                                                                                          
        "success": "Reconfiguration done."                                                 
}                                                                                          

# curl http://xxx.xxx.xxx.xxx/
Nginx-Unit Python Test
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2