LoginSignup
16
20

More than 5 years have passed since last update.

Nginxで何が起こってるんや...

Posted at

ってときにデバッグする方法

設定ファイルの/1つで挙動が大きく異なったりするため
禿げそうになることがある。そんな時にnginxの動作が見れると嬉しいね

想定環境

OS: CentOS7

nginxのインストール

yum install -y nginx

サービスの自動起動設定・起動

ここでnginxではなくnginx-debugというサービスを使うのが最初のつまづきポイント
nginxをインストールすると付いてくる。動作はnginxサービスと同様。

systemctl enable nginx-debug && systemctl start nginx-debug

ログ書き出しの設定

やることは2点
rewriteログの有効化(これがないとデバッグ厳し目
ログレベルの変更

元のファイルからの変更箇所は下記2行だけのはず

error_log   /var/log/nginx/example-error.log debug;
rewrite_log on;

変更後のファイルも併記しておく

変更後の/etc/nginx/nginx.conf
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

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;
    error_log   /var/log/nginx/example-error.log debug;
    rewrite_log on;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

terminalでログをみてみる

鬼のようにログが流れることが確認できるであろう。

tail -f  /var/log/nginx/example-error.log 

ログの見方

初めは見る気すら失せるが、HTTPリクエストごとに下記のような
レスポンスヘッダが現れるので、そこを1区切りと考えれば良い

Server: nginx/1.15.2
Date: Sun, 02 Sep 2018 19:00:38 GMT
Content-Type: text/html
Content-Length: 185
Connection: close
Location: https://localhost/

設定ファイルについても書けたらいいな(

16
20
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
16
20