- nginx 可以通过regex 对指定类型或者path的文件进行cache
- 可以根据upstream http code进行不同时间cache
- nginx plus 支持通过curl 进行purge
- 可以通过开源module 实现plus的purge
FROM centos:centos7.4.1708 RUN yum install -y make zlib zlib-devel gcc pcre-devel openssl-devel ENV PATH=/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin ENV NGINX_VERSION=1.21.6 ENV NJS_VERSION=0.7.2 ADD nginx-1.21.6.tar.gz /usr ADD ngx_cache_purge-2.3.1.tar.gz /usr WORKDIR /usr/nginx-1.21.6 RUN ./configure --without-select_module --with-http_auth_request_module --add-module=/usr/ngx_cache_purge-2.3.1 RUN make && make install CMD nginx -g "daemon off;" # docker build -t nginx:1.21.6 . # ngx_cache_purge来自 https://github.com/torden/ngx_cache_purge测试用配置文件
一下是测试用配置文件,注意
- 开源的proxy_cache_purge 不支持$scheme
- 开源用法与plus不一致
- purge的key 要和cache key一致
- purge一定要限定可以操作的ip range
worker_processes auto;
# nginx 规则表达式guide
#https://www.thegeekstuff.com/2017/05/nginx-location-examples/
# nginx cache guide,注意这里的purge 是居于nginx plus的,不适应于本配置文件
#https://www.nginx.com/blog/nginx-caching-guide/
# #docker build -t nginx:1.21.6 .
# docker run --name my-custom-nginx-container -p 1888:1888 -v `pwd`/log:/usr/local/nginx/logs -v `pwd`/cache.conf://usr/local/nginx/conf/nginx.conf:ro -v `pwd`/proxy_cache_path:/proxy_cache_path --rm -it nginx:1.21.6
#purge 使用https://github.com/torden/ngx_cache_purge
worker_rlimit_nofile 65535;
events {
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_iso8601] "$request" ' '$status $request_time $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$upstream_addr" $upstream_response_time' '$http_cookie"';
log_format main '$remote_addr - $remote_user [$time_iso8601] "$request" ' '$status $request_time $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$upstream_addr" $upstream_response_time ' '"$http_cookie"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#不显示版本号
server_tokens off;
#gzip on;
upstream bomc_cache {
ip_hash;
server 172.22.136.123:18080 ;
}
map $request_method $purge_method {
PURGE 1;
default 0;
}
# purger=on
#levels=1:2 temporary files will be put directly in the cache directory.
proxy_cache_path /proxy_cache_path use_temp_path=off keys_zone=mycache:10m ;
server {
listen 1888;
# server_name 10.230.65.197;
#通过变量设置指定是否缓存
if ($request_uri ~* .(png|gif|ico|jpg|jpeg|css|js|ttf|woff)$ ){
set $bypass 0;
}
#location ~ "/img/([0-9a-fA-F]{2})([0-9a-fA-F]+)$"
location / {
# proxy_redirect on;
proxy_buffering on;
#use default key
# proxy_cache_key $scheme$proxy_host$uri$is_args$args;
# $scheme所用的协议,比如http或者是https
proxy_cache_key $uri$proxy_host$is_args$args;
# proxy_cache_key $uri$is_args$args;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://bomc_cache;
#cache time 正常100m、404一分钟
proxy_cache_valid 200 302 100m;
proxy_cache_valid 404 1m;
proxy_cache mycache;
proxy_cache_revalidate on;
#X-Cache-Status 如果被设置为HIT – The response contains valid, fresh content direct from the cache.
add_header X-Cache-Status $upstream_cache_status;
proxy_next_upstream error timeout invalid_header http_429 http_500 http_502 http_503 http_504 http_403;
#服务端失败,使用缓存
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
#对于非指定文件类型,不做cache
proxy_cache_bypass $bypass;
}
#设置purge 在allow 主机上执行curl -X PURGE -D - "http://localhost:1888/purge/*";ls /*path/
location ~ /purge(/.*) {
allow 127.0.0.1;
allow 192.168.157.140;
allow 168.168.1.1;
deny all;
# ##和key 保持一致
proxy_cache_purge mycache $1$proxy_host$is_args$args;
# proxy_cache_purge mycache $1$is_args$args;
#$scheme$proxy_host$1$is_args$args;
#
}
}
}



