expires
、etag
、if-modified-since
指令来实现浏览器缓存控制upstream nodejs {
server 127.0.0.1:3002;
}
server {
listen 3001;
server_name 127.0.0.1;
location / {
proxy_pass http://nodejs;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
#gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";
expires 3m;
}
}
开启缓存后第一次请求
以后再请求
2.nginx代理层缓存
proxy_cache_path /data/cache levels=1:2 keys_zone=nodejs:10m max_size=1G inactive=10;
说明:
levels:配置在该目录下再分两层目录,一层1个随机字符作为名称,二层2个随机字符作为名称
levels最多三层,每层最多两个字符(为了加快访问文件的速度)
keys_zone:用来为这个缓存区起名,并设置大小
如上指定名为nodejs,供proxy_cache引用;10m就是内存空间的大小;
max_size:指定最大缓存数据磁盘空间的大小
inactive:在inactive指定的时间内,未被访问的缓存数据将从缓存中删除
http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; #新建缓存 proxy_cache_path D:/phpStudy/nginx/data/cache levels=1:2 keys_zone=nodejs:20M max_size=2G inactive=5; #为缓存数据添加头部信息 add_header my-Cache "$upstream_cache_status form $server_addr"; upstream nodejs { server 127.0.0.1:3002; } server { listen 3001; server_name 127.0.0.1; location / { proxy_pass http://nodejs; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; #gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; gzip_vary off; gzip_disable "MSIE [1-6]\."; #开启缓存 proxy_cache nodejs; #缓存设置 proxy_cache_valid 200 301 302 2m; proxy_cache_valid any 2m; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; add_header X-Via $server_addr; add_header X-Upstream $upstream_addr; add_header Nginx-Cache $upstream_cache_status; } } }
原文:https://www.cnblogs.com/baby123/p/11984636.html