Nginx系列(三)——缓存配置

本文最近更新于2020.6.14

(一)缓存配置

简单来所,缓冲可以避免请求再到后台服务器执行相应的php操作,节省执行时间.但其中一个用户访问后会生成对应的缓存,当其他用户进行相同的请求时,也可以直接使用之前的用户访问后生成的缓存.
该参数位于http{}模块内,配置了缓存路径,缓存名为CACHE,其共享内存大小为60m,缓存文件目录1:2代表两级目录,该缓存超过30s没有被使用则会自动删除,当缓存空间达到20g时清空旧缓存
proxy_cache_path /var/nginx/cache keys_zone=CACHE:60m levels=1:2 inactive=30s max_size=20g;

以下参数行放在server的location{}模块下
proxy_cache CACHE;  #接收到请求后,先在这里查看是否命中缓存文件
proxy_cache_valid  200 206 304 301 302 10d; #返回所示状态码则进行缓存,缓冲时长为10天
proxy_cache_key $uri;   #指定uri为缓冲key,对uri进行hash计算
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.2.130:80/;    #代理到另一台web上

完整配置文件

[root@129-node conf]# cat nginx.conf|grep -v '#'|grep -v ^$
worker_processes  1;
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;
    keepalive_timeout  65;
    proxy_cache_path /var/nginx/cache keys_zone=CACHE:60m levels=1:2 inactive=30s max_size=20g;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_cache CACHE;
            proxy_cache_valid  200 206 304 301 302 10d;
            proxy_cache_key $uri;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://192.168.2.130:80/;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@129-node conf]# 
View Code

验证
[root@129-node 7d]# cd /var/nginx/cache/9/7d/
[root@129-node 7d]# ls
6666cd76f96956469e7be39d750cc7d9

(二)缓存切片(Cache Slicing)

适用于不会变动的大文件
Nginx的slice模块可以将一个请求分解成多个子请求,每个子请求返回响应内容的一个片段,让大文件的缓存更有效率。
ngx_http_slice_filter_module模块默认没有编译到Nginx程序中,需要编译时添加--with-http_slice_module选项。
slice指令设置分片的大小为1m。 这里使用了proxy_set_header指令,在取源时的HTTP请求中添加了Range头部,向源服务器请求文件的一部分,而不是全部内容。在proxy_cache_key中添加slice_range变量这样可以分片缓存。
proxy_cache_path /tmp/mycache keys_zone=mycache:10m;
server {
    ...
    proxy_cache mycache;
    slice 1m;    #以1m为单位进行切片
    proxy_cache_key $host$uri$is_args$args$slice_range;    #切片命名方式?
    proxy_set_header Range $slice_range;
    proxy_http_version 1.1;
    proxy_cache_valid 200 206 1h;
    location / {
    proxy_pass http://origin:80;
    }
}

(三)其他常用缓冲配置Cache Bypass

避开缓存。适用于进行troubleshooting和debug的时候
proxy_cache_bypass $http_cache_bypass;
另外还可以通过设置 proxy_cache off; 进行关闭缓存
主动清除缓存可以使用proxy_cache_purge
在http头中查看缓存命中情况,只需要添加add_header  Nginx-Cache "$upstream_cache_status";即可

配置浏览器端缓冲
location ~* \.(css|js)$ {
    expires 1y;    #1年后过期,该参数效率较为底下,现在被Cache-Control代替了?
    add_header Cache-Control "public";        #配置http响应头缓存控制
}

(四)资料参考

参考链接
https://my.oschina.net/u/2539854/blog/1634878
https://blog.csdn.net/dengjiexian123/article/details/53386586

posted on 2020-05-21 18:06  标配的小号  阅读(489)  评论(0编辑  收藏  举报

导航