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

Laravel 11 + Vite公网部署后资源加载404问题求助

解决Laravel 11 + Vite静态资源404(net::ERR_ABORTED 404)问题

核心问题是反向代理的子目录/local-project未被Vite和Laravel正确识别,导致静态资源路径缺失前缀。以下是针对性修复步骤:

1. 配置Vite的资源路径前缀

修改vite.config.js,添加base和server.origin,强制资源路径带上/local-project前缀:

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';

export default defineConfig({
    plugins: [
        laravel({
            input: ['resources/css/app.css', 'resources/js/app.js'],
            refresh: true,
        }),
    ],
    base: '/local-project/',
    server: {
        host: '本地IP',
        port: 8181,
        origin: 'https://sub.domain.nusa.net.id/local-project',
    },
});

2. 修正Laravel路由与代理配置

在bootstrap/app.php中添加路由前缀,并确保信任代理:

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
        prefix: '/local-project',
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->trustProxies(at: '*');
    })
    ->withExceptions(function (Exceptions $exceptions) {
        // 保留原有异常配置
    })->create();

3. 确保环境变量与缓存同步

  • 确认.env中的APP_URL正确:
    APP_URL=https://sub.domain.nusa.net.id/local-project
    
  • 清除配置缓存:
    php artisan config:clear
    php artisan cache:clear
    

4. 重新构建/启动资源服务

  • 生产环境构建资源:
    npm run build
    
  • 开发环境启动热更新:
    npm run dev
    
  • 重启Laravel服务:
    php artisan serve --host=本地IP --port=8181
    

为什么之前的操作无效?

  • 仅修改APP_URL但未配置Vite的base和origin,导致Vite生成的资源路径仍为根路径;
  • 未给Laravel路由添加前缀,系统无法识别/local-project下的请求归属,资源路由匹配失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 15:22:07