JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

Nginx如何配置缓存,看完之后,大部分程序员收藏了...

wys521 2024-09-17 02:47:45 精选教程 24 ℃ 0 评论

缓存配置proxy_cache.conf:

#设置Web缓存区名称为cache,内存缓存空间大小为500MB
#1天没有被访问的内容自动清除,硬盘缓存空间大小为50GB
#levels=1:2 表示缓存目录的第一级目录是1个字符,第二级目录是2个字符
#proxy_cache_key 设置缓存的key,以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
proxy_cache_path /home/cache levels=1:2 keys_zone=cache:500m inactive=1d max_size=50g;
proxy_cache_valid 200 304 301 302 8h;
proxy_cache_valid 404 1m;
proxy_cache_key $host$uri$is_args$args;

domain.conf:

server {
 listen 80;
 server_name domain.test.com;
 access_log /usr/local/nginx/logs/domain.test.com.access.log access;
 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js|ico)?$ {
 expires 1d;#缓存时间
 proxy_cache cache;#设置资源缓存的zon
 add_header Nginx-Cache "$upstream_cache_status";#显示缓存的状态
 proxy_pass http://domain;
 }
 location / {
 proxy_pass http://domain;
 }
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root html;
 }
}
upstream report {
 fair;
 server 172.16.0.10:8882 weight=1 max_fails=2 fail_timeout=30s;
 server 172.16.0.10:8881 weight=1 max_fails=2 fail_timeout=30s;
}

重启Nginx,清空浏览器缓存,刷新页面,F12 查看:

Cache-Control:max-age=86400
Connection:keep-alive
Content-Encoding:gzip
Content-Type:application/javascript
Date:Fri, 05 May 2019 08:21:22 GMT
Expires:Sat, 06 May 2019 08:21:22 GMT
Last-Modified:Thu, 27 Apr 2019 01:27:36 GMT
Nginx-Cache:HIT
Server:Tengine/2.1.1
Transfer-Encoding:chunked

关注

感谢阅读,如果这篇文章帮助了您,欢迎 点赞收藏,关注转发 哟。您的帮助是我们前行的动力,我们会提供更多有价值的内容给大家... 谢谢!

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表