鉴于网站的图片过多,而且大部分都是缩略图,文件又非常小,非常容易造成NFS机的压力很大,很纠结。
刚开始使用过Varnish V2.1.5,上线后,负载很低,但是图片显示时,会出现延迟的情况,经常出现叉烧包的问题,调整过相应的参数,还是无效,最终只能放弃(如有童鞋知道原因,欢迎与我联络,非常感谢)。
Nginx作为强大的Web服务器,我们一直在用,值得依赖。然而对于缓存部分,却了解太少,这次正好使用了它的proxy_cache模块,非常好用,下面简单的介绍一下配置:
一、下载 ngx_cache_purge
ngx_cache_purge模块,用于清除指定 URL的缓存,非常实用。
# wget http://labs.frickle.com/files/ngx_cache_purge-1.3.tar.gz
# tar zxvf ngx_cache_purge-1.3.tar.gz
二、重新编译Nginx(根据实际情况而定)
# cd nginx-0.8.52
# ./configure –prefix=/usr/local/nginx –user=acc –group=acc –add-module=../ngx_cache_purge-1.3 –with-http_ssl_module -
-with-http_sub_module –with-http_dav_module –with-http_flv_module –with-http_gzip_static_module –with-http_stub_status_module –
http-proxy-temp-path=/var/nginx/proxy –http-fastcgi-temp-path=/var/nginx/fastcgi –http-client-body-temp-path=/var/nginx/client –w
ith-debug
# make
# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx-old
# cp objs/nginx /usr/local/nginx/sbin/
三、创建文件夹
# mkdir /tmp/proxy_temp_dir
# mkdir /tmp/proxy_cache_dir
# chmod 777 /tmp/proxy_temp_dir /tmp/proxy_cache_dir
四、修改Nginx配置文件
# vi /usr/local/nginx/conf/nginx.conf
user addcn addcn;
# 8核CPU
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;error_log logs/error.log;
pid logs/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}http {
include mime.types;
default_type application/octet-stream;access_log logs/access.log main;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;sendfile on;
tcp_nopush on;
server_tokens off;keepalive_timeout 60;
fastcgi_intercept_errors on;client_body_buffer_size 512k;
proxy_connect_timeout 10;
proxy_read_timeout 60;
proxy_send_timeout 10;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;# fastcgi
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;# gzip
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
# proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
proxy_temp_path /tmp/proxy_temp_dir;
# 设置Web缓存区名称为cache_one,内存缓存空间大小为1000MB,3天清理一次缓存,硬盘缓存空间大小为100GB。
proxy_cache_path /tmp/proxy_cache_dir levels=1:2 keys_zone=cache_one:1000m inactive=3d max_size=100g;
# 后台请求服务器
upstream backend_server {
server 192.168.1.2:8000 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.1.3:8000 weight=1 max_fails=2 fail_timeout=30s;
}server {
# listen port
listen 8000;server_name img.591rmb.info
charset utf-8;# root
include /usr/local/nginx/conf/module/path_params.conf;
root /var/www/
index index.html index.htm;# blocked
location ~ .*\.(gif|jpg|png|jpeg|bmp|swf)$ {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
proxy_cache_valid 200 304 30d;
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://backend_server;expires max;
access_log off;
}
#用于清除缓存,假设一个URL为http://192.168.8.42/test.txt,通过访问http://192.168.8.42/purge/test.txt就可以清除该URL的缓存
location ~ /purge(/.*)
{
#设置只允许指定的IP或IP段才可以清除URL缓存。
allow 127.0.0.1;
allow 192.168.0.0/16;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
}
五、启动Nginx
# /usr/local/nginx/sbin/nginx -t
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful
# /usr/local/nginx/sbin/nginx
六、参考文献:
使用Nginx的proxy_cache缓存功能取代Squid[原创] ( http://blog.s135.com/nginx_cache )
总结:
Varnish3最新版还没有试用,后续找时间试一下,使用proxy_cache后,图片访问一切正常^-^。
