nginx cache 配置
nginx cache 配置
Nginx從0.7.48版本開始,支持了類似Squid的緩存功能。這個緩存是把URL及相關組合當作Key,用md5編碼哈希后保存在硬盤上,所以它 可以支持任意URL鏈接,同時也支持404/301/302這樣的非200狀態碼。雖然目前官方的Nginx Web緩存服務只能為指定URL或狀態碼設置過期時間,不支持類似Squid的PURGE指令,手動清除指定緩存頁面,但是,通過一個第三方的Nginx 模塊,可以清除指定URL的緩存。?IT網,http://www.it.net.cn
Nginx的Web緩存服務主要由proxy_cache相關指令集和fastcgi_cache 相關指令集構成,前者用于反向代理時,對后端內容源服務器進行緩存,后者主要用于對FastCGI的動態程序進行緩存。兩者的功能基本上一樣。
最新的Nginx 0.8.32版本,proxy_cache和fastcgi_cache已經比較完善,加上第三方的ngx_cache_purge模塊(用于清除指定 URL的緩存),已經可以完全取代Squid。我們已經在生產環境使用了 Nginx 的 proxy_cache 緩存功能超過兩個月,十分穩定,速度不遜于 Squid。
在功能上,Nginx已經具備Squid所擁有的Web緩存加速功能、清除 指定URL緩存的功能。而在性能上,Nginx對多核CPU的利用,勝過Squid不少。另外,在反向代理、負載均衡、健康檢查、后端服務器故障轉移、 Rewrite重寫、易用性上,Nginx也比Squid強大得多。這使得一臺Nginx可以同時作為“負載均衡服務器”與“Web緩存服務器”來使用。
1、? Nginx 負載均衡與緩存服務器在 Linux 下的編譯安裝:
??Linux學習,http:// linux.it.net.c
ulimit -SHn 65535
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.00.tar.gz
tar zxvf pcre-8.00.tar.gz
cd pcre-8.00/
./configure
make && make install
cd ../
wget http://labs.frickle.com/files/ngx_cache_purge-1.0.tar.gz
tar zxvf ngx_cache_purge-1.0.tar.gz
wget http://nginx.org/download/nginx-0.8.32.tar.gz
tar zxvf nginx-0.8.32.tar.gz
cd nginx-0.8.32/
./configure --user=www --group=www --add-module=../ngx_cache_purge-1.0 --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
cd ../?IT網,http://www.it.net.cn
2、/usr/local/nginx/conf/nginx.conf 配置文件內容如下:
??IT網,http://www.it.net.cn
user? www www;
worker_processes 8;
error_log? /usr/local/nginx/logs/nginx_error.log? crit;?IT網,http://www.it.net.cn
pid??????? /usr/local/nginx/logs/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
? use epoll;
? worker_connections 65535;
}
http
{
? include?????? mime.types;
? default_type? application/octet-stream;?Linux學習,http:// linux.it.net.cn
? #charset? utf-8;
??????
? server_names_hash_bucket_size 128;
? client_header_buffer_size 32k;
? large_client_header_buffers 4 32k;
? client_max_body_size 300m;
??????
? sendfile on;
? tcp_nopush???? on;
? keepalive_timeout 60;?IT網,http://www.it.net.cn
? tcp_nodelay on;
? client_body_buffer_size? 512k;
? proxy_connect_timeout??? 5;
? proxy_read_timeout?????? 60;
? proxy_send_timeout?????? 5;
? proxy_buffer_size??????? 16k;
? proxy_buffers??????????? 4 64k;
? proxy_busy_buffers_size 128k;
? proxy_temp_file_write_size 128k;?IT網,http://www.it.net.cn
? gzip on;
? gzip_min_length? 1k;
? gzip_buffers???? 4 16k;
? gzip_http_version 1.1;
? 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?? /data0/proxy_temp_dir;
? #設置Web緩存區名稱為cache_one,內存緩存空間大小為200MB,1天沒有被訪 問的內容自動清除,硬盤緩存空間大小為30GB。
? proxy_cache_path? /data0/proxy_cache_dir? levels=1:2?? keys_zone=cache_one:200m inactive=1d max_size=30g;
??
? upstream backend_server {
??? server?? 192.168.8.43:80 weight=1 max_fails=2 fail_timeout=30s;
??? server?? 192.168.8.44:80 weight=1 max_fails=2 fail_timeout=30s;
??? server?? 192.168.8.45:80 weight=1 max_fails=2 fail_timeout=30s;
? }
? server
? {
??? listen?????? 80;
??? server_name? www.it.net.cn 192.168.8.42;
??? index index.html index.htm;
??? root? /data0/htdocs/www;?
??? location /
??? {
???????? #如果后端的服務器返回502、504、執行超時等錯誤,自動將請求轉發到upstream負載均衡池中的另一臺服務器,實現故障轉移。
???????? proxy_next_upstream http_502 http_504 error timeout invalid_header;
???????? proxy_cache cache_one;
???????? #對不同的HTTP狀態碼設置不同的緩存時間
???????? proxy_cache_valid? 200 304 12h;
???????? #以域名、URI、參數組合成Web緩存的Key值,Nginx根據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://backend_server;
???????? expires????? 1d;
??? }
????
??? # 用于清除緩存,假設一個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;
??? }???
??? # 擴展名以.php、.jsp、.cgi結尾的動態應用程序不緩存。
??? location ~ .*\.(php|jsp|cgi)?$
??? {
???????? proxy_set_header Host? $host;
???????? proxy_set_header X-Forwarded-For? $remote_addr;
???????? proxy_pass http://backend_server;
??? }
??? access_log? off;
? }
}?Linux學習,http:// linux.it.net.cn
總結
以上是生活随笔為你收集整理的nginx cache 配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 司法再冻结什么意思
- 下一篇: IOS7实现扫描二维码