View Nginx Cache Hit rate

Source: Internet
Author: User

first, add a hit display on the HTTP header

Nginx provides $upstream_cache_status this variable to show the state of the cache, we can add an HTTP header in the configuration to display this state, to achieve similar squid effect.

 
1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Location / { proxy_redirect off; proxy_set_header host $host;          proxy_set_header         x-Real-ip       $remote_addr; proxy_set_header X-forwarded-for $proxy_add_x_forwarded_f or; proxy_connect_timeout ; proxy_send_timeout ; proxy_read_timeout ; proxy_buffer_size 128k; proxy_buffers 4 128k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; proxy_cache cache; proxy_cache_valid 304 1h; proxy_cache_valid 404 1m; proxy_cache_key $uri$is_args$args; add_header Nginx-Cache "$upstream _cache_status"; proxy_pass http://backend;     }

The header you see through Curl or browser is as follows:

 
1 2 3 4 5 6 7 8 9 HTTP/1.1 OK Date: Mon, April: 02 GMT Server: nginx Content-Type: image/jpeg Content-Length: 23560 last-Modified: Thu, April :05 : GMT Nginx-Cache: hitAccept-Ranges: bytes Vary: User-Agent

$upstream _cache_status contains the following states:

· MISS miss, request is routed to backend
· Hit Cache Hits
· EXPIRED Cache has expired request sent to backend
· UPDATING is updating the cache and will use the old answer
· STALE back end will get an expired reply

second, Nginx cache hit rate statistics

That is, Nginx provides us with the $upstream_cache_status function, which naturally writes the hit state to the log. Specifically, you can define the log format as follows:

 
1 2 3 4 log_format   main   ' $remote _addr-$remote _ User [$time _local] "$request" '                                       < Span class= "Crayon-s" "$http _user_agent" "$http _x_forwarded_for" '                   < Span class= "Crayon-s", "$upstream _cache_status" '

Hit Count method: Divide the number of hits by the total log amount to get the cache hit rate:

 
1 awk ' {if ($NF = = "" Hit ") hit++} END {printf"%.2f% ", Hit/nr} ' access. Log

Once you understand the principle, you can also use the Crontab script to count the daily hits to a log for viewing.

 
1 2 # crontab-l 1 0 * * * /opt /shell/nginx_cache _hit >> /usr/local/ nginx/logs/hit

The contents of the visit script are:

 
1 2 3 4 #!/bin/bashlog_file='/usr/local/nginx/logs/access.log.1 ' Last_day=$(date +%F -D "-1 day") awk ' {if ($NF = = "" Hit "") hit++} END {printf "'$ last_day':%d%d%.2f%n", HIT,NR,HIT/NR } ' $log_file

Transferred from: http://www.361way.com/nginx-cache/2665.html

View Nginx Cache Hit rate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.