Statistics on snginx logs to view nginx cache hit rate
To calculate the nginx cache, make sure that you have configured the nginx cache, and then output the cache status in the log, as shown in the following log format Configuration:
The last entry in the log is the cache status.
Log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request" ''$ status $ response" $ http_referer "'' "$ http_user_agent" "$ http_x_forwarded_for" ''" $ upstream_cache_status "';
Statistical Method: Calculate the cache HIT rate by dividing the number of HIT logs by the total number of logs:
Awk '{if ($ NF = "\" HIT \ "") hit ++} END {printf "%. 2f %", hit/NR}' access. log
The input percentage is similar to: 35.15%.
$ Upstream_cache_status contains the following statuses:
MISS missed, the request is sent to the backend
HIT cache HIT
An EXPIRED cache EXPIRED request is sent to the backend
UPDATING is UPDATING the cache. The old response will be used.
The STALE backend will receive an expired response.
BYPASS cache is bypassed
You can also modify the preceding script to calculate the percentage of other statuses based on these statuses.