ウェブサーバーしたい(nginx
)
1$ docker run -d -p 8082:80 --name my-nginx nginx
2$ docker excec -it my-nginx
3(my-nginx) $ nginx
4nginx version: nginx/1.23.3
5(my-nginx) $ pwd
6/
最近シェアが増えてきているというNginx
の設定方法を確認します。
ここではApacheと同じように、Dockerで起動したNginxサーバーのコンテナを使って、設定内容を調べています。
サーバーを操作したい
1$ nginx -s stop
2$ nginx -s quit
3$ nginx -s reload
4$ nginx -s reopen
設定ファイルを確認したい(nginx.conf
)
1$ find . -name *.conf | grep nginx
2./etc/nginx/nginx.conf
3./etc/nginx/conf.d/default.conf
設定ファイルの名前はnginx.conf
です。
上のコマンドでは拡張子が*.conf
で、nginx
の文字列を含むパスを検索しています。
メインの設定ファイル(/etc/nginx/nginx.conf
)と、追加のファイル(/etc/nginx/conf.d/*.conf
)が見つかりました。
設定ファイルには各種の「ディレクティブ」がすでに書き込まれていて、サーバー設定の確認がができます。 また、このディレクティブを書き換えることで設定を変更できます。
Nginxのディレクティブにはsimple directive
とblock directive
の主に2種類の書き方があります。
そしてディレクティブの中にディレクティブを持つ構造をcontext
と呼びます。
設定ファイルのシンタックス確認(nginx -t
)
1$ nginx -t
2$ nginx -T
設定ファイルのシンタックス(=書き方)が正しいかチェックできます。 設定を書き換えた場合、サーバーを再起動する前には必ずチェックするとよいです。
ポート番号を確認したい(listen
)
server {
listen 80;
listen [::]:80;
server_name localhost;
}
公開用ディレクトリを確認したい(location
)
server {
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
ログフォーマットを確認したい(log_format
)
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
}
Apacheのcombined
ログ形式と同じフォーマットです。