很多使用OpenLiteSpeed环境的小伙伴,基本都会安装LiteSpeed Cache插件,其实很多静态文件的缓存设置,直接用该插件设置浏览器缓存是无效的。特别对于缓存时间等。这里搬主题就分享一下OpenLiteSpeed设置浏览器缓存,加快WordPress站点访问速度图文教程。特别是在 WordPress 站点优化中,浏览器缓存是提升访问速度的关键技术之一。使用LiteSpeed环境是可以直接用LiteSpeed Cache插件设置浏览器缓存时间的,但是使用OpenLiteSpeed环境则不行,所以要通过配置 OpenLiteSpeed 的缓存策略,可以有效减少服务器负载并大幅缩短页面加载时间。本教程将手把手教您如何通过 OpenLiteSpeed 的 WebAdmin 控制台,为静态资源设置缓存标头(Cache-Control、Expires)及自定义响应头,全面提升网站性能。比如你查看你的错误日志,在每次重启后,日志一般都会留下2025-02-11 14:25:43.513706 [INFO] [3636512] Invalid rewrite directive: ExpiresActive on
2025-02-11 14:25:43.513709 [INFO] [3636512] Invalid rewrite directive: ExpiresByType application/pdf A31557600
2025-02-11 14:25:43.513712 [INFO] [3636512] Invalid rewrite directive: ExpiresByType image/x-icon A31557600
2025-02-11 14:25:43.513715 [INFO] [3636512] Invalid rewrite directive: ExpiresByType image/vnd.microsoft.icon A31557600
2025-02-11 14:25:43.513722 [INFO] [3636512] Invalid rewrite directive: ExpiresByType image/svg+xml A31557600
2025-02-11 14:25:43.513725 [INFO] [3636512] Invalid rewrite directive: ExpiresByType image/jpg A31557600
2025-02-11 14:25:43.513728 [INFO] [3636512] Invalid rewrite directive: ExpiresByType image/jpeg A31557600
2025-02-11 14:25:43.513731 [INFO] [3636512] Invalid rewrite directive: ExpiresByType image/png A31557600
2025-02-11 14:25:43.513733 [INFO] [3636512] Invalid rewrite directive: ExpiresByType image/gif A31557600
2025-02-11 14:25:43.513736 [INFO] [3636512] Invalid rewrite directive: ExpiresByType image/webp A31557600
2025-02-11 14:25:43.513739 [INFO] [3636512] Invalid rewrite directive: ExpiresByType video/ogg A31557600
2025-02-11 14:25:43.513741 [INFO] [3636512] Invalid rewrite directive: ExpiresByType audio/ogg A31557600
2025-02-11 14:25:43.513744 [INFO] [3636512] Invalid rewrite directive: ExpiresByType video/mp4 A31557600
2025-02-11 14:25:43.513747 [INFO] [3636512] Invalid rewrite directive: ExpiresByType video/webm A31557600
2025-02-11 14:25:43.513749 [INFO] [3636512] Invalid rewrite directive: ExpiresByType text/css A31557600
2025-02-11 14:25:43.513752 [INFO] [3636512] Invalid rewrite directive: ExpiresByType text/javascript A31557600
2025-02-11 14:25:43.513755 [INFO] [3636512] Invalid rewrite directive: ExpiresByType application/javascript A31557600
2025-02-11 14:25:43.513758 [INFO] [3636512] Invalid rewrite directive: ExpiresByType application/x-javascript A31557600
2025-02-11 14:25:43.513760 [INFO] [3636512] Invalid rewrite directive: ExpiresByType application/x-font-ttf A31557600
2025-02-11 14:25:43.513763 [INFO] [3636512] Invalid rewrite directive: ExpiresByType application/x-font-woff A31557600
2025-02-11 14:25:43.513766 [INFO] [3636512] Invalid rewrite directive: ExpiresByType application/font-woff A31557600
2025-02-11 14:25:43.513768 [INFO] [3636512] Invalid rewrite directive: ExpiresByType application/font-woff2 A31557600
2025-02-11 14:25:43.513771 [INFO] [3636512] Invalid rewrite directive: ExpiresByType application/vnd.ms-fontobject A31557600
2025-02-11 14:25:43.513774 [INFO] [3636512] Invalid rewrite directive: ExpiresByType font/ttf A31557600
2025-02-11 14:25:43.513777 [INFO] [3636512] Invalid rewrite directive: ExpiresByType font/otf A31557600
2025-02-11 14:25:43.513779 [INFO] [3636512] Invalid rewrite directive: ExpiresByType font/woff A31557600
2025-02-11 14:25:43.513782 [INFO] [3636512] Invalid rewrite directive: ExpiresByType font/woff2 A31557600其实就是不支持OpenLiteSpeed。因此在某些情况下,您需要设置自定义响应标头,例如Cache-Control标头或Expire标头。下面是一些示例,向您展示如何完成此操作。浏览器缓存登录 WebAdmin 控制台,导航到虚拟主机 > 资源集,然后创建一个新的上下文。在新创建的上下文中,将类型设置为Static。换成中文,参考如下配置为:对于我们的浏览器缓存,我们希望定位 JavaScript 和 CSS 文件,因此我们可以使用正则表达式来匹配URI设置中这些类型的所有文件,如下所示:exp:^.*(css|js)$
如果愿意,您可以添加更多静态文件扩展名,例如:exp:^.*(css|gif|ico|jpeg|jpg|js|png|webp|woff|woff2|fon|fot|ttf)$
设置以下内容:位置:( $DOC_ROOT/$0我们需要这个,$0因为URI使用正则表达式)可访问:Yes启用过期:Yes将Expires by Type设置为以下内容(15552000秒等于 180 天):application/javascript=A15552000, text/css=A15552000
如果愿意,您可以向Expires by Type添加更多类型 ,例如:text/css=A15552000, image/gif=A15552000, image/x-icon=A15552000, image/jpg=A15552000, image/jpeg=A15552000, application/x-javascript=A15552000, text/javascript=A15552000, application/javascript=A15552000, image/png=A15552000, image/webp=A15552000, font/ttf=A15552000, font/woff=A15552000, font/woff2=A15552000, application/x-font-ttf=A15552000, application/x-font-woff=A15552000, application/font-woff=A15552000, application/font-woff2=A15552000
将Header Operations设置为以下内容:unset Cache-control
set Cache-control public, max-age=15552000
该unset cache-control行会删除先前的缓存控制标头,然后在下一行设置新的缓存控制标头。设置后的效果如下:X-Powered-By如果需要添加更多标头,则语法和用法与 Apache 的支持操作的mod_headers指令类似。以下是添加标题的示例X-Powered-By:设置以下内容:网址: exp:^.*(css|js)$位置:( $DOC_ROOT/$0我们需要这个,$0因为URI使用正则表达式)可访问:Yes标头操作:set X-Powered-By OpenLiteSpeed。
THE END