You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:将Vue Nuxt.js应用部署至Windows Server的IIS子站点site.com/app3

解决方案:Windows Server 2019 IIS 10部署Nuxt/Vue子应用site.com/app3

1. 配置Nuxt/Vue应用基础路径

这是解决资源加载和路由问题的核心,必须确保应用构建时所有路径都带上/app3/前缀:

  • Nuxt 2:修改nuxt.config.js
export default {
  router: {
    base: '/app3/'
  }
}
  • Nuxt 3:修改nuxt.config.ts
export default defineNuxtConfig({
  app: {
    baseURL: '/app3/'
  }
})
  • 纯Vue项目:
    • 修改vue.config.js(Vite项目改vite.config.js)
    module.exports = {
      publicPath: '/app3/'
    }
    
    • 修改路由配置文件router/index.js
    const router = new VueRouter({
      base: '/app3/',
      routes: [...]
    })
    

修改后重新构建项目,确保dist(或Nuxt3的.output/public)目录下的资源路径均包含/app3/前缀。

2. 修正IIS URL重写规则

不要用简单重定向,改用反向代理+SPA fallback规则,确保所有请求正确转发且路由刷新正常:

在主站点的web.config(或app3子应用独立的web.config)中添加以下配置:

<system.webServer>
  <rewrite>
    <rules>
      <!-- 反向代理:将/app3/*请求转发到3000端口,保留路径参数 -->
      <rule name="Proxy to App3" stopProcessing="true">
        <match url="^app3/(.*)" />
        <action type="Rewrite" url="http://localhost:3000/{R:1}" />
        <serverVariables>
          <set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="{HTTP_ACCEPT_ENCODING}" />
          <set name="HTTP_ACCEPT_ENCODING" value="" />
        </serverVariables>
      </rule>
      <!-- SPA路由 fallback:解决刷新页面404问题 -->
      <rule name="App3 SPA Fallback" stopProcessing="true">
        <match url="^app3/.+" />
        <conditions logicalGrouping="MatchAll">
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        </conditions>
        <action type="Rewrite" url="/app3/" />
      </rule>
    </rules>
  </rewrite>
  <urlCompression doStaticCompression="true" doDynamicCompression="true" />
</system.webServer>

如果将app3作为IIS子应用创建,需确保子应用物理路径指向项目构建后的输出目录,且主站点已绑定SSL证书(子应用会自动复用主站点的SSL配置,无需单独绑定)。

3. IISNode托管配置(可选)

若直接通过IISNode托管Nuxt服务(而非独立运行3000端口),调整web.config如下:

<system.webServer>
  <iisnode node_env="production" nodeProcessCommandLine="node node_modules/nuxt/bin/nuxt.js start" />
  <rewrite>
    <rules>
      <rule name="Nuxt IISNode Proxy" stopProcessing="true">
        <match url="app3/*" />
        <action type="Rewrite" url="app3/index.js" />
      </rule>
    </rules>
  </rewrite>
  <handlers>
    <add name="iisnode" path="app3/index.js" verb="*" modules="iisnode" />
  </handlers>
</system.webServer>

4. 验证与排查

  • 检查静态资源路径:访问site.com/app3,查看浏览器控制台中JS/CSS/图片的请求路径是否为site.com/app3/xxx,若仍为根路径则说明基础路径配置未生效。
  • 测试路由刷新:访问site.com/app3/xxx后刷新页面,确认不会出现404错误。
  • SSL复用验证:直接用https://site.com/app3访问,确认证书正常生效(主站点SSL绑定site.com域名即可覆盖所有子路径)。

内容的提问来源于stack exchange,提问作者Bruno Tourinho

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 22:50:19