Nginx从0.7.48版本开始,支持了类似Squid的缓存功能。这个缓存是把URL及相关组合当作Key,用md5编码哈希后保存在硬盘上,所以它可以支持任意URL链接,同时也支持404/301/302这样的非200状态码。虽然目前官方的Nginx Web缓存服务只能为指定URL或状态码设置过期时间,不支持类似Squid的PURGE指令,手动清除指定缓存页面,但是,通过一个第三方的Nginx模块,可以清除指定URL的缓存。
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缓存服务器”来使用。
目的: 缓解后端应用服务器访问压力
简介: 使用Nginx的proxy_cache缓存静态文件(包括图片、css、js),利用nginx第三方模块ngx_cache_purge清除缓存,PURGE指令手动清除指定URL的缓存页面。
步骤:
添加了第三方模块,用于清除缓存,需要重新编译nginx;
wget http://labs.frickle.com/files/ngx_cache_purge-1.3.tar.gz
tar zxvf ngx_cache_purge-1.0.tar.gz
wget http://nginx.org/download/nginx-1.0.0.tar.gz
tar zxvf nginx-1.0.0.tar.gz
cd nginx-1.0.0/
./configure --user=nobody --group=nobody --add-module=../ngx_cache_purge-1.3 --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
需要重新修改nginx的配置文件
下面贴一份完整的nginx.conf配置文件
user nobody nobody;
worker_processes 8;
error_log /data/logs/error.log crit;
pid /usr/local/webserver/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;
server_tokens off;
#charset gb2312;
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;
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 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;
#limit_zone crawler $binary_remote_addr 10m;
#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
proxy_temp_path /data/proxy_temp_dir;
#设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为3GB。
proxy_cache_path /data/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=3g;
upstream img {
server 192.168.1.181 weight=4 max_fails=2 fail_timeout=10s;
# server 192.168.1.163 weight=4 max_fails=2 fail_timeout=10s;
}
server
{
listen 80;
server_name arezone.cn *.arezone.cn;
#index index.html index.htm index.php;
#root /data/htdocs/www;
location ~* .(jpg|jpeg|gif|png|ico|swf|bmp|css|js)$ {
proxy_pass http://img;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header X-Forwarder-For $remote_addr;
access_log off;
# expires 30d;
proxy_cache cache_one;
proxy_cache_valid 200 304 12h;
proxy_cache_valid 301 3d;
proxy_cache_valid any 1m;
proxy_cache_key $host$uri$is_args$args;
if ($http_Cache_Control = "no-cache") { //响应Ctrl+F5清除缓存
rewrite ^(.*)$ /purge$1 last;
}
add_header X-Cache "HIT_olinux"; //标识内容是否缓存
#用于清除缓存,假设一个URL为http://img4.tiboo.cn/css.css,通过访问http://img4.tiboo.cn/purge/css.css就可以清除该URL的缓存。
location ~ /purge(/.*)
{
allow 192.168.1.155;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
}
location /
{
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://img;
proxy_cache cache_one;
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header X-Forwarder-For $remote_addr;
expires 10m;
if ($http_Cache_Control = "no-cache") {
rewrite ^(.*)$ /purge$1 last;
}
}
log_format accesslog '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /data/logs/access.log accesslog;
}
include vhosts.conf;
}
缓存应用于线上环境
可缓存内容:所有的附件图片img.arezone.cn img0-7.arezone.cn
由于js、css还有一些图标图片存放在www.arezone.cn下面
您可以选择一种方式赞助本站
支付宝转账赞助
