upstream imooc { server 192.168.11.135:8001; server 192.168.11.135:8002; server 192.168.11.135:8003; } proxy_cache_path /etc/nginx/cache levels=1:2 keys_zone=imooc_cache:10m max_size=10g inactive=60m use_temp_path=off; server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { proxy_cache imooc_cache; proxy_pass http://imooc; proxy_cache_valid 200 304 12h; proxy_cache_valid any 10m; proxy_cache_key $host$uri$is_args$args; add_header Nginx-Cache "$upstream_cache_status"; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; } }
server { listen 8001; server_name 127.0.0.1; location / { root /home/testzq/app/code1; index index.html; } }# /home/testzq/app/code1/index.html
第二台: # /etc/nginx/conf.d/server2.confserver 1 server 1
server { listen 8002; server_name 127.0.0.1; location / { root /home/testzq/app/code2; index index.html; } }# /home/testzq/app/code2/index.html
第三台: # /etc/nginx/conf.d/server3.confserver 2 server 2
server { listen 8003; server_name 127.0.0.1; location / { root /home/testzq/app/code3; index index.html; } }# /home/testzq/app/code3/index.html
重启nginx:server 3 server 3
nginx -t -c /etc/nginx/nginx.conf # 测试配置文件语法 nginx -s reload -c /etc/nginx/nginx.conf # 重新加载配置项>>>先在代理服务器中将缓存关闭 (proxy_cache off),刷新页面,发现页面可以在三个站点间轮询显示: >>>然后在把代理缓存打开,发现页面不在轮询了,请求头多了缓存头(这头是我们配置的): 同时也会在我们配置的缓存目录( /etc/nginx/cache)生成缓存目录: 缓存的内容如下:
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" ' '"$upstream_cache_status"';2、然后配置缓存代理的 access_log 的路径 3、然后使用linux 的awk 命分析日志
awk '{if($NF=="\"HIT\""){hit++}}END{printf "%.2f", hit/NR}' /var/log/nginx/proxy_cache_access.log
命令解释: