Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aaronranard's avatar

subdomain map to subdirectory route in existing Laravel project?

I have an app setup at app.example.com, and I have a forum setup at app.example.com/forum.

Is it possible using nginx to be able to create a new subdomain forum.example.com and have that map all requests to the Laravel app as if it were prefixed with the /forum route? For example, forum.example.com/view/new-post would serve the same as the app.example.com/forum/view/new-post route.

Is this possible using only nginx, or is it even possible at all?

Thanks!

0 likes
9 replies
rumm.an's avatar

I also have a same question? Anybody?

tuneless's avatar

I serve multiple subdomains with Laravel with this code here:

RouteServiceProvider.php

  protected function mapSubdomainRoutes()
  {
    Route::group([
      'namespace' => $this->namespace,
      'domain' => '{subdomain}.affekt.de',
    ], function () {
      require base_path('routes/web.php');
    });
  }

But watch out you have an new argument then for your controllers. It's $subdomain. I remove this argument with a custom middleware. Because I don't want to add $subdomain argument to all my controllers.

$route->forgetParameter('subdomain'); will remove it.

<?php

namespace App\Http\Middleware;

use Closure;

class RemoveSubdomainArgs
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $route = $request->route();
        $route->forgetParameter('subdomain');
        return $next($request);
    }
}

Don't forget to add the new RemoveSubdomainArgs middleware to your HTTP kernel.

singh's avatar

@tuneless I am taking your approach because I am hosting my app on multiple physical servers with subdomains.

I got the following error. Trying to solve the same problem except that I am using apache.

include(/var/www/html/app/Http/Middleware/RemoveSubdomainArgs.php): Failed to open stream: Permission denied

tuneless's avatar

Ah and nginx has to serve all of your subdomains ;-)

And point it to the same Laravel webroot location.

Laravel will do the routing ;-)

aaronranard's avatar

@tuneless does that fix the changing of the actual routes that get served?

For example if I add

  protected function mapSubdomainRoutes()
  {
    Route::group([
      'namespace' => $this->namespace,
      'domain' => '{subdomain}.example.com',
    ], function () {
      require base_path('routes/web.php');
    });
  }

how does that know to map all requests from forum.example.com to the /forum route group?

For example, forum.example.com/view/new-post would serve the same as the app.example.com/forum/view/new-post route.

tuneless's avatar

Ah okay, so you change also the folder? Like

/view/new-post /forum/view/new-post

should return same view?

So first part for subdomains is done. Next step is to edit your routes/web.php.

tuneless's avatar

You can create to route files

  protected function mapAppRoutes()
  {
    Route::group([
      'namespace' => $this->namespace,
      'domain' => 'app.example.com',
    ], function () {
      require base_path('routes/app.php');
    });
  }
  protected function mapForumRoutes()
  {
    Route::group([
      'namespace' => $this->namespace,
      'domain' => 'forum.example.com',
    ], function () {
      require base_path('routes/forum.php');
    });
  }

and your route files do explicit their work.

'routes/app.php' with '/view/new-post'

'routes/forum.php' with '/forum/view/new-post'

d3xt3r's avatar

Use proxy_pass to redirect forum.example.com/* to app.example.com/forum/$1 ...

aaronranard's avatar
aaronranard
OP
Best Answer
Level 2

For those who are looking for an answer, this is what I did solely using nginx,

in forum.example.com nginx config:

location / {
        proxy_set_header Host app.example.com;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass_request_headers on;
        proxy_pass_request_body on;
        proxy_pass_header http;
        proxy_pass http://localhost:8001/forum/;

        proxy_redirect off;
    }

Then the things I had to add to my app.example.com nginx config was:

server {
    # for the forum.example.com proxy_pass
    listen 8001 default_server;
...
}
location ~ \.php$ {

        set_real_ip_from 127.0.0.1/32;
        real_ip_header X-Forwarded-For;
}
location ~* \.(eot|ttf|woff|woff2)$ {
        add_header Access-Control-Allow-Origin forum.example.com;
}

The set_real_ip passes the user's IP along, otherwise the IP that will get stored for them is the IP of the server

The one thing to note for sharing sessions between subdomains is you will have to set the SESSION_DOMAIN in your .env like so:

SESSION_DOMAIN=.example.com

Please or to participate in this conversation.