laravelroutes

How I resolve different routes on an application using same url but different port?


In my case I want to create a small monolith but:

I am using laravel 11 and the route config resides on different files:

But how I can achieve this.


Solution

  • A solution to this problem is to configure it via bootstrap/app.php:

    use Illuminate\Foundation\Application;
    use Illuminate\Foundation\Configuration\Exceptions;
    use Illuminate\Foundation\Configuration\Middleware;
    use Illuminate\Support\Facades\Route;
    
    
    return Application::configure(basePath: dirname(__DIR__))
        ->withRouting(
            commands: __DIR__.'/../routes/console.php',
            using: function (){
                $adminPort = (int)env('ADMIN_PORT',8334);
                $routerFile = (int)request()->getPort() === $adminPort?'routes/admin.php':'routes/web.php';
    
                Route::middleware('web')
                    ->group(base_path($routerFile));
            },
        )
        ->withMiddleware(function (Middleware $middleware) {
            //
        })
        ->withExceptions(function (Exceptions $exceptions) {
            //
        })->create();
    
    

    As you can see I am using ADMIN_PORT from .env:

    ADMIN_PORT=8443
    

    And upon:

    $routerFile = (int)request()->getPort() === $adminPort?'routes/admin.php':'routes/web.php';
    

    As you can see i removed the default config upon withRouting and I am using callback in order to resolve it.

    The url is the same but now the application required 2 ports to served with:


    Webserver config

    If using nginx, a reccomended vhost configuration is:

    server {
            listen 80 default;
            server_name  _;
    
            return 301 https://$host$request_uri;
        }
    
        server {
            listen 443 ssl;
            listen 8443 ssl;
            server_name _;
    
            root /var/www/html/public;
            index index.php;
    
            ssl_certificate /etc/nginx/ssl/www.crt;
            ssl_certificate_key /etc/nginx/ssl/www.key;
    
            location / {
                try_files $uri $uri/ /index.php?$query_string;
            }
    
            location ~ \.php$ {
                include   fastcgi_params;
    
                fastcgi_param PATH_INFO  $fastcgi_path_info;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    
                fastcgi_pass php_app:9000;
                
                fastcgi_buffer_size 128k;
                fastcgi_buffers 4 256k;
                fastcgi_busy_buffers_size 256k;
                fastcgi_read_timeout 300s;
            }
        }
    

    At config above you can set server_name with your domain if multiple domains used on a single server. Also check if default is needed as well in your case.

    Pay attention that for the same vhost I listen both into 443 and 8443:

    listen 443 ssl;
    listen 8443 ssl;
    

    Thus The same application for the same domain will be served upoin same url defined in server_name. Also keep in mind to config the keys accorditly as well.