鑒于網(wǎng)站的圖片過多,而且大部分都是縮略圖,文件又非常小,非常容易造成NFS機(jī)的壓力很大,很糾結(jié)。
剛開始使用過Varnish V2.1.5,上線后,負(fù)載很低,但是圖片顯示時,會出現(xiàn)延遲的情況,經(jīng)常出現(xiàn)叉燒包的問題,調(diào)整過相應(yīng)的參數(shù),還是無效,最終只能放棄(如有童鞋知道原因,歡迎與我聯(lián)絡(luò),非常感謝)。
Nginx作為強(qiáng)大的Web服務(wù)器,我們一直在用,值得依賴。然而對于緩存部分,卻了解太少,這次正好使用了它的proxy_cache模塊,非常好用,下面簡單的介紹一下配置:
一、下載 ngx_cache_purge
ngx_cache_purge模塊,用于清除指定 URL的緩存,非常實(shí)用。
# wget http://labs.frickle.com/files/ngx_cache_purge-1.3.tar.gz
# tar zxvf ngx_cache_purge-1.3.tar.gz
二、重新編譯Nginx(根據(jù)實(shí)際情況而定)
# 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/
三、創(chuàng)建文件夾
# 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指定的路徑必須在同一分區(qū)
proxy_temp_path /tmp/proxy_temp_dir;
# 設(shè)置Web緩存區(qū)名稱為cache_one,內(nèi)存緩存空間大小為1000MB,3天清理一次緩存,硬盤緩存空間大小為100GB。
proxy_cache_path /tmp/proxy_cache_dir levels=1:2 keys_zone=cache_one:1000m inactive=3d max_size=100g;
# 后臺請求服務(wù)器
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;
}
#用于清除緩存,假設(shè)一個URL為http://192.168.8.42/test.txt,通過訪問http://192.168.8.42/purge/test.txt就可以清除該URL的緩存
location ~ /purge(/.*)
{
#設(shè)置只允許指定的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
六、參考文獻(xiàn):
使用Nginx的proxy_cache緩存功能取代Squid[原創(chuàng)] ( http://blog.s135.com/nginx_cache )
總結(jié):
Varnish3最新版還沒有試用,后續(xù)找時間試一下,使用proxy_cache后,圖片訪問一切正常^-^。