Nginx代理proxy_cache缓存文件
时间:2014-12-11 00:35 来源:linux.it.net.cn 作者:IT
Nginx代理proxy_cache缓存文件
1.首先Nginx需要启用proxy_cache模块
可以通过nginx -V 来查看下,编译安装的时候是否加有proxy_cache模块
itnetcn@ubuntu-T2:~$ sudo /home/app/nginx/nginx-1.2.2/sbin/nginx -V
nginx version: nginx/1.2.2
built by gcc 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
configure arguments: --prefix=/home/app/nginx/nginx-1.2.2 --add-module=../ngx_cache_purge-2.1
2.配置如下:
itnetcn@ubuntu-T2:~$ cat /home/app/nginx/nginx-1.2.2/conf/nginx.conf
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
###################################################################
#proxy_cache_path 缓存存放路径
#levels 设置目录层次
#keys_zone 设置缓存名字和共享内存大小 [我这定义的缓存名字是content]
#inactive 在指定时间内没人访问则被删除在这里是1天
#max_size 最大缓存空间
##################################################################
proxy_cache_path /tmp/proxy_cache levels=1:2 keys_zone=content:20m inactive=1d max_size=100m;
server {
listen 80;
server_name localhost;
#注意: 过滤放行的规则需要放到location / 前面,不然也是不生效的
#动态的页面放行,不进行缓存
location ~ .*\.(aspx|php|jsp|cgi)?$
{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://127.0.0.1:8000; #代理转发的地址
}
location /
{
proxy_cache content; #根keys_zone后的内容对应,缓存名字
proxy_cache_valid 200 304 301 302 1d; #那些状态缓存多长时间
proxy_cache_valid any 1d; #其它的缓存多长时间
proxy_cache_key $host$uri$is_args$args; #通过key来hash,定义KEY的值
proxy_pass http://127.0.0.1:8000; #代理转发的地址
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
3.重新加载配置文件或重启nginx来验证下
itnetcn@ubuntu-T2:~$ sudo /home/app/nginx/nginx-1.2.2/sbin/nginx -s reload
4.查看下这个目录是否有数据 /tmp/proxy_cache
itnetcn@ubuntu-T2:~$ sudo watch -n 1 du -h /tmp/proxy_cache/
(责任编辑:IT)
Nginx代理proxy_cache缓存文件 1.首先Nginx需要启用proxy_cache模块 可以通过nginx -V 来查看下,编译安装的时候是否加有proxy_cache模块 itnetcn@ubuntu-T2:~$ sudo /home/app/nginx/nginx-1.2.2/sbin/nginx -V nginx version: nginx/1.2.2 built by gcc 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) configure arguments: --prefix=/home/app/nginx/nginx-1.2.2 --add-module=../ngx_cache_purge-2.1 2.配置如下: itnetcn@ubuntu-T2:~$ cat /home/app/nginx/nginx-1.2.2/conf/nginx.conf #user nobody; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; ################################################################### #proxy_cache_path 缓存存放路径 #levels 设置目录层次 #keys_zone 设置缓存名字和共享内存大小 [我这定义的缓存名字是content] #inactive 在指定时间内没人访问则被删除在这里是1天 #max_size 最大缓存空间 ################################################################## proxy_cache_path /tmp/proxy_cache levels=1:2 keys_zone=content:20m inactive=1d max_size=100m; server { listen 80; server_name localhost; #注意: 过滤放行的规则需要放到location / 前面,不然也是不生效的 #动态的页面放行,不进行缓存 location ~ .*\.(aspx|php|jsp|cgi)?$ { proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://127.0.0.1:8000; #代理转发的地址 } location / { proxy_cache content; #根keys_zone后的内容对应,缓存名字 proxy_cache_valid 200 304 301 302 1d; #那些状态缓存多长时间 proxy_cache_valid any 1d; #其它的缓存多长时间 proxy_cache_key $host$uri$is_args$args; #通过key来hash,定义KEY的值 proxy_pass http://127.0.0.1:8000; #代理转发的地址 proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } 3.重新加载配置文件或重启nginx来验证下 itnetcn@ubuntu-T2:~$ sudo /home/app/nginx/nginx-1.2.2/sbin/nginx -s reload 4.查看下这个目录是否有数据 /tmp/proxy_cache itnetcn@ubuntu-T2:~$ sudo watch -n 1 du -h /tmp/proxy_cache/ (责任编辑:IT) |