Server from subdirectory using nginx + vite #42576
-
I Have an application that must be run from a subdiretory using nginx and I use Vite as asset bundler. My app.blade.php <!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
@routes
@php
$manifest = json_decode(file_get_contents(public_path('build/manifest.json')), true);
@endphp
<script type="module" src="{{ asset('build/' . $manifest['resources/js/app.js']['file']) }}"></script>
<link rel="stylesheet" href="{{ asset('build/' . $manifest['resources/js/app.js']['css'][0]) }}">
@inertiaHead
</head>
<body>
@inertia
</body>
</html> My .env APP_NAME=Laravel
APP_ENV=local
APP_KEY=key
APP_DEBUG=true
APP_URL=http://app.localhost/laravel My RouteServiceProvider <?php
declare(strict_types=1);
namespace App\Providers;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
use function base_path;
class RouteServiceProvider extends ServiceProvider
{
public const HOME = '/';
public function boot(): void
{
$this->configureRateLimiting();
$this->routes(static function (): void {
Route::prefix('/laravel')->group(static function (): void {
Route::prefix('api')
->middleware('api')
->group(base_path('routes/api.php'));
Route::middleware('web')
->group(base_path('routes/web.php'));
});
});
}
protected function configureRateLimiting(): void
{
RateLimiter::for(
'api',
static fn (Request $request) => Limit::perMinute(60)->by($request->user()?->id ?: $request->ip()),
);
}
} My Vite Config import { defineConfig } from 'vite'
import { createVuePlugin } from 'vite-plugin-vue2'
import path from 'path'
export default defineConfig(({ command }) => ({
plugins: [
createVuePlugin()
],
base: command === 'serve' ? '' : '/build/',
publicDir: false,
cacheDir: 'storage/vite',
resolve: {
alias: {
'@': path.resolve(__dirname, 'resources', 'js'),
'@pages': path.resolve(__dirname, 'resources', 'pages'),
'@components': path.resolve(__dirname, 'resources', 'components'),
'@@': path.resolve(__dirname, 'resources'),
ziggy: path.resolve(__dirname, 'vendor', 'tightenco', 'ziggy', 'src', 'js', 'vue.js')
}
},
server: {
origin: 'http://localhost:3000'
},
build: {
manifest: true,
outDir: 'public/build',
rollupOptions: {
input: 'resources/js/app.js'
}
}
})) My nginx config server {
listen 80;
listen [::]:80;
server_name app.localhost;
root /home/lucas/atmon/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location /laravel {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
} If I change, for example, the JS script tag to |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
So. By using @fideloper config I was able to make my application work in a subdirectory but it has two problems.
Any ideas? |
Beta Was this translation helpful? Give feedback.
-
InertiaJs is causing this problem. |
Beta Was this translation helpful? Give feedback.
InertiaJs is causing this problem.
There is no fix for the moment, but there is a workaround.
Check this issue for more information:
inertiajs/inertia-laravel#359