こんにちは、LinuCエバンジェリストこと、鯨井貴博@opensourcetechです。
今回は、nginx unit-1.11.0のリリース記事でも触れた新機能「static content support」を試してみます。
Docker環境のnginx unitの準備
Dockerでnginx unitを用意します。
まずは、イメージの取得。
REPOSITORY TAG IMAGE ID CREATED SIZE
bash-3.2$ docker pull nginx/unit
Using default tag: latest
latest: Pulling from nginx/unit
8f91359f1fff: Pull complete
47299d66a23f: Pull complete
2b60faa4e81d: Pull complete
Digest: sha256:5d9ae1e9a9353ede5ea9ba3de3d0e235332b631e348ebaf2f4680a951b720cf4
Status: Downloaded newer image for nginx/unit:latest
docker.io/nginx/unit:latest
bash-3.2$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx/unit latest 9ffd0ad66444 2 days ago 216MB
続いて、コンテナの起動します。
8bfa73fd9f7549a110c19546f56a1003f945c3db28b976b443b5caaf12b648c4
bash-3.2$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8bfa73fd9f75 nginx/unit "unitd --no-daemon -…" 6 seconds ago Up 4 seconds 0.0.0.0:8080->80/tcp nervous_ishizaka
nginx unitの設定
コンテナ内に入って、操作を行なっていきます。
当然ですが、まだnginx unitの設定はありません。
root@8bfa73fd9f75:/# curl --unix-socket /var/run/control.unit.sock http://localhost
{
"certificates": {},
"config": {
"listeners": {},
"applications": {}
}
}
今回のstatic content supportの構成ですが、シンプルに以下のようにしました。
-------
ドキュメントルート:/www/data/static/html/
コンテンツファイル:index.html
解放ポート:80/TCP
上記コンテンツを配置します。
root@8bfa73fd9f75:/# ls /www/data/static/html/
root@8bfa73fd9f75:/# echo "nginx unit's Static file function TEST" > index.html
root@8bfa73fd9f75:/# cat index.html
nginx unit's Static file function TEST
続いて、nginx unitの設定ファイル(json)をコンテナを動かしているホスト上で作成し、コンテナ内の/tmpにコピーします。
bash-3.2$ cat static_file.json
{
"listeners": {
"127.0.0.1:80": {
"pass": "routes"
}
},
"routes": [
{
"action": {
"share": "/www/data/static/html/"
}
}
]
}
bash-3.2$ docker cp ./static_file.json 8bfa73fd9f75:/tmp
root@8bfa73fd9f75:/# cd /tmp
root@8bfa73fd9f75:/tmp# ls
static_file.json
jsonファイルの適用。
そして、適用されたか確認を行います。
root@8bfa73fd9f75:/tmp# curl -X PUT -d @/tmp/static_file.json --unix-socket /var/run/control.unit.sock http://localhost/config/
{
"success": "Reconfiguration done."
}
root@8bfa73fd9f75:/tmp# curl --unix-socket /var/run/control.unit.sock http://localhost
{
"certificates": {},
"config": {
"listeners": {
"127.0.0.1:80": {
"pass": "routes"
}
},
"routes": [
{
"action": {
"share": "/www/data/static/html/"
}
}
]
}
}
動作確認
意図した通りになっているか確認します。
コンテナ内でcurlを使って、「http://localhost」として作成したコンテンツファイルの内容が返されるか確認します。
root@8bfa73fd9f75:/tmp# curl http://localhost/
nginx unit's Static file function TEST
root@8bfa73fd9f75:/tmp# curl -vL 127.0.0.1:80/
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1
> User-Agent: curl/7.52.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Last-Modified: Sun, 22 Sep 2019 11:45:54 GMT
< ETag: "5d875ef2-27"
< Content-Type: text/html
< Server: Unit/1.11.0・・・・unit-1.11.0ですね
< Date: Sun, 22 Sep 2019 11:54:32 GMT
< Content-Length: 39
<
nginx unit's Static file function TEST・・・ちゃんとコンテンツが見れてますね
* Curl_http_done: called premature == 0
* Connection #0 to host 127.0.0.1 left intact
おわりに
1.11.0となり更に進化したnginx unit。
Static content supportによって、開発者のIgoreが日本に来た際にも言っていたnginx version2に少し近づきましたね。
また、今回使用したファイルなどGithubに置いてあります。
-------
static_file.json
https://github.com/kujiraitakahiro/docker/blob/master/static_file.json
今回の作業ログ
https://github.com/kujiraitakahiro/docker/blob/master/20190922_nginx_unit_staticfile.log
Dockerのシンプルな使い方とか、nginx unitが初めての方は以下も見るといいかもしれません。
docker 〜Dockerfile を使用した、コンテナの作成・起動〜 - Opensourcetechブログ
nginx unit 1.8.0を使ってみる! - Opensourcetechブログ