网站首页 > 技术教程 正文
通常网站由动态和静态两种资源构成,其动态资源一般为PHP、ASP、ASP.net、JAVA等程序,而静态资源一般是图片、样式文件(CSS)、JS代码文件等。由于静态资源在一般情况下是很少变更的,所以在WEB服务器中可以通过设置客户端的缓存时间来达到节省网络带宽及提高效率的目的。一般来说,图片文件、CSS样式文件、JS代码文件推荐设置的缓存时间可以为一年,这也是PageSpeed Insights推荐的。
Nginx设置静态资源缓存的方法
Nginx虚拟主机配置文件中的Server块中添加以下代码:
location ~ .*/.(gif|jpg|jpeg|png|bmp|swf|WebP)$
{
expires 365d;
error_log /dev/null;
access_log off;
}
location ~ .*/.(js|css)?$
{
expires 365d;
error_log /dev/null;
access_log off;
}
注意,以上代码需插入在Server块中,也就是server{.......}的 } 之前。
expires 365d;的意思就是将该类型的文件缓存时间设置为365天,expires的具体语法如下:
expires 60s; #缓存60秒
expires 10m; #缓存10分钟
expires 12h; #缓存12小时
expires 30d; #缓存30天
设置完成后,重启Nginx即生效。
Apache设置静态资源缓存
Apache设置缓存之前,需先开启LoadModule expires_module modules/mod_expires.so模块,编辑Apache的”httpd.conf”,找到这么一行:
#LoadModule expires_module modules/mod_expires.so
将该行前面的”#”字号删除,保存,重新启动Apache生效。
然后在主机配置文件中加入以下代码(示例):
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A86400
ExpiresByType image/x-icon A31536000
ExpiresByType application/x-javascript A31536000
ExpiresByType text/css "access plus 30 days"
ExpiresByType image/gif A31536000
ExpiresByType image/png A31536000
ExpiresByType image/jpeg A31536000
ExpiresByType text/plain A31536000
ExpiresByType application/x-shockwave-flash A31536000
ExpiresByType video/x-flv A31536000
ExpiresByType application/pdf A604800
ExpiresByType text/html A900
</IfModule>
其中A31536000就是31536000 秒,相当于一年。或者也可以写成:"access plus 365 days",如下示例:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A86400
ExpiresByType image/x-icon "access plus 365 days"
ExpiresByType application/x-javascript "access plus 365 days"
ExpiresByType text/css "access plus 365 days"
ExpiresByType image/gif "access plus 365 days"
ExpiresByType image/png "access plus 365 days"
ExpiresByType image/jpeg "access plus 365 days"
ExpiresByType text/plain "access plus 365 days"
ExpiresByType application/x-shockwave-flash "access plus 365 days"
ExpiresByType video/x-flv "access plus 365 days"
ExpiresByType application/pdf A604800
ExpiresByType text/html A900
</IfModule>
保存,重新启动Apache生效。
验证缓存设置是否生效
在Chrome或Edge浏览器中,访问某.png文件Url(已设置缓存时间为一年),然后按F12查看该文件的Headers信息,可以看到如下图:
浏览器中查看Headers信息
可以看到cache-control中的最大缓存时间为:31536000秒(一年),这就代表设置成功了。
猜你喜欢
- 2024-10-10 Nginx web服务器(nginx Web服务器)
- 2024-10-10 Nginx 了解一下?(nginx的理解)
- 2024-10-10 Nginx可以做什么?看完这篇你就懂了
- 2024-10-10 Nginx常用配置(nginx配置cgi)
- 2024-10-10 Spring Boot 中的静态资源到底要放在哪里?
- 2024-10-10 「Web开发」Spring MVC 中的静态资源与缓存
- 2024-09-14 Openresty/Nginx 缓存设置(nginx 缓存配置)
- 2024-09-14 Nginx之6大千世界 - (FastCGI)(nginxgui)
- 2024-09-14 一篇文章了解nginx特性,值得收藏
- 2024-09-14 Nginx-location的匹配规则(nginx配置文件中的location)
你 发表评论:
欢迎- 08-06linux 和 windows文件格式互相转换
- 08-06谷歌 ChromeOS 已支持 7z、iso、tar 文件格式
- 08-06Linux下比较文件内容的6种方法
- 08-06文件格式及功能汇总
- 08-0610个Linux文件内容查看命令的实用示例
- 08-06Linux-如何区分不同文件类型
- 08-06Zabbix技术分享——监控windows进程资源使用情况
- 08-06Linux系统卡顿?学会ps命令这三招,轻松定位问题进程
- 最近发表
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)