← Other topics

Run Laravel via a subdirectory on a Nginx server

Video Notes

The following Nginx site config can be used to run a Laravel application via a subdirectory (e.g. yourdomain.com/admin).

In this config you should replace:

  • server_name to your domain
  • References of php8.2-fpm with the version number of PHP that your server is running (check with php --version)
  • References to /admin with your desired subdirectory name
  • References /var/www/admin/public/ with the path to your application’s public directory.

Config

server {

    # Listen for incoming requests on port 80 (default for HTTP traffic)
    listen 80;

    # Also listen on port 80 but for IPv6 connections
    listen [::]:80;

    # Domain to listen for traffic on
    server_name yourdomain.com;

    # The directory to serve from when we visit the root domain
    root /var/www/main/public;

    # Default file to serve when requesting a directory
    index index.php index.html;

    # How to handle requests to the root location (/)
    # First try to serve the file or directory if found
    # If not, rewrite the request to index.php (Laravel’s main controller)
    # Note: This is only needed if your root domain is also serving a Laravel app
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # Handle requests for PHP files by passing them to PHP-FPM for processing
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    #
    # Configs specific to our Laravel app running in the /admin subdirectory
    #
    location /admin {
        # The alias directive allows for the replacement of the part of the path specified
        # in the location directive with the path defined in the alias directive.
        # i.e. makes it so location of /admin will alias to /var/www/admin/public
        alias /var/www/admin/public;

        # The try_files directive will first attempt to serve the requested URL path as a file,
        # then as a directory then as our @admin named location (see below)
        try_files $uri $uri/ @admin;

        location ~ \.php$ {
            fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME /var/www/admin/public/index.php;
        }
    }

    location @admin {
        # Take any requests that start with /admin/ and rewrite them to /admin/index.php (Laravel’s front controller)
        rewrite /admin/(.*)$ /admin/index.php last;
    }

    #
    # MISC
    #
    # Security headers to combat clickjacking and cross-site scripting attacks
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    location = /favicon.ico {
        access_log off; log_not_found off;
    }
    location = /robots.txt {
        access_log off; log_not_found off;
    }

    # When a 404 error occurs, don’t show default 404 error page, instead redirect to Laravel’s main controller
    # So that a Laravel-specific 404 error can be shown
    error_page 404 /index.php;

    # NGINX will add the Content-Type header with the charset=utf-8 parameter in HTTP responses,
    # which informs the client (like a web browser) that the server is sending data encoded in UTF-8.
    # This helps ensure that text in the HTTP response is properly displayed to the user.
    charset utf-8;
}

Summary of setting up a Nginx site config

Place config file in /etc/nginx/sites-available directory

Symbolically link to sites-enabled

> ln -s /etc/nginx/sites-available/myconfig /etc/nginx/sites-enabled

Check for any syntax issues:

> nginx -t

Restart Nginx to make changes take effect

> systemctrl restart nginx

As an alternative to running multiple Laravel apps on the same server via subdirectories you can use separate domains or subdomains. More details here: Multiple Laravel apps on a single Nginx server

If this info helped you out you can say thanks and support future content by clicking my Amazon affiliate link: https://amzn.to/3UtgnYk. If you make a purchase on Amazon within 24 hours of clicking the link I may receive a micro-commission and it costs you nothing extra. Any income from these sales goes directly to supporting me in making new videos and guides. Thank you for your support!

← Other topics