Nginx做爲web緩存服務器php
從0.7.48版本開始,Nginx支持相似Squid的緩存功能。Nginx的web緩存服務主要由proxy_cache相關命令集合fastcgi_cache相關命令集構成,前者用於反向代理時對後端內容源服務器進行緩存,後者主要用於對FastCGI的動態程序進行緩存。此外,若是不想使用Nginx自帶的緩存功能,也可以使用第三方模塊ngx_slowfs_cache來實現緩存服務器的配置。html
這裏使用Nginx自帶的緩存模塊,經過proxy命令來實現數據的緩存,因此在編譯的時候要加上ngx_cache_purge模塊,這個第三方模塊是清理Nginx緩存的一個插件。nginx
在網上下載ngx_cache_purge插件的最新版本,而後從新編譯安裝nginx。web
./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-openssl=/usr/ --with-pcre=/usr/local/src/pcre-8.31 --add-module=/usr/local/src/ngx_cache_purge-2.3(文件解壓後存放的位置)後端
Nginx緩存服務器的配置主要經過proxy_cache相關命令來實現。數組
proxy_cache_path /backup/proxy_cache_dir levels=1:2 keys_zone=cache_one:4096m inactive=1d max_size=3g;緩存
proxy_temp_path /backup/proxy_temp_dir;服務器
location / {memcached
root html;ui
index index.html index.htm index.php;
proxy_cache cache_one; #反向代理緩存設置命令,語法爲「proxy_cache zone|off「,默認爲off,須要將proxy_cache命令放在location字段,這樣匹配以此location的url才能被緩存。
proxy_cache_valid 200 304 12h; #對不懂HTTP狀態碼設置不一樣的緩存時間
proxy_cache_key $host$uri$is_args$args; #這個命令是設置以什麼樣的參數獲得緩存的文件名,默認爲」$scheme$proxy_host$request_uri」,表示以協議,主機名,請求uri(包括參數)作MD5得出緩存的文件名。這裏以域名,URI,參數組合成Web緩存的key值,nginx會根據key哈希,存儲緩存內容到二級緩存目錄內
}
下面配置手動清除緩存策略:
location ~ /purge(/.*) {
allow 127.0.0.1;
allow 192.168.1.0/24;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
這裏設置能夠清除緩存的ip和網段,下面說的是清除的內容
location ~ \.php?$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
以.php結尾的文件不用緩存
手動清除緩存的方法
http://192.168.1.120/index.html 訪問
http://192.168.1.120/purge/index.html 清除緩存策略
t
[root@Goun conf]# ps -ef | grep nginx
root 9390 1 0 20:52 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
www 9392 9390 0 20:52 ? 00:00:00 nginx: worker process
www 9393 9390 0 20:52 ? 00:00:00 nginx: cache manager process
進程nginx:cache manager process這個進程是用來管理緩存服務和文件的。