Nginx的Web緩存服務與新浪網的開源NCACHE模塊
什麼是web緩存
Web緩存位於內容源web服務器和客戶端之間,當用戶訪問一個 URL時,web緩存服務器回去後端web源服務器取回要輸出的內容,然後,當下一個請求到來時,如果訪問的是相同的URL,web緩存服務器直接輸出內容給客戶端,而不是像源服務器再次發送請求。web緩存降低了內容源web服務器、數據庫的負載,減輕了網絡延遲,提高了用戶的響應速度,增強了用戶體驗。
最著名的還要數Squid Cache,其主要在Unix一類系統運行。
Nginx的Web緩存服務
Nginx從0.7.48後支持類似於Squid的緩存模塊。這個緩存是把URL及相關組合當做key,用md5算法對key進行希哈,得到硬盤上對應的希哈路徑,從而將緩存內容保存在該目錄內。支持任意URL鏈接。同時也支持404/301/302這樣的非200狀態碼。
Nginx的Web緩存服務主要用於proxy_cache相關指令集和fastcgi相關指令集構成,前者用於反向代理時,對後端內容源進行緩存,後者主要用於對FastCDI的動態程序進行緩存。兩者功能基本一樣。
proxy_cache相關指令集
1、proxy_cache指令
語法:proxy_cache zone_name;
默認值:none
使用環境:http,server,location
該指令用於設置那個緩存區將被應用,zone_name的值爲proxy_cache_path指令創建的緩存區明稱。
2、proxy_cache_path指令
語法:proxy_cache_path path[levels=number] keys_zone=zone_name:zone_size[incative=time] [max_size=size];
默認值:none
使用環境:HTTP
eg:
proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one: 500m inactive=1d max_size=30g;
注意該指令只能在http標籤內配置,levels指定該緩存有兩層hash目錄,第一層爲1個字母,第二層爲2個字母,保存文件名類似於/data0/proxy_cache_dir/c/29/fdg35415fg35f4gsdf2g1535gh465h;key_zone參數用來爲緩存區起名,500m指定內存空間大小爲500MB;inactive的1d是如果緩存數據在1天之內沒有被訪問,將被刪除;max_size的30g是指硬盤的緩存空間爲30GB。
3proxy_cache_methods指令
語法:proxy_cache_methods [GET HEAD POST];
默認值:proxy_cache_methods GET HEAD;
使用環境:http,server,location
該指令用於設置用於緩存那些HTTP方法,默認緩存 HTTP GET/HEAD 方法,不緩存HTTP POST方法。
4proxy_cache_min_uses指令
語法:proxy_cache_min_uses the_number;
默認值:proxy_cache_min_uses 1;
使用環境:http,server,location
該指令設置緩存最小的使用次數,默認值是1.
5、proxy_cache_valid指令
語法:proxy_cache_valid reply_code [reply_code…]time;
默認值:none
使用環境:http,server,location
該指令用於對不同的返回狀態碼的URL設置不同的緩存時間,例如:
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
如果不指定狀態嗎,直接指定時間,則只有200、301、302狀態的URL緩存5分鐘。
6、proxy_cache_key指令
語法:proxy_cache_key line;
默認值:none
使用環境:http,server,location
該指令用來設置web緩存的key值,Nginx根據key值md5希哈存儲緩存。一般根據‘$host(域名)、$request_uri(請求路徑)’
等組合變量合成proxy_cache_key.例如:proxy_cache_key "$host:$server_port$uri$is_args$args";
proxy_cache完整示例
su
yum -y install pcre//安裝pcre
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar zxvf ngx_cache_purge-2.3.tar.gz//獲取nginx_cache_purge
cd nginx-1.6.3//進入你的nginx文件目錄(nginx安裝請參考前面的博客)
./configure --user=www --group=www --addmodule=../ngx_cache_purge-2.3 --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module
配置nginx.conf
cd /usr/local/webserver/nginx/conf
#user www www;
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 {
use epoll;
worker_connections 1024;
}
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;
#charset utf-8;
server_name_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
sendfile on;
#tcp_nopush on;
keepalive_timeout 30;
tcp_nodely on;
proxy_temp_path /data0/proxy_temp_path;
proxy_temp_path /data0/proxy_temp_path levels=1:2 key_zone=cache_one:200m inactive=1d max_size=30g;
upstream my_sever_pool{
server 192.168.1.2:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.1.4:80 weight=1 max_fails=2 fail_timeout=30s;
}
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_set_header Host $host;
proxy_set_header X-Forward-For $remote_addr;
proxy_pass http://my_server_pool;
# root html;
#index index.html index.htm;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$
{
#使用web緩存區cache_one
proxy_cache cache_one;
#對不同狀態碼設置不同緩存時間
proxy_cache_valid 200 304 12h;
proxy_cache_valid 301 302 1m;
proxy_cache_valid any im;
#設置web緩存的key值,nginx根據key值md5希哈存儲緩存,這裏根據“域名/URL 參數”組合成key。
proxy_cache_key $host$uri$is_args$args;
#反向代理,訪問後端內容源服務器
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http:my_server_pool;
}
#用於清除緩存,假設一個URL爲http://my.domain.com/text.gif通過訪問http://my.domain.com/purge/test.gif可以清除該URK緩存。
location ~ /purge(/.*)
{
#設定只允許指定的IP或IP段纔可以清除URL緩存。
allow 127.0.0.1
allow 192.168.0.0/16;
deny all;
proxy_cache_purge cache_one $shot$1$is-args$args;
}
access_log 0ff
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}