Nginx Cache測試

nginx反向代理緩存測試

1. 安裝openresty做爲web服務器
1.1 獲取安裝包
# wget  http://openresty.org/download/ngx_openresty-1.4.3.6.tar.gz
1.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.conf
user 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.conf
1.4 測試
# curl localhost:8080
hello, world
# tail -f /data/openresty/nginx/logs/access.log
127.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.gz
2.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 install
2.3 修改配置文件
# vi /etc/nginx/nginx.conf
user 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.conf
proxy_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
# nginx
2.4 查看頁面緩存是否命中
頁面未緩存
# curl -I localhost/test
HTTP/1.1 200 OK
Server: nginx/1.4.6
Date: Thu, 13 Mar 2014 03:24:54 GMT
Content-Type: text/plain
Content-Length: 13
Connection: keep-alive
Expires: Fri, 14 Mar 2014 03:24:54 GMT
Cache-Control: max-age=86400
Nginx-Cache: MISS
頁面緩存
# curl -I localhost/test
HTTP/1.1 200 OK
Server: nginx/1.4.6
Date: Thu, 13 Mar 2014 03:24:56 GMT
Content-Type: text/plain
Content-Length: 13
Connection: keep-alive
Expires: Fri, 14 Mar 2014 03:24:56 GMT
Cache-Control: max-age=86400
Nginx-Cache: HIT
查看緩存文件
# cat /data/www/mytest_cache/7/79/184e40fe0d5f085da4f1d2cae054e797
KEY: localhost/test
HTTP/1.1 200 OK
Server: ngx_openresty/1.4.3.6
Date: Thu, 13 Mar 2014 03:03:28 GMT
Content-Type: text/plain
Content-Length: 13
Connection: close
hello, world
2.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.gz
3.2 編譯安裝
# tar zxvf ngx_cache_purge-2.1.tar.gz
# cd ngx_cache_purge-2.1
3.3 從新編譯安裝
# nginx -V
nginx version: nginx/1.4.6
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) 
TLS SNI support enabled
configure 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
# nginx
3.4 修該配置文件
# vi /etc/nginx/sites/mytest.conf
location ~ /purge(/.*)
{
    allow 127.0.0.1;
    deny all;
    proxy_cache_purge mytest $host$1$is_args$args;
}
3.5 測試
訪問緩存命中
# curl -I localhost/test
HTTP/1.1 200 OK
Server: nginx/1.4.6
Date: Thu, 13 Mar 2014 03:56:56 GMT
Content-Type: text/plain
Content-Length: 13
Connection: keep-alive
Expires: Fri, 14 Mar 2014 03:56:56 GMT
Cache-Control: max-age=86400
Nginx-Cache: HIT
清除緩存
# curl localhost/purge/test
<html>
<head><title>Successful purge</title></head>
<body bgcolor="white">
<center><h1>Successful purge</h1>
<br>Key : localhost/test
<br>Path: /data/www/mytest_cache/7/79/184e40fe0d5f085da4f1d2cae054e797
</center>
<hr><center>nginx/1.4.6</center>
</body>
</html>
查看緩存目錄
# cd /data/www/mytest_cache/7/79/
# ls
訪問緩存未命中
# curl -I localhost/test
HTTP/1.1 200 OK
Server: nginx/1.4.6
Date: Thu, 13 Mar 2014 03:57:03 GMT
Content-Type: text/plain
Content-Length: 13
Connection: keep-alive
Expires: Fri, 14 Mar 2014 03:57:03 GMT
Cache-Control: max-age=86400
Nginx-Cache: MISS
相關文章
相關標籤/搜索