Host Multiple Laravel Project on Same Port in Nginx Server

Multiple web projects can be deployed in Nginx web server under virtual host. In this way domain or ip address can be different. UserĀ  can those projects using different url address like below

example.com
subdomain.example.com
domain.com
testdomain.org


Sometimes we have to face some situations where it's needed to deploy multiple projects on same port so that, people can access those site using same URL. But their path are different from each other.

example.com
example.com/project1
example.com/project2
example.com/project2

Let's try to implement this using Nginx. First we will break down whole Nginx configuration into smaller parts and finally integrate those parts in a complete Nginx configuration.

Let's Create Basic Nginx Configuration

server {
    # Listing port and host address
    # If 443, make sure to include ssl configuration for the same.
    listen 80;
    listen [::]:80;

    server_name 192.168.0.132;

    index index.php;

    root /var/www/html/mainproject/public;

    # Handle main root / mainproject
    location / {
        #deny all;
        try_files $uri $uri/ /index.php?$args;
    }

    # via factcgi PHP-FPM unix socket
    location ~ \.php$ {
        
        set $newurl $request_uri;

        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param REQUEST_URI $newurl;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }

    # Deny .ht* access
    location ~ /\.ht {
        deny all;
    }
}

Create location block for project 1

location /project1 {
        # Root for this project
        root /var/www/html/project1/public;

        # Rewrite $uri=/project1/xyz back to just $uri=/xyz
        rewrite ^/project1/(.*)$ /$1 break;

        # Try to send static file at $url or $uri/
        # Else try /index.php (which will hit location ~\.php$ below)
        try_files $uri $uri/ /index.php?$args;
    }

Create location block for project 2

location /project2 {
        # Root for this project
        root /var/www/html/project2/public;

        # Rewrite $uri=/project2/xyz back to just $uri=/xyz
        rewrite ^/project2/(.*)$ /$1 break;

        # Try to send static file at $url or $uri/
        # Else try /index.php (which will hit location ~\.php$ below)
        try_files $uri $uri/ /index.php?$args;
    }


Below two if block should be added in configuration those will handle request according to URI
if ($newurl ~ ^/project1(.*)$) {
	set $newurl $1;
	root /var/www/html/project1/public;
}

if ($newurl ~ ^/project2(.*)$) {
	set $newurl $1;
	root /var/www/html/project2/public;
}


Final Configuration will be like this
# Nginx.conf
# Project 1(Path: /var/www/html/mainproject, Url: http://192.168.0.132)
# Project 2(Path: /var/www/html/project1, Url: http://192.168.0.132/project1)
# Project 3(Path: /var/www/html/project2, Url: http://192.168.0.132/project2)

server {
    # Listing port and host address
    # If 443, make sure to include ssl configuration for the same.
    listen 80;
    listen [::]:80;

    server_name 192.168.0.132;

    # Default index pages
    index index.php;

    # Root for / shipment
    root /var/www/html/mainproject/public;

    # Handle main root / mainproject
    location / {
        #deny all;
        try_files $uri $uri/ /index.php?$args;
    }

    # Handle project1 project, just replicate this section for further projects app3, app4
    # by just replacing project1 with appropriate tag(project1 or project2 or project3)

    location /project1 {
        # Root for this project
        root /var/www/html/project1/public;

        # Rewrite $uri=/project1/xyz back to just $uri=/xyz
        rewrite ^/project1/(.*)$ /$1 break;

        # Try to send static file at $url or $uri/
        # Else try /index.php (which will hit location ~\.php$ below)
        try_files $uri $uri/ /index.php?$args;
    }

    location /project2 {
        # Root for this project
        root /var/www/html/project2/public;

        # Rewrite $uri=/project2/xyz back to just $uri=/xyz
        rewrite ^/project2/(.*)$ /$1 break;

        # Try to send static file at $url or $uri/
        # Else try /index.php (which will hit location ~\.php$ below)
        try_files $uri $uri/ /index.php?$args;
    }

    # Handle all locations *.php files (which will always be just /index.php)
    # via factcgi PHP-FPM unix socket
    location ~ \.php$ {
        # At this point, $uri is /index.php, $args=any GET ?key=value and $request_uri = /project1/xyz.
        # But we don't want to pass /project1/xyz to PHP-FPM, we want just /xyz to pass to fastcgi REQUESTE_URI below.
        # This allows laravel to see /project1/xyz as just /xyz in its router.
        # So laravel route('/xyz') responds to /project1/xyz as you would expect.
        set $newurl $request_uri;

        if ($newurl ~ ^/project1(.*)$) {
                set $newurl $1;
                root /var/www/html/project1/public;
        }

        if ($newurl ~ ^/project2(.*)$) {
                set $newurl $1;
                root /var/www/html/project2/public;
        }

        # Pass all PHP files to fastcgi php fpm unix socket
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # Use php fpm sock which is installed on your machine like php7.2, php5.6
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        # Here we are telling php fpm to use updated route that we've created to properly
        # response to laravel routes.
        fastcgi_param REQUEST_URI $newurl;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }

    # Deny .ht* access
    location ~ /\.ht {
        deny all;
    }
}
You can now access project in browser by
http://192.168.0.132
http://192.168.0.132/project1
http://192.168.0.132/project2

If you feel that , you are benefitted from this blog. Please do bookmark this website and you are always welcome

Tags: