Run Laravel in a Subdirectory in Nginx
This is a remake of the original video/article found here.
That article explains things pretty well (as does the Youtube video here, but if you like words better than video, check that out!.
Here you'll find only the tl;dr version.
The Nginx Config
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/top/public;
index index.html index.htm index.php;
server_name _;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /nested {
alias /var/www/nested/public;
try_files $uri $uri/ @nested;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
}
}
location @nested {
rewrite /nested/(.*)$ /nested/index.php?/$1 last;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
}
}
The main things to care about:
- We use an
alias
in the/nested
block - We need to override
SCRIPT_FILENAME
when sending requests sent to/nested/*
to PHP-FPM - We use a rewrite to correctly inform Nginx that the request should be treated as a request to index.php