网站首页 > 技术教程 正文
通常网站由动态和静态两种资源构成,其动态资源一般为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)
欢迎 你 发表评论:
- 12-16华为定时关机怎么设置在哪(华为定时开关机在哪里设置)
- 12-16夸克浏览器在线打开网页(夸克浏览器网页版)
- 12-16修复windows(修复windowsapp文件夹)
- 12-16球队排名榜实时排名(球队排名榜实时排名怎么看)
- 12-16windows10产品密钥永久激活免费
- 12-16怎样恢复u盘里的文件(怎样恢复u盘内容)
- 12-16电脑用着用着就黑屏了是怎么回事
- 12-16uc浏览器下载安装2025最新版
- 最近发表
- 标签列表
-
- 下划线是什么 (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)

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