Multiple Laravel installs in subfolders - Nginx rewrite rules, full guide

Last updated: May 25th 2021


This guide explains how to run multiple Laravel installs on your server in subfolders of your domain (or IP). Sometimes, as a developer, you need to quickly deploy Laravel app without creating a subdomain, having to generate new SSL Certificates or opening new ports and configuring a firewall.

Please note: These instructions are based on the Webdock Perfect Server Stacks for Nginx. It will work on any NGINX configuration with slight changes, depending on your server setup.

Basic Nginx setup for Laravel app

You will need to install/set-up a Laravel app which resides in the default html folder /var/www/html , and responds to your server domain name (eg. or IP (x.x.x.x)

Things are pretty much straight forward here. The only change we have to make to get things working is to point the web-server root directive to the Laravel public folder. 

In the Webdock Control Panel click on the small pencil icon next to your Web Root in your server overview screen,  and change the web root to  /var/www/html/public;

If you need to do this manually, edit your Nginx configuration, typically found at /etc/nginx/sites-available/webdock, and edit the following line to:

root /var/www/html/public;

Don`t forget to restart Nginx if you do this manual update:

# sudo systemctl restart nginx

Laravel app in subdirectory

What we try to achieve here is to run the Laravel application in a subdirectory of the main domain. For example:

For the matter of semantics and good practice, we will place our second Laravel install in the folder: /var/www/second

Add this code to your Nginx configuration file, typically found at /etc/nginx/sites-available/webdock, at the bottom of the server block:

  # SECOND location rewrite instructions
    location /second {
        alias /var/www/second/public;
        try_files $uri $uri/ @rewsecond;

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

    location @rewsecond {
        rewrite /second/(.*)$ /second/index.php?/$1 last;
 # end of the SECOND location   

Please note the version number for php-fpm in the fastcgi_pass directive. If you are using a different version of php-fpm you will need to adjust the version number here, otherwise you will get a 502 Bad Gateway error from Nginx.

We introduced here an alias directive for the path instead of root inside location block. Although they seem to do the same job, there is some crucial differences regarding the path building algorithm they utilize. In the case of the root directive, a full path is appended to the root including the location part, whereas in case of the alias directive, only the portion of the path NOT including the location part is appended to the alias

Also, we used a named location for our rewrite @rewsecond. You can change the named location to anything suits you, but keep in mind to describe right location block for a rewrite, since named location MUST BE outside any other location block you may have or refer and can potentially create confusion later.

Don`t forget to restart Nginx.

# sudo systemctl restart nginx

Multiple Laravel installations in dynamic subdirectories

In this section we are going to deploy Laravel applications in dynamic folders eg. /apps/__APP_NUMBER__

Although this approach may be unorthodox in many ways, this part will complete our guide to installing Laravel applications in subfolders/subdirectories.


We want to configure Nginx rewrite rules for multiple Laravel installations stored in /var/www/apps/_APP_NO_/ (_APP_NO_ is numeric eg. 1001, 1002, 1003, ... and represent folders with separate Laravel installs )

/var/www/apps/1001/ ->
/var/www/apps/1002/ ->
/var/www/apps/1003/ ->


Add this code to your Nginx configuration file (right after the first location block)

Detailed explanation of what we have done here exceeds the scope of this article so we will leave you to check and experiment further. Capturing variables from regex expression definitely help us  solve this complex problem. Moreover, explaining it on Laravel installs can give us a clue how can be done with less or more demanding tasks.

So, this is how should look like our default Webdock Nginx file extended with all examples:

You should not copy/paste this Nginx configuration wholesale without reading and understanding it first.


Laravel is a most popular PHP framework, and Nginx web server with reverse proxy capabilities is a natural choice. Sometimes, Nginx complex rewrite rules do not seem very user friendly, but once mastered, they will unleash full potential of your Webdock server stack.

Please be informed, the main goal of this article is to avoid common setbacks related to Laravel installs in different subdirectories and how Nginx should handle these. In production, your server config file definitely needs security, caching and redirect settings which this guide is not aimed at providing.

Author: Aleksandar Milivojevic is a PHP programmer for over 10 years (Laravel and Wordpress). He writes articles between projects, to help out the community and  in order to popularize PHP in general.

Related articles

We use cookies. Please see our Privacy Policy.