proxy_cache_path /app/proxy_cache_dir levels=1:2 keys_zone=cache1:200m inactive=1d max_size=10g; #设置Web缓存区名称为cache1,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。levels=1:2 表示缓存目录的第一级目录是1个字符,第二级目录是2个字符,即/app/proxy_cache_dir/cache1/a/1b这种形式 server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } location ~ /purge(/.*) { allow 127.0.0.1; allow 192.168.116.0/24 deny all; proxy_cache_purge cache1 $host$1$is_args$args; } location ~ \.(gif|jpg|jpeg|png|bmp|ico)$ { proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://127.0.0.1:8080; proxy_cache cache1; #设置资源缓存的zone proxy_cache_key $host$uri$is_args$args; #设置缓存的key,以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内 proxy_cache_valid 200 304 12h; #对不同的HTTP状态码设置不同的缓存时间 expires 7d; #缓存时间 } #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; } }
user root; #user nobody; worker_processes 1; #error_log logs/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/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream myapp { server 10.10.10.75:8058 weight=1 max_fails=2 fail_timeout=30s; server 10.10.10.73:8058 weight=1 max_fails=2 fail_timeout=30s; server 10.10.10.72:8058 weight=1 max_fails=2 fail_timeout=30s; server 10.10.10.74:8058 weight=1 max_fails=2 fail_timeout=30s; } upstream backend { # server 10.10.10.73:8058 weight=1; # server 10.10.10.75:8058 weight=1; # server 10.10.10.72:8058 weight=1; server 10.10.10.76:8058 weight=1; } #设置图片缓存 proxy_cache_path /usr/local/insbook/nginxcache levels=1:2 keys_zone=cache1:200m inactive=1d max_size=10g; # use_temp_path off; server { listen 80; server_name 10.10.10.76; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } location ~ \.jsp$ { proxy_pass http://backend; } location ~ \.action$ { proxy_pass http://myapp; } location ~ \.(gif|jpg|jpeg|png)$ { proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://backend; proxy_cache cache1; #设置资源缓存的zone proxy_cache_key $host$uri$is_args$args; #设置缓存的key,以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内 proxy_cache_valid 200 304 12h; #对不同的HTTP状态码设置不同的缓存时间 expires 7d; #缓存时间 } #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; } } # listen 8000; }
访问一张图片
查看缓存目录
打卡tomcat运行日志,再次访问没有请求后台,而是读取的缓存。