nginx設定のサンプルを記載します。
設定ファイル配置場所
/etc/nginx/sites-enabled/xxxxxx.conf
※ xxxxxxはドメイン名とする。
LB配下にnginxサーバを配置する場合
アクセスログがすべてLBのIPになるため、下記を設定ファイル内に記載する。
set_real_ip_from xx.xx.xx.xx/xx; ← LBのIP
real_ip_header X-Forwarded-For;
アクセスログ
/var/log/nginx/xxxxx/access.log ltsv
※ xxxxxxはドメイン名とする。
※ ltsv形式にする。
ログフォーマットは下記とする。
---
log_format ltsv "time:$time_local" : アクセス日時
"\thost:$remote_addr" : クライアントのIPアドレス
"\tforwardedfor:$http_x_forwarded_for" : アクセス元のIP
"\treq:$request" : httpの要求URI
"\tstatus:$status" : httpの要求URI
"\tsize:$body_bytes_sent" : 送信バイト数
"\treferer:$http_referer" : リファラーURL(遷移元URL)
"\tua:$http_user_agent" : ユーザエージェント情報(ブラウザ名・バージョン等)
"\treqtime:$request_time" : 要求処理時間(ミリ秒)最後のバイトがクライアントに送信された後、 "\tcache:$upstream_http_x_cache" 最初のバイトの間の経過時間がクライアントから読み取られ、ログの書き込み
"\truntime:$upstream_http_x_runtime"
"\tvhost:$host";
---
ログローテート設定
vi /etc/logrotate.d/nginx
---
# LB用のアクセスログ
/var/log/nginx/*.log {
daily
missingok
rotate 30
ifempty
compress
delaycompress
create 0640 www-data adm
dateext
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}
#各サービス用のアクセスログ
/var/log/nginx/*/*.log {
daily
missingok
rotate 30
ifempty
compress
delaycompress
create 0640 www-data adm
dateext
sharedscripts
prerotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}
---
ドキュメントルート
/var/www/xxxxx/current/zzzzzz/public/
※ xxxxxxはドメイン名とする。zzzzzzはサービス名など。
レスポンスヘッダーにバージョン番号を表示させない。
/etc/nginx/nginx.conf
server_tokens off; ← #を外す
LBのヘルスチェックは下記を利用。
LBからのヘルスチェックは、各案件ログとは別に記録されるように設定を行う。
ドキュメントルート直下にヘルスチェック用のファイル「healthcheck.php」を準備し、LBのヘルスチェックは、/healthcheck.phpに対して行う。
nginxの設定ファイルに下記を記載して、LBからのヘルスチェックのアクセスログは、/var/log/nginx/access.logに記載されるようにする。
location = /healthcheck.php {
empty_gif;
access_log /var/log/nginx/access.log;
break;
}
phpが停止したときに、LBから切り離しされるようにhealthcheck.phpのファイルの中身は下記を記載する。
<?php {print 'healthcheck';}?>