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

在IIS部署Nuxt与Laravel API后,代理URL返回404问题求助

Nuxt代理返回404问题排查与解决
  • 先确认目标API本身能访问
    在服务器上用浏览器或curl工具直接访问http://hrmapis.mygwambo.com,比如请求一个具体接口(如http://hrmapis.mygwambo.com/api/users),先排除Laravel API自身不可用的情况。

  • 检查代理路径重写是否匹配Laravel路由
    当前配置里pathRewrite: {'^/api/': ''}会把Nuxt端的/api/xxx转发成http://hrmapis.mygwambo.com/xxx。如果你的Laravel API路由是带/api前缀的(比如默认api.php里的路由组都加了/api前缀),那这个重写就会导致请求地址错误,直接返回404。

    修正方法:如果Laravel路由带/api前缀,要么删掉pathRewrite配置,要么改成pathRewrite: {'^/api/': '/api/'}。示例:

    proxy: {
      '/api/': { 
        target: 'http://hrmapis.mygwambo.com',
        // pathRewrite: {'^/api/': ''}, // 若Laravel路由带/api前缀,注释或修改这行
        changeOrigin: true
      }
    },
    
  • 确认IIS的反向代理组件已启用
    打开IIS管理器,查看服务器节点下的“模块”,必须确保URL Rewrite和Application Request Routing (ARR)这两个模块已安装并启用。如果没有,通过Web平台安装器添加这两个组件,ARR是IIS实现反向代理的核心。

  • 配置IIS的URL重写规则
    Nuxt作为SPA需要路由重写,同时要确保代理请求能正确转发到Laravel。在站点的web.config中添加以下规则:

    <rewrite>
      <rules>
        <!-- 处理Nuxt SPA路由,避免刷新页面404 -->
        <rule name="Nuxt SPA" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="index.html" />
        </rule>
        <!-- 转发/api开头的请求到Laravel API -->
        <rule name="Proxy to API" stopProcessing="true">
          <match url="^api/(.*)" />
          <!-- 若Laravel路由带/api前缀,这里改成http://hrmapis.mygwambo.com/api/{R:1} -->
          <action type="Rewrite" url="http://hrmapis.mygwambo.com/{R:1}" />
          <serverVariables>
            <set name="HTTP_ACCEPT_ENCODING" value="" />
          </serverVariables>
        </rule>
      </rules>
    </rewrite>
    
  • 核对Nuxt请求写法
    确保项目中发起请求时用的是$axios.get('/api/xxx')这种相对路径,不要直接写死目标API的URL,否则代理不会生效。

  • 开启代理日志排查转发地址
    在Nuxt的proxy配置中添加logLevel: 'debug',启动项目后查看控制台日志,确认实际转发的URL是否符合预期,比如是否正确拼接了路径:

    proxy: {
      '/api/': { 
        target: 'http://hrmapis.mygwambo.com',
        pathRewrite: {'^/api/': ''},
        changeOrigin: true,
        logLevel: 'debug'
      }
    },
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 16:50:23