nginx反向代理缓存测试1. 安装openresty作为web服务器1.1 获取安装包# wget http://openresty.org/download/ngx_openresty-1.4.3.6.tar.gz1.2 配置安装# tar zxvf ngx_openresty-1.4.3.6.tar.gz# ./configure --prefix=/data/openresty \--with-luajit \--with-http_iconv_module \--with-http_ssl_module \--with-http_gzip_static_module \--with-http_stub_status_module \--with-http_perl_module --with-debug # gmake && gmake install# vi /data/openresty/nginx/conf/nginx.confuser root root;worker_processes 2;worker_rlimit_nofile 51200;error_log logs/error.log;pid logs/nginx.pid;events { use epoll; worker_connections 50000;}http { access_log logs/access.log; server { listen 8080 default; server_name 127.0.0.1; location = / { content_by_lua 'ngx.say("hello, world")'; } }1.3 启动openresty# /data/openresty/nginx/sbin/nginx -c /data/openresty/nginx/conf/nginx.conf1.4 测试# curl localhost:8080hello, world# tail -f /data/openresty/nginx/logs/access.log127.0.0.1 - - [13/Mar/2014:10:58:52 +0800] "GET / HTTP/1.1" 200 23 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"2. 安装nginx作为反向代理和缓存服务器。2.1 获取安装包# wget http://nginx.org/download/nginx-1.4.6.tar.gz2.1 配置安装# tar zxvf nginx-1.4.6.tar.gz# cd nginx-1.4.6# ./configure --prefix=/data/nginx/ --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --pid-path=/var/run/nginx/nginx.pid --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --user=nginx --group=nginx --with-pcre=/root/pcre-8.32 --with-http_gzip_static_module --with-http_ssl_module --with-http_realip_module# make && make install2.3 修改配置文件# vi /etc/nginx/nginx.confuser root root;worker_processes 2;worker_cpu_affinity 01 10;worker_rlimit_nofile 51200;error_log /var/log/nginx/error.log;pid /var/run/nginx/nginx.pid;events { use epoll; worker_connections 51200;}http { include 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" ' '"$upstream_cache_status"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; gzip on; gzip_disable "MSIE [1-6]\."; gzip_comp_level 9; gzip_min_length 1000; gzip_types text/plain text/css application/x-javascript; fastcgi_connect_timeout 300s; fastcgi_send_timeout 300s; fastcgi_read_timeout 300s; include sites/mytest.conf;}# vi /etc/nginx/sites/mytest.confproxy_temp_path /data/www/mytemp_cache;proxy_cache_path /data/www/mytest_cache levels=1:2 keys_zone=mytest:20m inactive=1h max_size=1000m;#upstream my_cluster {# server localhost:8080;#}server { listen 80; server_name localhost;# resolver 8.8.8.8; proxy_cache_valid 200 304 12h; proxy_cache_valid 500 404 2s; proxy_read_timeout 20s; proxy_send_timeout 20s; proxy_connect_timeout 20s; proxy_cache_key $host$uri$is_args$args; expires 1d; location /test { proxy_ignore_headers X-Accel-Expires Expires Cache-Control; proxy_cache mytest; add_header Nginx-Cache "$upstream_cache_status"; proxy_pass http://localhost:8080/;# proxy_pass http://my_cluster; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_redirect off; }}2.3 启动nginx# nginx2.4 查看页面缓存是否命中页面未缓存# curl -I localhost/testHTTP/1.1 200 OKServer: nginx/1.4.6Date: Thu, 13 Mar 2014 03:24:54 GMTContent-Type: text/plainContent-Length: 13Connection: keep-aliveExpires: Fri, 14 Mar 2014 03:24:54 GMTCache-Control: max-age=86400Nginx-Cache: MISS页面缓存# curl -I localhost/testHTTP/1.1 200 OKServer: nginx/1.4.6Date: Thu, 13 Mar 2014 03:24:56 GMTContent-Type: text/plainContent-Length: 13Connection: keep-aliveExpires: Fri, 14 Mar 2014 03:24:56 GMTCache-Control: max-age=86400Nginx-Cache: HIT查看缓存文件# cat /data/www/mytest_cache/7/79/184e40fe0d5f085da4f1d2cae054e797KEY: localhost/testHTTP/1.1 200 OKServer: ngx_openresty/1.4.3.6Date: Thu, 13 Mar 2014 03:03:28 GMTContent-Type: text/plainContent-Length: 13Connection: closehello, world2.5 查看访问日志# tail -f /var/log/nginx/access.log 127.0.0.1 - - [13/Mar/2014:11:30:42 +0800] "HEAD /test HTTP/1.1" 200 0 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "-" "HIT"统计命中率# awk '{if($NF=="\"HIT\"") hit++} END {printf "%.2f%\n",hit/NR}' /var/log/nginx/access.log 0.04%3. 安装第三方purge模块3.1 获取安装包wget http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz3.2 编译安装# tar zxvf ngx_cache_purge-2.1.tar.gz# cd ngx_cache_purge-2.13.3 重新编译安装# nginx -Vnginx version: nginx/1.4.6built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) TLS SNI support enabledconfigure arguments: --prefix=/data/nginx/ --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --pid-path=/var/run/nginx/nginx.pid --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --user=nginx --group=nginx --with-pcre=/root/pcre-8.32 --with-http_gzip_static_module --with-http_ssl_module --with-http_realip_module加入模块重新编译# ./configure --prefix=/data/nginx/ --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --pid-path=/var/run/nginx/nginx.pid --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --user=nginx --group=nginx --with-pcre=/root/pcre-8.32 --with-http_gzip_static_module --with-http_ssl_module --with-http_realip_module --add-module=/root/ngx_cache_purge-2.1只需要make,不要make install会覆盖原文件。# make替换nginx执行文件# cp /usr/sbin/nginx /usr/sbin/nginxbak# cp -rp /root/nginx-1.4.6/objs/nginx /usr/sbin启动nginx# nginx3.4 修该配置文件# vi /etc/nginx/sites/mytest.conflocation ~ /purge(/.*){ allow 127.0.0.1; deny all; proxy_cache_purge mytest $host$1$is_args$args;}3.5 测试访问缓存命中# curl -I localhost/testHTTP/1.1 200 OKServer: nginx/1.4.6Date: Thu, 13 Mar 2014 03:56:56 GMTContent-Type: text/plainContent-Length: 13Connection: keep-aliveExpires: Fri, 14 Mar 2014 03:56:56 GMTCache-Control: max-age=86400Nginx-Cache: HIT清除缓存# curl localhost/purge/testSuccessful purge Successful purge
Key : localhost/test Path: /data/www/mytest_cache/7/79/184e40fe0d5f085da4f1d2cae054e797
nginx/1.4.6 查看缓存目录# cd /data/www/mytest_cache/7/79/# ls访问缓存未命中# curl -I localhost/testHTTP/1.1 200 OKServer: nginx/1.4.6Date: Thu, 13 Mar 2014 03:57:03 GMTContent-Type: text/plainContent-Length: 13Connection: keep-aliveExpires: Fri, 14 Mar 2014 03:57:03 GMTCache-Control: max-age=86400Nginx-Cache: MISS