nginxpublish

nginx部署相关问题

# 1. TypeError: Failed to execute 'compile' on 'WebAssembly': Incorrect response MIME type. Expected 'application/wasm'.

  1. 打开 nginx.conf 配置文件
  2. 进入 http 块,添加以下类型声明:
http {
    types {
      application/wasm wasm;
    }
}

open-im 账号为我和刘芝旺的; 密码:text01

export const WS_URL = "ws://18.167.120.52:10001"; export const API_URL = "http://18.167.120.52:10002"; export const USER_URL = "http://18.167.120.52:10008";

Nginx 在线配置 https://www.digitalocean.com/community/tools/nginx?domains.0.php.php=false&domains.0.routing.index=index.html&domains.0.routing.fallbackHtml=true&global.app.lang=zhCN

# 2. 域名解析

图片描述

# 3. 发版后还要手动清浏览器缓存,待验证


server {
    listen 80;
    server_name your.domain.com; # 替换为你的域名
    root /usr/share/nginx/html; # 替换为你的项目根目录

    # 规则1:HTML 文件 - 永不缓存
    # 这是最关键的一步,确保浏览器总是获取最新的入口文件。
    location = /index.html {
        add_header Cache-Control "no-cache, no-store, must-revalidate";
        add_header Pragma "no-cache";
        add_header Expires "0";
    }

    # 规则2:带 Hash 的静态资源 - 永久缓存
    # 文件名中的 Hash 确保了内容变化时文件名也会变化,所以可以放心地让浏览器永久缓存。
    # `immutable` 告诉浏览器这个文件内容永远不会变,连校验请求都无需发送。
    location ~* \.[a-f0-9]{8}\.(css|js)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
    }

    # 规则3:其他静态资源(如图片、字体) - 长期缓存
    # 这些文件通常不带 Hash,但也不常变动,可以设置一个较长的缓存时间。
    location ~* \.(jpg|jpeg|png|gif|ico|svg|woff|woff2|ttf)$ {
        expires 30d;
        add_header Cache-Control "public";
    }

    # 规则4:单页应用(SPA)路由处理
    # 这是保证 React/Vue 等路由正常工作的关键。
    # 重要的是,它会将所有未匹配到具体文件的请求都交由 index.html 处理。
    # 由于我们已为 /index.html 单独设置了不缓存规则,所以这里是安全的。
    location / {
        try_files $uri $uri/ /index.html;
    }
}

# 3.1 配置解读

  • location = /index.html:使用 = 精确匹配 /index.html,并强制其不被任何一方缓存。这是整个策略的核心。
  • location ~* .[a-f0-9]{8}.(css|js)$:通过正则表达式匹配所有带 8 位 Hash 的 JS 和 CSS 文件,并设置长达一年的缓存(1y)和 immutable 属性,实现最佳性能。
  • location /:作为最后的 fallback,处理 SPA 的前端路由,将所有页面导航都指向不缓存的 index.html。
上次更新: