LoginSignup
8

More than 5 years have passed since last update.

nginx の proxy_cache で、どれ位Hitしているかログに出した!

Last updated at Posted at 2016-02-17

何だかんだと苦労して設定した Nginx の proxy_cache なんだけど、どれぐらい有効なのでしょうか!? いままで、よく判っていなかったのでログに出して集計できるようにした!

Nginx の proxy_cache 設定について

ググって、頑張って設定して下さい。

Nginx のログ出力設定

Nginx の proxy_cache で、どれぐらいヒットしたかは、

$upstream_cache_status

で判ります。

nginx.conf にこんな感じで設定しましょう!

nginx.conf
log_format rt_cache '$remote_addr - $upstream_cache_status [$time_local]  '
                    '"$host" "$request" $status $body_bytes_sent '
                    '"$http_referer" "$http_user_agent"';

自分は、サブドメインサイトが複数あるので、"$host" を埋め込みました。単一サイトor単一ログを出すなら、要らないかも。

ログファイルの設定は、こんな感じにします。

nginx.conf
access_log   /var/log/nginx/example.com.access.log;
access_log   /var/log/nginx/example.com.cache.log rt_cache;

集計しましょう!

作成された example.com.cache.log を集計します。

awk '{print $3}' /var/log/nginx/example.com.cache.log  | sort | uniq -c | sort -r

結果は、こんな感じに出力されます。

8800 HIT
 779 -
1392 BYPASS
  19 EXPIRED
2614 MISS

ダッシュ("-")は、モジュールを上流側に到達しないことを意味します。最も可能性が高いというように、 403を返す404を返している事をを意味します。

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
8