LoginSignup
106
102

More than 5 years have passed since last update.

nginxのログ出力変更

Posted at

Webサーバ、nginx

nginxのaccess.log。デフォルトだと、POSTされたデータのMessageBodyが出力されない…。
何がPOSTされたのか見たい!ということがあったので、調べてみた。

設定ファイルの場所

/etc/nginx/nginx.conf

ログファイルの場所

/var/log/nginx/access.log

設定ファイル変更!

設定ファイル内のhttpコンテキストを変更します。
設定ファイルの中身は以下のURLを参考にさせていただきました。
参考URL

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    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;
    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  65;
    gzip  on;
    include /etc/nginx/conf.d/*.conf;
}

この中の、
log_formatディレクティブが、ログの出力フォーマットを定義しています。
ので、この定義に、ちょちょいと。

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" "$request_body"';

最後に"$request_body"を追記するのみ。
(ログの最初に出力したい場合は、ディレクティブの先頭に追記する)

nginx再起動

設定を変更した後は、nginxを再起動。

service nginx stop
service nginx start

すると、上記で書いた場所のaccess.logに、POSTされたデータのMessageBodyが出力されるようになります。

106
102
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
106
102