Nuxt JS reverse proxy load balancer on Laravel Forge

Updated: Feb 12, 2025

Nuxt JS reverse proxy load balancer on Laravel Forge

To set up a reverse proxy load balancer using Nuxt.js on Laravel Forge, you'll need to follow these steps:

  1. Create a new Laravel project on Laravel Forge:

    • Sign in to your Laravel Forge account.
    • Click on the "Create a New Site" button.
    • Fill in the required information such as the site name, domain, and database details.
    • Choose a server location and size.
    • Click on the "Create Site" button.
  2. Install Laravel and Nuxt.js on your new server:

    • SSH into your server using your preferred terminal or command prompt.
    • Run the following command to install Laravel: ssh your_server "sudo apt-get install -y apache2 php php-mbstring php-xml php-zip unzip git".
    • Run the following command to install Composer: ssh your_server "curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer".
    • Clone your Laravel project repository and navigate to the project directory: git clone your_repository_url && cd your_project_name.
    • Install Laravel using Composer: ssh your_server "composer install".
    • Install Nuxt.js using npm: ssh your_server "npm install --save nuxt express".
  3. Configure Laravel and Nuxt.js:

    • Create a new Nuxt.js application in the resources/js directory: ssh your_server "cd resources/js && nuxt init".
    • Update the .env file in the root directory of your Laravel project with your database and application keys.
    • Update the web.php file in the routes directory of your Laravel project to serve the Nuxt.js application:
    Route::get('/', function () {
        return view('welcome');
    });
    
    Route::get('/{*any}', function () {
        return view('welcome');
    });
    
    Route::get('/api/{any}', function () {
        return response()->file(base_path('resources/js/nuxt/server.js'));
    });
    
    • Update the server.php file in the root directory of your Laravel project to start Nuxt.js:
    <?php
    
    it('serves the Nuxt application', function () {
        $this->get('/')
            ->assertStatus(200);
    });
    
    it('routes API requests to Nuxt', function () {
        $this->get('/api/user')
            ->assertStatus(200);
    });
    
    it('passes the request to Nuxt', function () {
        $response = $this->get('/api/user');
    
        $response->assertHeaderType('application/json');
    });
    
    it('triggers Nuxt middleware', function () {
        $this->get('/api/user')
            ->assertSeeText('Hello User');
    });
    
    it('can handle Inertia requests', function () {
        $response = $this->get('/register');
    
        $response->assertStatus(200);
    });
    
    it('can handle Inertia components', function () {
        $response = $this->get('/register');
    
        $response->assertSee('Register');
    });
    
    it('can handle Inertia forms', function () {
        $response = $this->post('/register', [
            'name' => 'John Doe',
            'email' => 'john.doe@example.com',
            'password' => 'password',
        ]);
    
        $response->assertRedirect('/login');
    });
    
    it('can handle Inertia errors', function () {
        $this->post('/register', [
            'name' => 'John Doe',
            'email' => 'john.doe@example.com',
            'password' => '',
        ])->assertStatus(422);
    
        $response->assertJsonValidationErrors(['password']);
    });
    
    it('can handle Inertia redirects', function () {
        $response = $this->get('/login');
    
        $response->assertRedirect('/');
    });
    
    it('can handle Inertia authentication', function () {
        $this->post('/api/login', [
            'email' => 'john.doe@example.com',
            'password' => 'password',
        ])->assertJsonStructure([
             'token',
             'user' => [
                 'id',
                 'name',
                 'email',
             ],
         ]);
    
        $this->withHeaders([
            'Authorization' => 'Bearer ' . $response->json('token'),
        ])->get('/dashboard')
             ->assertStatus(200)
             ->assertSeeText('Dashboard');
    });
    
    it('can handle Inertia logging out', function () {
        $this->withHeaders([
            'Authorization' => 'Bearer ' . $response->json('token'),
        ])->post('/api/logout')
             ->assertStatus(204)
             ->assertEmpty(cookie('laravel_session'));
    });
    
    it('can handle Inertia middleware', function () {
        $this->get('/api/protected')
            ->assertStatus(401);
    
        $this->withHeaders([
            'Authorization' => 'Bearer ' . $response->json('token'),
        ])->get('/api/protected')
             ->assertStatus(200)
             ->assertSeeText('Protected');
    });
    
    it('can handle Inertia routes with parameters', function () {
        $this->get('/api/users/1')
            ->assertStatus(200)
            ->assertJsonPath('data.id', 1);
    });
    
    it('can handle Inertia routes with dynamic segments', function () {
        $this->get('/api/users/{id}/posts/{postId}')
            ->assertStatus(200)
            ->assertJsonPath('data.id', 1)
            ->assertJsonPath('data.post.id', 1);
    });
    
    it('can handle Inertia routes with query strings', function () {
        $this->get('/api/users?sort=name')
            ->assertStatus(200)
            ->assertJsonPath('data[0].name', 'John Doe');
    });
    
    it('can handle Inertia routes with multiple query strings', function () {
        $this->get('/api/users?sort=name&filter=active')
            ->assertStatus(200)
            ->assertJsonPath('data[0].name', 'John Doe')
            ->assertJsonPath('data[0].active', true);
    });
    
    it('can handle Inertia routes with pagination', function () {
        $this->get('/api/users?page=2')
            ->assertStatus(200)
            ->assertJsonPath('meta.current_page', 2)
            ->assertJsonPath('meta.last_page', 3)
            ->assertJsonPath('data[0].name', 'Jane Doe');
    });
    
    it('can handle Inertia routes with filtering', function () {
        $this->get('/api/users?filter=active')
            ->assertStatus(200)
            ->assertJsonPath('data[0].name', 'John Doe')
            ->assertJsonPath('data[0].active', true);
    });
    
    it('can handle Inertia routes with sorting', function () {
        $this->get('/api/users?sort=name')
            ->assertStatus(200)
            ->assertJsonPath('data[0].name', 'John Doe');
    });
    
    it('can handle Inertia routes with searching', function () {
        $this->get('/api/users?search=doe')
            ->assertStatus(200)
            ->assertJsonPath('data[0].name', 'John Doe')
            ->assertJsonPath('data[0].email', 'john.doe@example.com');
    });
    
    it('can handle Inertia routes with filtering and sorting', function () {
        $this->get('/api/users?filter=active&sort=name')
            ->assertStatus(200)
            ->assertJsonPath('data[0].name', 'John Doe