To configure Nginx for a Laravel application located within a subfolder, a location
block is required to handle requests to that specific subfolder. This configuration ensures that Nginx correctly serves the Laravel application from its public
directory within the subfolder.
Here is a basic Nginx configuration example for a Laravel application in a subfolder named
subapp
:server {
listen 80;
server_name your_domain.com; # Replace with your domain
root /var/www/html; # Root for your main website or other content
index index.html index.htm index.php;
# Configuration for the Laravel subfolder
location /subapp {
alias /var/www/subapp/public; # Path to your Laravel application's public directory
try_files $uri $uri/ @subapp; # Handle requests within the subapp
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; # Adjust PHP-FPM socket path
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
# Named location for Laravel's index.php within the subapp
location @subapp {
rewrite /subapp/(.*)$ /subapp/index.php?/$1 last;
}
# Default PHP handling for other parts of your website (if any)
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; # Adjust PHP-FPM socket path
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# Deny access to .env files and other sensitive directories
location ~ /\.env {
deny all;
}
location ~ /\.ht {
deny all;
}
}
Explanation:
alias /var/www/subapp/public;
: This directive tells Nginx to serve files for requests to/subapp
from the specified path, which is thepublic
directory of your Laravel application.try_files $uri $uri/ @subapp;
: This attempts to serve the requested URI, then a directory with that URI, and finally, if neither is found, it uses the named location@subapp
.
- This nested location block handles PHP requests specifically for the Laravel application in the subfolder.
fastcgi_pass
: Specifies the address of your PHP-FPM service. Adjust the socket path to match your PHP version.fastcgi_param SCRIPT_FILENAME $request_filename;
: This is crucial for subfolder setups as it ensures PHP-FPM correctly identifies the script to execute within the aliased directory.
rewrite /subapp/(.*)$ /subapp/index.php?/$1 last;
: This named location is used bytry_files
to rewrite requests to the Laravelindex.php
file, passing the original request path as a query string.
No comments:
Post a Comment