Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How can I use Traefik to manage subdomains in Laravel
    Laravel Service Provider


How can I use Traefik to manage subdomains in Laravel


To use Traefik to manage subdomains in Laravel, you need to configure Traefik to route requests based on the subdomain. Here are the steps:

Step 1: Configure Traefik

1. Create a Traefik Configuration File:

yaml
   version: '2.2'

   services:
     whoami:
       image: containous/whoami
       labels:
         - "traefik.http.routers.whoami.rule=Host(`whoami.docker.localhost`)"
         - "traefik.http.routers.whoami.tls=true"
         - "traefik.docker.network=web"
         - "traefik.http.services.whoami.loadbalancer.server.port=80"
   

2. Create a Docker Network:

bash
   docker network create web
   

3. Start Traefik:

bash
   docker-compose up -d
   

Step 2: Configure Laravel

1. Update the `routes/web.php` File:

php
   Route::domain('*.myapp.com')->group(function () {
       // Routes for subdomains
   });
   

2. Update the `RouteServiceProvider`:

php
   protected function mapWebRoutes()
   {
       Route::domain('*.myapp.com')->middleware('web')->namespace($this->namespace)->group(base_path('routes/web.php'));
   }
   

Step 3: Configure Nginx

1. Update the `nginx.conf` File:

nginx
   server {
       listen 80;
       server_name *.myapp.com;
       root /var/www/vhost;

       index index.php index.html index.htm;

       location / {
           try_files $uri $uri/ /index.php?_share=++;
       }

       location ~ \.php$ {
           try_files $uri =404;
           fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
           fastcgi_param SCRIPT_FILENAME $request_filename;
           include fastcgi_params;
       }
   }
   

By following these steps, you can set up Traefik to manage subdomains in Laravel.

Citations:
[1] https://juststeveking.uk/articles/laravel-subdomains-in-docker/
[2] https://community.traefik.io/t/multiple-domains-on-one-app/9367
[3] https://github.com/dipaksarkar/laravel-sail-with-traefik
[4] https://community.traefik.io/t/multiple-sites-domains/960
[5] https://laracasts.com/discuss/channels/general-discussion/connecting-multiple-subdomains-to-single-laravel-project