Nginx服务器的缓存相关配置
时间:2015-01-25 20:08 来源:linux.it.net.cn 作者:IT
nginx已经具备了squid的缓存功能,并且可以绑定cpu性能更好。通过ngx_cache_purge模块可以清楚缓存,本文带您一起来学习nginx的缓存配置.
nginx不仅可以缓存 图片 文本借助于proxy_cache模块 还可以缓存动态内容 借助于fastcgi模块来完成
1.准备软件包
wget http://labs.frickle.com/files/ngx_cache_purge-1.2.tar.gz
wget http://nginx.org/download/nginx-0.8.55.tar.gz
yum -y install pcre openssl openssl-devel gcc gcc-c++
2.安装
useradd -s /sbin/nologin nginx
tar zxvf ngx_cache_purge-1.2.tar.gz
tar zxvf nginx-0.8.55.tar.gz
cd nginx-0.8.55
./configure
3.配置
user nginx nginx;
worker_processes 8; 一般情况根据你的逻辑cpu个数来定
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/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;
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;
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;
#开启gzip压缩
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 /data/proxy_temp_dir;
#设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
proxy_cache_path /data/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
upstream backend_server {
server 192.168.0.251:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.0.252:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.0.253:80 weight=1 max_fails=2 fail_timeout=30s;
}
server
{
listen 80;
server_name 192.168.0.20;
index index.html index.htm;
root /data/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.0.20/test.txt,通过访问http://192.168.0.20/purge/test.txt就可以清除该URL的缓存。
location ~ /purge(/.*)
{
#设置只允许指定的IP或IP段才可以清除URL缓存。
allow 127.0.0.1;
allow 192.168.0.0/24;
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;
}
}
4.相关指令的解释
官网上有,我真不想直接copy来copy去 比较不好 下次我一起copy总结下 放我博客里
5.测试
未缓存前(每次测试我都清楚了浏览器自身的缓存)

缓存后

从图中可以明显看出接收时间大大缩小了。
由此可证明缓存达到效果了
6.展望
目前只能单个删除缓存页面 无法批量删除正在寻求好的方法、
(责任编辑:IT)
nginx已经具备了squid的缓存功能,并且可以绑定cpu性能更好。通过ngx_cache_purge模块可以清楚缓存,本文带您一起来学习nginx的缓存配置.
nginx不仅可以缓存 图片 文本借助于proxy_cache模块 还可以缓存动态内容 借助于fastcgi模块来完成 1.准备软件包 wget http://labs.frickle.com/files/ngx_cache_purge-1.2.tar.gz wget http://nginx.org/download/nginx-0.8.55.tar.gz yum -y install pcre openssl openssl-devel gcc gcc-c++ 2.安装 useradd -s /sbin/nologin nginx tar zxvf ngx_cache_purge-1.2.tar.gz tar zxvf nginx-0.8.55.tar.gz cd nginx-0.8.55 ./configure 3.配置 user nginx nginx; worker_processes 8; 一般情况根据你的逻辑cpu个数来定 error_log /usr/local/nginx/logs/nginx_error.log crit; pid /usr/local/nginx/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; 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; 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; #开启gzip压缩 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 /data/proxy_temp_dir; #设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。 proxy_cache_path /data/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g; upstream backend_server { server 192.168.0.251:80 weight=1 max_fails=2 fail_timeout=30s; server 192.168.0.252:80 weight=1 max_fails=2 fail_timeout=30s; server 192.168.0.253:80 weight=1 max_fails=2 fail_timeout=30s; } server { listen 80; server_name 192.168.0.20; index index.html index.htm; root /data/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.0.20/test.txt,通过访问http://192.168.0.20/purge/test.txt就可以清除该URL的缓存。 location ~ /purge(/.*) { #设置只允许指定的IP或IP段才可以清除URL缓存。 allow 127.0.0.1; allow 192.168.0.0/24; 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; } } 4.相关指令的解释 官网上有,我真不想直接copy来copy去 比较不好 下次我一起copy总结下 放我博客里 5.测试 未缓存前(每次测试我都清楚了浏览器自身的缓存)
缓存后
从图中可以明显看出接收时间大大缩小了。 由此可证明缓存达到效果了 6.展望 目前只能单个删除缓存页面 无法批量删除正在寻求好的方法、 (责任编辑:IT) |