Skip to content

Instantly share code, notes, and snippets.

@yidas
Last active September 21, 2023 03:49
Show Gist options
  • Save yidas/37c442111a0922f61b14b41877503c63 to your computer and use it in GitHub Desktop.
Save yidas/37c442111a0922f61b14b41877503c63 to your computer and use it in GitHub Desktop.
Laravel Server Configuration for Nginx & Apache (Subdirectory)

Laravel Server Configuration for Nginx & Apache (Subdirectory)

It's easy to configurate a Laravel server site with directory protection:

Laravel Web Server Configuration

Sub Directory Site Application

Laravel smartly detects the current base url so that you don't need to set the base url for subdirectoy:

// All Url path would return full URL with current path
url('/');
url('');

Nginx Configuration

Basic Subdirectory

With same path included /public, for example: /sublara/

##
# Subdirectory Laravel Application Configuration
##
# App directories protection
location /sublara/ {                                                 
    return 403;                                                                           
}       
# Laravel public
location /sublara/public/ {                                          
    try_files $uri $uri/ /sublara/public/index.php?$query_string;
}    

After above setting:

  • Access Laravel web: //hostname/sublara/public/
  • Protect Laravel app directories: //hostname/sublara/ (/sublara/composer.json)

Protected Subdirectory

Using alias to implement different subdiretory pointed to specified project path:

location /site2/ {
    
    alias /srv/www/project2/;
    
    # Pretty URI trick
    try_files $uri $uri/ /site2//site2/index.php?$query_string;
    
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        # Apply the subdirectory base path to PHP script
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
}

try_files with trick


Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment