宝塔 Nginx 构建 Service Worker

发布时间:2026/8/16 14:44:51

宝塔 Nginx 构建 Service Worker
当网站后端暂时无法访问时可以让 Nginx 自动显示一个静态备用页面提示用户检查网络并提供售后信息。一、准备文件将以下两个文件上传到宝塔网站根目录/www/wwwroot/test.zhangsan.cool/offline.html /www/wwwroot/test.zhangsan.cool/sw.js文件说明供参考具体自行修改offline.html网站无法访问时显示的备用页面。!doctype htmlhtmllangzh-CNheadmetacharsetutf-8metanameviewportcontentwidthdevice-width, initial-scale1, viewport-fitcovermetanametheme-colorcontent#f5f7fbmetanamedescriptioncontent网站备用入口和联系方式。title网站暂时无法访问备用入口/titlestyle:root{color-scheme: light;--ink:#182235; --muted:#687386; --blue:#2563eb; --line:#e2e7f0; --surface:#fff; }*{box-sizing:border-box;}html, body{margin:0;min-height:100%;}body{font-family:-apple-system,BlinkMacSystemFont,Segoe UI,PingFang SC,Microsoft YaHei,Arial,sans-serif;color:var(--ink);background:#f5f7fb; }.shell{min-height:100vh;padding:24px 16px calc(28px env(safe-area-inset-bottom));display:flex;align-items:center;justify-content:center;}.page{width:min(100%,620px);}.card{background:var(--surface);border:1px solid var(--line);border-radius:18px;padding:28px 20px 22px;box-shadow:0 12px 35px rgba(30,45,75,.07);}h1{margin:0;font-size:clamp(28px,7vw,42px);line-height:1.2;letter-spacing:-.03em;}.status{display:inline-flex;align-items:center;gap:7px;margin-bottom:14px;color:#a16207; font-size:13px; font-weight:700; }.status-dot{width:8px;height:8px;border-radius:50%;background:#eab308; box-shadow:0 0 0 4px #fef3c7; }.intro{margin:10px022px;color:var(--muted);font-size:15px;line-height:1.65;}.nav-link{display:flex;align-items:center;justify-content:center;gap:9px;min-height:54px;padding:14px 18px;border-radius:12px;color:#fff; background:var(--blue); text-decoration:none; font-size:17px; font-weight:700; box-shadow:0 8px 16px rgba(37,99,235,.2); }.nav-link:hover, .nav-link:focus-visible{background:#1d4ed8; }.nav-link svg{width:19px;height:19px;}.section-title{margin:26px010px;font-size:17px;}.contacts{display:grid;gap:10px;}.contact{display:flex;align-items:center;gap:12px;min-width:0;padding:14px;border:1px solid var(--line);border-radius:12px;}.contact-icon{flex:0036px;width:36px;height:36px;display:grid;place-items:center;border-radius:10px;color:var(--blue);background:#eef4ff; }.contact-icon svg{width:20px;height:20px;}.contact-copy{flex:1;min-width:0;}.label{margin:003px;color:var(--muted);font-size:13px;}.value{margin:0;font-size:19px;font-weight:700;overflow-wrap:anywhere;}.copy{flex:00auto;min-height:40px;padding:0 14px;border:1px solid#bfd2f6; border-radius:9px; color:var(--blue); background:#fff; font:inherit; font-size:14px; font-weight:700; cursor:pointer; }.copy:hover, .copy:focus-visible{background:#eef4ff; }.copy.copied{border-color:#b7dfc9; color:#147a4b; background:#edf9f2; }.contact--featured{border-color:#bfd2f6; background:#f8fbff; }:is(.nav-link, .copy):focus-visible{outline:3px solid rgba(37,99,235,.28);outline-offset:3px;}.note{margin:18px00;color:#8a94a5; font-size:12px; line-height:1.6; text-align:center; }.toast{position:fixed;left:50%;bottom:calc(22px env(safe-area-inset-bottom));transform:translate(-50%,16px);opacity:0;pointer-events:none;padding:9px 13px;border-radius:9px;color:#fff; background:#1d293d; font-size:13px; transition:opacity .18s,transform .18s; }.toast.show{opacity:1;transform:translate(-50%,0);}media(max-width:599px){.shell{padding:14px 12px calc(20px env(safe-area-inset-bottom));}.card{padding:18px 14px 22px;border-radius:15px;}.intro{margin-bottom:18px;}.section-title{margin-top:22px;}}media(min-width:760px){.card{padding:40px 42px 30px;}.contacts{grid-template-columns:1fr 1fr;}}media(prefers-reduced-motion: reduce){.toast{transition:none;}}/style/headbodymainclassshellsectionclasspage cardaria-labelledbytitledivclassstatusspanclassstatus-dotaria-hiddentrue/span当前连接可能不稳定/divh1idtitle网站暂时无法访问/h1pclassintro请先检查网络连接并刷新页面如果仍无法访问可通过下方联系方式获取帮助。/paclassnav-linkhrefhttps://vlink.cc/uu5wok0ktarget_blankrelnoopener noreferrer打开最新网址svgviewBox0 0 24 24fillnonestrokecurrentColorstroke-width2aria-hiddentruepathdM14 5h5v5M19 5l-8 8/pathdM18 13v5a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5//svg/ah2classsection-title备用联系方式/h2divclasscontactsdivclasscontact contact--featuredspanclasscontact-iconaria-hiddentruesvgviewBox0 0 24 24fillnonestrokecurrentColorstroke-width2pathdM7 7h10v8H7z/pathdM7 10H4v5h3M17 10h3v5h-3M9 18h6M12 15v3//svg/spandivclasscontact-copypclasslabel售后QQ群 · 进群获取最新资讯/ppclassvalue815440815/p/divbuttonclasscopytypebutton>815440815>复制aria-label复制QQ群号 815440815复制/button/divdivclasscontactspanclasscontact-iconaria-hiddentruesvgviewBox0 0 24 24fillnonestrokecurrentColorstroke-width2pathdM20 11.5a7.5 7.5 0 0 1-11.8 6.1L4 19l1.5-3.9A7.5 7.5 0 1 1 20 11.5Z/pathdM9 10.5c.6 1.3 1.7 2.4 3 3l1-.8 1.5.7-.3 1.3c-.2.6-.8.8-1.4.6a8.3 8.3 0 0 1-5.1-5.1c-.2-.6 0-1.2.6-1.4l1.3-.3.7 1.5-.8 1Z//svg/spandivclasscontact-copypclasslabel售后微信/ppclassvalue15682448658/p/divbuttonclasscopytypebutton>15682448658>复制aria-label复制售后微信号 15682448658复制/button/div/divpclassnote建议收藏本页方便下次快速找到入口。/p/section/maindivclasstoastrolestatusaria-livepolite/divscript((){ const toastdocument.querySelector(.toast);let timer;const show(message){ toast.textContentmessage;toast.classList.add(show);clearTimeout(timer);timersetTimeout(()toast.classList.remove(show),1800);};const fallbackCopy(text){ const areadocument.createElement(textarea);area.valuetext;area.setAttribute(readonly,);area.style.positionfixed;area.style.opacity0;document.body.appendChild(area);area.select();const okdocument.execCommand(copy);area.remove();return ok;};document.querySelectorAll([data-copy]).forEach((button)button.addEventListener(click,async(){ const textbutton.dataset.copy;try { if(navigator.clipboardwindow.isSecureContext)await navigator.clipboard.writeText(text);else if(!fallbackCopy(text))throw new Error(copy failed);button.textContent已复制 ✓;button.classList.add(copied);clearTimeout(button._resetTimer);button._resetTimersetTimeout((){ button.textContentbutton.dataset.defaultLabel;button.classList.remove(copied);},1800);show(已复制text);} catch { show(复制失败请手动复制);} }));})();/scriptscriptif(serviceWorkerinnavigator){window.addEventListener(load,(){navigator.serviceWorker.register(/sw.js,{scope:/}).then((registration){ console.log(Service Worker 注册成功:,registration.scope);return registration.update();}).catch((error)console.error(Service Worker 注册失败:,error));});}else{console.warn(当前浏览器不支持 Service Worker);}/script/body/htmlsw.js负责缓存备用页面并在断网时返回缓存内容。const CACHE_NAMEoffline-contact-v5;const OFFLINE_URL/offline.html;self.addEventListener(install,(event){event.waitUntil(caches.open(CACHE_NAME).then((cache)cache.add(OFFLINE_URL)));self.skipWaiting();});self.addEventListener(activate,(event){event.waitUntil(caches.keys().then((keys)Promise.all(keys.filter((key)key!CACHE_NAME).map((key)caches.delete(key)))));self.clients.claim();});self.addEventListener(fetch,(event){const urlnew URL(event.request.url);if(event.request.method!GET||event.request.mode!navigate||url.origin!self.location.origin||url.pathname.startsWith(/service-api/))return;event.respondWith(fetch(event.request).then((response){ if(![502,503,504].includes(response.status))returnresponse;returncaches.match(OFFLINE_URL).then((cached)cached||response);}).catch(()caches.match(OFFLINE_URL).then((cached)cached||Response.error())));});二、配置 HTTPSService Worker 必须运行在 HTTPS 环境下。在宝塔中进入网站 → 你的域名 → SSL申请并启用 SSL之后使用下面的地址访问网站https://你的域名/不要使用普通 HTTP 或服务器 IP 地址测试。三、添加宝塔自定义配置进入网站 → 你的域名 → 配置文件 → 自定义配置文件不要修改“主配置文件”将下面完整内容粘贴到“自定义配置文件”中location /sw.js { root /www/wwwroot/test.zhangsan.cool; default_type application/javascript; add_header Cache-Control no-cache always; add_header Service-Worker-Allowed / always; try_files $uri 404; } location /offline.html { root /www/wwwroot/test.zhangsan.cool; default_type text/html; add_header Cache-Control no-cache always; try_files $uri 404; } error_page 502 503 504 200 /offline.html; proxy_set_header Accept-Encoding ; sub_filter_types text/html; sub_filter_once on; sub_filter /body scriptif(serviceWorker in navigator){window.addEventListener(load,function(){navigator.serviceWorker.register(/sw.js,{scope:/}).then(function(r){console.log(Service Worker 注册成功,r.scope);return r.update();}).catch(function(e){console.error(Service Worker 注册失败,e);});});}/script/body;其中反向代理后端使用本机地址127.0.0.1:8080如果宝塔已经自动生成了反向代理配置不要重复添加第二个location ^~ /只添加上面的自定义内容即可。四、修改页面后更新缓存编辑修改offline.html或sw.js后需要修改sw.js中的缓存版本号例如constCACHE_NAMEoffline-contact-v4;然后重新上传文件联网刷新页面再测试断网。

相关新闻

奥晟博数控立式车床(立车)核心技术解析

奥晟博数控立式车床(立车)核心技术解析

2026/8/16 14:44:51

奥晟博数控立式车床(立车)核心技术解析与应用指南在重型机械加工领域,立式车床(立车)是加工大直径、短轴类及盘类零部件的关键设备。作为东北地区专业数控机床制造实体的奥晟博数控机床,其数控立车&#xf…

微信聊天记录永久保存方案:WeChatExporter开源导出工具完整上手指南(含避坑清单)

微信聊天记录永久保存方案:WeChatExporter开源导出工具完整上手指南(含避坑清单)

2026/8/16 14:34:50

微信聊天记录永久保存方案:WeChatExporter开源导出工具完整上手指南(含避坑清单) 【免费下载链接】WeChatExporter 一个可以快速导出、查看你的微信聊天记录的工具 项目地址: https://gitcode.com/gh_mirrors/wec/WeChatExporter 你有…

手机远程控制电脑实用指南,附花生壳远程控制对比

手机远程控制电脑实用指南,附花生壳远程控制对比

2026/8/16 14:34:50

灵活办公成常态,远程需求越来越高频据IDC《2026全球IT行业十大预测》相关分析,企业在智能化与远程协作工具上的投入仍在持续增加,灵活办公、异地协作已经成为常态化的工作方式之一。这也让“手机远程控制电脑”这个需求越来越高频——出差路上…

星穹铁道全自动托管终极指南:三月七小助手从安装到避坑的完整教程

星穹铁道全自动托管终极指南:三月七小助手从安装到避坑的完整教程

2026/8/16 15:44:53

星穹铁道全自动托管终极指南:三月七小助手从安装到避坑的完整教程 【免费下载链接】March7thAssistant 崩坏:星穹铁道全自动 三月七小助手 项目地址: https://gitcode.com/gh_mirrors/ma/March7thAssistant 晚上十一点半,你瘫在沙发上…

设置了 box-sizing: border-box; 不管用,下边框还是被挤压没了

设置了 box-sizing: border-box; 不管用,下边框还是被挤压没了

2026/8/16 15:44:53

div(height: 100vh; width: 100vw;)包裹一个 input (height: 100vh; width: 100vw;)input 设置了边框 border: 1px solid rgb(234, 11, 11); 但显示效果,下边框被挤没了,没有显示。设置了 box-sizing: bord…

Wells涡轮尾缘锯齿CFD气动性能分析

Wells涡轮尾缘锯齿CFD气动性能分析

2026/8/16 15:44:53

关键词:Wells涡轮;尾缘锯齿;ANSYS CFX;k-ω SST;气动性能 一、文章简要介绍 本文基于Energies期刊2022年发表的论文(Alkhalifa et al., DOI: 10.3390/en15239075),采用ANSYS CFX对带…

你的歌单不认别的播放器?10分钟用QMCDecode把QQ音乐加密文件转成通用格式

你的歌单不认别的播放器?10分钟用QMCDecode把QQ音乐加密文件转成通用格式

2026/8/16 15:44:53

你的歌单不认别的播放器?10分钟用QMCDecode把QQ音乐加密文件转成通用格式 【免费下载链接】QMCDecode QQ音乐QMC格式转换为普通格式(qmcflac转flac,qmc0,qmc3转mp3, mflac,mflac0等转flac),仅支持macOS,可自动识别到QQ音乐下载目录…

理性看待AI炒股:stock-trading-ml的预测局限性分析与未来改进路线图

理性看待AI炒股:stock-trading-ml的预测局限性分析与未来改进路线图

2026/8/16 15:44:53

理性看待AI炒股:stock-trading-ml的预测局限性分析与未来改进路线图 【免费下载链接】stock-trading-ml A stock trading bot that uses machine learning to make price predictions. 项目地址: https://gitcode.com/gh_mirrors/st/stock-trading-ml stock-…

autobloody 工作原理深度剖析:Dijkstra 算法如何帮你秒级找到最短提权路径

autobloody 工作原理深度剖析:Dijkstra 算法如何帮你秒级找到最短提权路径

2026/8/16 15:34:53

autobloody 工作原理深度剖析:Dijkstra 算法如何帮你秒级找到最短提权路径 【免费下载链接】autobloody Tool to automatically exploit Active Directory privilege escalation paths shown by BloodHound 项目地址: https://gitcode.com/gh_mirrors/au/autobloo…

【文章复现】非线性值迭代自适应动态规划(ADP):离散时间非线性系统的策略迭代自适应动态规划算法研究附Matlab代码

【文章复现】非线性值迭代自适应动态规划(ADP):离散时间非线性系统的策略迭代自适应动态规划算法研究附Matlab代码

2026/8/16 0:04:13

✅作者简介:热爱科研的Matlab仿真开发者,擅长毕业设计辅导、数学建模、数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。🍎 往期回顾关注个人主页:Matlab科研工作室👇 关注我领取海量matlab电子书和…

【双层规划,节点出清价,绿证交易,CVaR方法】两级电力市场环境下计及风险的省间交易商最优购电模型附Matlab代码

【双层规划,节点出清价,绿证交易,CVaR方法】两级电力市场环境下计及风险的省间交易商最优购电模型附Matlab代码

2026/8/16 0:04:13

✅作者简介:热爱科研的Matlab仿真开发者,擅长毕业设计辅导、数学建模、数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。🍎 往期回顾关注个人主页:Matlab科研工作室👇 关注我领取海量matlab电子书和…

隐式mpc+自适应mpc+时变mpc,线性时变模型预测控制附Simulink仿真

隐式mpc+自适应mpc+时变mpc,线性时变模型预测控制附Simulink仿真

2026/8/16 0:04:13

✅作者简介:热爱科研的Matlab仿真开发者,擅长毕业设计辅导、数学建模、数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。🍎 往期回顾关注个人主页:Matlab科研工作室👇 关注我领取海量matlab电子书和…

【文章复现】非线性值迭代自适应动态规划(ADP):离散时间非线性系统的策略迭代自适应动态规划算法研究附Matlab代码

【文章复现】非线性值迭代自适应动态规划(ADP):离散时间非线性系统的策略迭代自适应动态规划算法研究附Matlab代码

2026/8/16 0:04:13

✅作者简介:热爱科研的Matlab仿真开发者,擅长毕业设计辅导、数学建模、数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。🍎 往期回顾关注个人主页:Matlab科研工作室👇 关注我领取海量matlab电子书和…

【双层规划,节点出清价,绿证交易,CVaR方法】两级电力市场环境下计及风险的省间交易商最优购电模型附Matlab代码

【双层规划,节点出清价,绿证交易,CVaR方法】两级电力市场环境下计及风险的省间交易商最优购电模型附Matlab代码

2026/8/16 0:04:13

✅作者简介:热爱科研的Matlab仿真开发者,擅长毕业设计辅导、数学建模、数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。🍎 往期回顾关注个人主页:Matlab科研工作室👇 关注我领取海量matlab电子书和…

隐式mpc+自适应mpc+时变mpc,线性时变模型预测控制附Simulink仿真

隐式mpc+自适应mpc+时变mpc,线性时变模型预测控制附Simulink仿真

2026/8/16 0:04:13

✅作者简介:热爱科研的Matlab仿真开发者,擅长毕业设计辅导、数学建模、数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。🍎 往期回顾关注个人主页:Matlab科研工作室👇 关注我领取海量matlab电子书和…

摆脱论文困扰!盘点2026年全网爆红的的AI论文写作工具

摆脱论文困扰!盘点2026年全网爆红的的AI论文写作工具

2026/8/15 1:04:46

一天写完毕业论文在2026年已不再是天方夜谭。2026年最炸裂、实测能大幅提速的AI论文写作工具,覆盖选题构思、文献整理、内容生成、格式排版等核心场景,真正帮你高效搞定论文难题。 一、全流程王者:一站式搞定论文全链路(一天定稿首…

导师推荐!2026最新AI论文工具测评与实用推荐

导师推荐!2026最新AI论文工具测评与实用推荐

2026/8/15 10:10:27

2026年真正好用的AI论文工具,核心看生成的论文质量、低AI味、格式正确、学术适配四大指标。综合实测,千笔AI、ThouPen、豆包、DeepSeek、Grammarly 是当前最值得推荐的梯队,覆盖从免费到付费、从中文到英文、从文科到理工的全场景需求。 一、…

告别游戏崩溃:XCOM 2模组管理器的智能革命

告别游戏崩溃:XCOM 2模组管理器的智能革命

2026/8/14 19:35:14

告别游戏崩溃:XCOM 2模组管理器的智能革命 【免费下载链接】xcom2-launcher The Alternative Mod Launcher (AML) is a replacement for the default game launchers from XCOM 2 and XCOM Chimera Squad. 项目地址: https://gitcode.com/gh_mirrors/xc/xcom2-lau…