
proxy模块指令
192.168.34.165
nginx.conf加入下面的代码:
proxy_cache_path /cache/nginx levels=1:2 keys_zone=cache_filename:10m max_size=5g inactive=60m use_temp_path=off;
注:位置在server外面
/vhost/study_nginx.conf
server{
listen 80;
server_name www.study_nginx.com;
root /www/study_nginx;
index index.html index.htm;
location /api/ {
proxy_cache test_cache;
proxy_pass http://www.nginx_cache.com:8080/api/;
proxy_cache_valid 200 304 12h;
proxy_cache_valid any 10m;
proxy_cache_key $host$uri$is_args$args;
include proxy_params;
}
location ~ /clear_cache(.*) {
allow all;
proxy_cache_purge test_cache $host$is_args$args;
}
}
192.168.34.135【代理服务器】
/vhost/nginx_cache.conf
server{
listen 8080;
server_name www.nginx_cache.com;
root /www/nginx_cache;
index index.html index.htm;
location ~ .php$ {
root /www/nginx_cache;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param script_FILENAME $document_root$fastcgi_script_name ;
include fastcgi_params;
}
}
/www/nginx_cache/api/index.php
访问:http://www.study_nginx.com/api/index.php
查看缓存文件【/cache/nginx/e/2b】
修改/www/nginx_cache/api/index.php 内容。再次访问:http://www.study_nginx.com/api/index.php。
发现还是上面的页面内容。缓存成功。
清除缓存
ngx_cache_purge介绍
若清除缓存时出现异常:
12180#0: unknown directive "proxy_cache_purge" in /vhost/blog.conf:24:
则表明缺失这个模块,nginx需重新编译,如下:
ngx_cache_purge是nginx的第三方模块,能够帮助我清除nginx中的缓存。
./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_gzip_static_module --with-http_stub_status_module --with-file-aio --with-http_realip_module --with-http_ssl_module --with-pcre=/home/pcre-8.44 --with-zlib=/home/zlib-1.2.11 --with-openssl=/home/openssl-1.1.1g --add-module=/home/ngx_cache_purge-2.3 make -j2 make instal
配置/vhost/study_nginx.conf
location ~ /clear_cache(.*) {
allow all;
proxy_cache_purge cache_filename $host$is_args$args;
}
#cache_name 需清除的缓存文件名
访问:http://www.study_nginx.com/clear_cache/api/index.php
再次访问:http://www.study_nginx.com/api/index.php。
发现页面显示了新修改的内容,缓存已删除。
/vhost/study_nginx.conf
set $a 0;
if ( $request_uri ~ .php$) {
set $a 1;
}
proxy_no_cache $a;
先访问:http://www.study_nginx.com/clear_cache/api/index.php
删除缓存文件。
再次访问:http://www.study_nginx.com/api/index.php
然后查看是否生成缓存文件。
nginx浏览器缓存proxy_no_cache
参数中的值可以设置多个,但是多个值中,只要有一个是不为0的,就不会缓存数据。
/vhost/study_nginx.conf
location {
expires 1m;
}
访问:http://www.study_nginx.com/
发现第2次访问之后状态为304。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)