nginx静态缓存配置

nginx_静态缓存

##缓存cache参数配置
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_fuffers_size 128k;
proxy_temp_file_write_size 128k;

##缓存到nginx的本地目录
proxy_temp_path /User/xxx/Web/nginx/temp/;
proxy_cache_path /User/xxx/Web/nginx/temp/cache_temp evels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;

##end

#压缩配置
gzip on; #打开gzip压缩功能
gzip_min_length 1k; #压缩阈值
gzip_buffers 4 16k; #buffer不用修改
gzip_comp_level 2; #压缩级别:1-10, 数字越大压缩越好,时间越长
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; $压缩文件类型
gizp_vary off; #跟squid等缓存服务有关,on的话会在header里增加"Vary: Accept-Encoding"
gzip_disable "MSIE [1-6]." #IE1-6 版本不支持gzip压缩

#具体的服务器地址及端口号,该处可以配置集群实现负载均衡,cluster 为服务器集群名
upstream cluster{
server 10.16.39.12:8080; #可以分配权重weight,这样来配置集群服务器的访问优先权
}

rewrite ^(.*)$ https://$host$1 permanent; #http跳转https

#缓存响应的文件,在 server下 location

location ~ .(gif|jpg|png|htm|html|css|js|flv|ico|swf)(.*) {
proxy_pass http://cluster; # 如果没有缓存则通过proxy-pass转向请求
proxy_redirect off;
proxy_set_header Host $host;
proxy_cache cache_one;
proxy_cache_valid 200 302 1h; #对不同的HTTP状态设置不同的缓存时间,h小时,d天数
proxy_cache_valid 301 1d;
proxy_cache_valid any 1m;
expires 30d;
}

#动态请求代理给相应服务器
location / {
proxy_pass http://cluster;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}

#purge插件缓存清理
location ~ /purge(/.*) {
allow 127.0.0.1; #能够清除缓存的服务器IP地址
allow 10.16.39.12;
deny all;
proxy_cache_purge cache_one $1$is_args$args;
}

原文地址:https://www.cnblogs.com/sqbk/p/14283321.html