nginx cache试用

nginx cache现在已经在0.8以上的版本默认启用了,其实是作为proxy部分的一部分。而且现在nginx cache已经支持expires, gzip,内存cache管理等等,已经可以作为线上可以使用了。

使用nginx cache一直是有这个想法的,因为squid实在是太过臃肿了,而连接数到达400已经很吃力了,而nginx的优势恰恰是在连接数上。下面就开始试用吧。

首先是安装,我还特地下载了一个purge工具,专门用来进行推送,但是还是要单独安装squidclient。

./configure --prefix=/usr/local/nginx --with-http_realip_module
--with-http_sub_module --with-http_gzip_static_module
--with-http_stub_status_module --with-file-aio
--add-module=/home/timo/download/ngx_cache_purge-1.2
make
make install

下面这个是我自己的一个Nginx.conf文件,用来做最简单的cache功能

user www-data;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;

#pid logs/nginx.pid;

events {
worker_connections 1024;
use	epoll;
}

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"';

access_log logs/access.log main;

sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;

gzip on;
gzip_http_version 1.0;
gzip_comp_level 9;
gzip_proxied any;
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
#gzip_min_length 1100;
gzip_buffers 16 8k;
#gzip_disable "MSIE [1-6]\.(?!.*SV1)";

proxy_cache_path /var/www/nginx_cache levels=1:2
keys_zone=cache_one:200m inactive=1d max_size=200m;
# proxy_temp_path /var/www/nginx_cache/temp;

server {
listen 10.1.41.81:81;
# expires 2d;

location / {
proxy_cache cache_one;
# proxy_cache_valid 200 301 302 304 30d;
proxy_cache_key $host$uri;
proxy_hide_header Vary;
proxy_set_header Host $host;
proxy_set_header Accept-Encoding '';
proxy_set_header X-Forwarded-For $remote_addr; add_header X-Cache "HIT from cache_test";
proxy_pass http://10.1.41.81;
if ( $request_method = "PURGE" ) {
rewrite ^(.*)$ /purge$1 last;
}
}

location ~ /purge(/.*) {
allow 10.0.0.0/8;
deny all;
error_page 405 =200 /purge$1;
proxy_cache_purge cache_one $host$1$is_args$args;
}
}

}

现在对proxy cache进行下说明:

proxy_cache_path /var/www/nginx_cache levels=1:2
keys_zone=cache_one:200m inactive=1d max_size=200m;

levels1:2表示有2层目录,第一层是一个字母或数字,第二层为2个字母或数字
keys_zones=cache_one:200m 其中cache_one为自己定义的名字,200m为这个cache目录的大小 inactive=1d 表示当这个文件在一天没有请求后会进行清理
max_size=200m 能够缓存的最大空间为200m

proxy_cache_valid 200 301 302 304 30d;
这个看wiki按我的理解是200,301,302,304这些Http code可以缓存有效30天,也可以把这些http code替换成any, 但是这样会导致404,502,503也会被缓存。

add_header X-Cache “HIT from cache_test”;
这个是在http头中加入自定义的头,比如像我加如一个跟squid一样的Hit头

if purge这个部分是直接抄的http://sudone.com/nginx/nginx_cache_purge.html 这个为了做推送用的。

推送的方法就是参考ayou的

squidclient -h192.168.0.123 -p80 -mPURGE -v http://www.sudone.com/index.html 

###########################################

Best regards
Timo Seven
blog: http://www.timoseven.com
twitter: http://twitter.com/zauc
Linux System Admin & MySQL DBA

关于 Timo
XNIX SA & MYSQL DBA

2 Responses to nginx cache试用

  1. anonymous说道:

    “max_size=200m 能够缓存的最大文件为200m”
    关于max_size
    http://wiki.nginx.org/HttpProxyModule#proxy_cache_path
    A special process, called “cache manager”, is created to control the on-disk cache. It is responsible for removing inactive items and enforcing the size of the cache, as defined by the parameter max_size. When the total size of the cache exceeds the maximum size set by max_size, the least recently used data in the cache is deleted to make room for a new cache entry (a LRU replacement policy).

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: