Laravel in a subdirectory: the nginx server block
Serve a Laravel project under /shop with one prefix location that aliases its public folder, a nested PHP location and a named fallback to its index.php.
A post about this problem was printed at this address on . The text below is new, written on 2026-11-25 from the official documentation linked in it.
Two Laravel projects under one host name, one at /shop and one at /blog, need three things each: an alias to the project's public folder, a PHP location that knows the aliased path, and a fallback that sends every other address to that project's index.php.
The block below was put together from the nginx and Laravel documentation linked in it. nginx was not run for this page, so test it on your machine with sudo nginx -t and one request per line of the checklist.
server {
listen 80;
server_name example.com;
root /srv/www;
index index.php;
location = /shop { return 301 /shop/; }
location ^~ /shop/ {
alias /srv/shop/public/;
try_files $uri $uri/ @shop;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
}
location @shop {
rewrite ^/shop/(.*)$ /shop/index.php last;
}
}For the second project, copy the three locations and change the name in five places: both prefixes, the alias, the named location and the rewrite.
Why each line is there
alias and not root. With root, nginx appends the whole address to the path, so /shop/css/app.css is looked up in /srv/shop/public/shop/css/. The alias directive replaces the part that matched. Keep the trailing slash on both the location and the alias.
$request_filename and not $document_root$fastcgi_script_name. Most copied PHP locations use the second form. Under an alias it contains /shop twice over, and PHP-FPM answers "File not found" or "Primary script unknown". The same nginx page describes $request_filename as the file path based on the root or alias and the request.
The nested PHP location. A PHP location outside the prefix block would use the server's root. ^~ keeps nginx from considering regular expression locations outside the block once this prefix has matched.
rewrite ... last in a named location. Every address that is not a file restarts at /shop/index.php, and that request goes through the nested location. The query string is kept, because nginx appends the old arguments to the new address unless it ends in a question mark.
The folder that is published is public and nothing above it. Laravel's deployment page is blunt about this: serving the application from the project root exposes configuration files to the internet.
What to change in Laravel
Set APP_URL in the project's .env to the full address with the directory, for example APP_URL=https://example.com/shop. URL generation uses the current request where there is one, but queue workers and commands have no request to look at and rely on this value. Rebuild the configuration cache afterwards with php artisan config:cache.
Two projects on one host also share a cookie jar. Give each one its own SESSION_COOKIE, and set SESSION_PATH to its directory, otherwise logging in to one project can end the session in the other.
Front-end files built with a root address of / will be requested from /build/... and not from /shop/build/.... Set the asset address to include the directory before you build.
Checklist
| Request | Expected |
|---|---|
/shop | 301 to /shop/ |
/shop/ | The start page of the project |
/shop/robots.txt | The static file |
/shop/any/route | A Laravel route, or Laravel's own 404 page |
/shop/.env | Not the file |
A 502 on the PHP requests means that the socket path is wrong, and the error log line says which path was tried. nginx's own 404 page on a route, in place of Laravel's, means the fallback location was not reached.
If you may choose, a subdomain per project needs none of this: the server block from Laravel's deployment page works as it stands, and the cookies are separate from the start.
Same subject
nginx 502 Bad Gateway with PHP-FPM: read the error log line first
A 502 means nginx got no usable answer from PHP-FPM. One line in the nginx error log says why: no socket, no permission, a dead worker or a big header.
Install phpMyAdmin on Ubuntu with nginx and PHP-FPM, the manual way
Download and verify the archive, unpack it outside the web root, write a server block with a PHP location, add config.inc.php and lock the address down.
Increase the maximum number of open files on Ubuntu (ulimit -n)
For a service such as nginx or PHP-FPM set LimitNOFILE in a systemd drop-in. The limits.conf file only changes login sessions, which is why edits seem ignored.
PHPSolvedThe fix first, then the reason