【摘自张宴的"实战:Nginx"】使用nginx的proxy_cache模块替代squid,缓存静态文件


#user nobody;
worker_processes 1;

error_log logs/static_source.error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
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/static_source.access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

gzip on;


#*************************************************************************
#nginx缓存区的使用,作用类似squid,第一次访问的时候加载资源,后面访问的时候直接使用资源,这里是使用Nginx的方式实现缓存,这里是缓存静态的资源类文件, fastcgi也可以可以缓存的,在server模块里面可以设置的,后面会提到
#注 proxy_temp_path, proxy_cache_path指定的路径必须在一个分区
proxy_temp_path /data/proxy_temp_path;

#设置web缓存区的名字为cache_one ,内存缓存空间大小为200M, 自动清除超过1天美意被访问的缓存数据,磁盘空间大小为30GB
proxy_cache_path /data/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
#************************************************************************
server {
listen 81;
server_name localhost;

#charset koi8-r;

access_log logs/81.access.log main;
error_log logs/81.error.log info;

root /data/www/static; #下面的每一个location模块都共用这个root地址,每个location也可以单独的配置root地址
location / {
#root /data/www/static;
index index.php index.html index.htm;
}

#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; 共享上面的在server里配置的root值,注意,如果这里配置了root,那么php后缀的,都是去这里的root找哦,每个单独的location都是可以单独的配置root的
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$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;
#}
}

 

upstream my_server_pool
{
server 127.0.0.1:81 weight=1 max_fails=2 fail_timeout=30s;
}

server {
listen 80;
server_name www.test.com;

charset utf-8;

access_log logs/test.access.log main;

#location /
#{
# proxy_set_header Host $host;
# proxy_set_header X-Forwarded-For $remote_addr;
# proxy_pass http://my_server_pool;
#
#}

#用于清除缓存,假设URL为my.domain.com/test.gif 通过http://my.domain.com/purge/test.gif可以清除该URL的缓存
location ~ /purge(/.*)
{
#return 402;
#设置值允许指定的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;
#proxy_pass http://my_server_pool;
#proxy_cache cache_one;
#proxy_cache_key $host$uri$is_args$args;
#proxy_cache_purge PURGE from 127.0.0.1;
}


#使用nginx作为缓存,缓存静态文件
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$
{
#使用web缓存区cache_one (在nginx.conf文件定义的)
proxy_cache cache_one;

expires 1h;
#对不同http状态马缓存设置不同的缓存时间
proxy_cache_valid 200 304 12h;         #这里表示如果访问的返回的是200或者304状态码,那么缓存12h,在这个12之内,即使静态资源有改变,也会返回缓存的,所以资源改变了,需要删除缓存的资源
proxy_cache_valid 301 302 1m;
proxy_cache_valid any 1m;

#设置web缓存的key值,nginx根据key值md5哈希存储缓存,这里根据域名,uri, 参数 组合合成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;
}


#扩展名以.php、.jsp、.cgi结尾的动态应用程序不缓存
location ~ .*\.(php|jsp|cgi)?$
{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://my_server_pool;
}

}
}

 

 

 

访问资源 http://192.168.66.138/b.css,会在/data/proxy_cache_path生成缓存文件

页面输出的内容:

body {
border-style:solid;
border-width:5px;
}

在缓存文件目录/data/proxy_cache_path生成文件6cd081e217db73624337f1521e858b6a
内容如下(下面那个不是乱码,是缓存文件里面的内容):

^C^@^@^@e¯HW/¶GW¥^FHW<9c>^L0^S^@^@<9b>^@<81>^A^M"5747b62f-2f"^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^^@
KEY: 192.168.66.138/b.css
HTTP/1.1 200 OK^M
Server: nginx/1.11.0^M
Date: Fri, 27 May 2016 08:34:45 GMT^M
Content-Type: text/css^M
Content-Length: 47^M
Last-Modified: Fri, 27 May 2016 02:51:27 GMT^M
Connection: close^M
ETag: "5747b62f-2f"^M
Accept-Ranges: bytes^M
^M
body {
border-style:solid;
border-width:5px;
}

 

访问http://192.168.66.138/purge/b.css页面会显示如下图片

 

posted @ 2016-05-27 13:49  maxomnis  阅读(1177)  评论(0编辑  收藏  举报