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.
subapp:alias /var/www/subapp/public;: This directive tells Nginx to serve files for requests to/subappfrom the specified path, which is thepublicdirectory 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_filesto rewrite requests to the Laravelindex.phpfile, passing the original request path as a query string.