Config Nginx for Laravel in a Subfolder
Deploying an existing Apache-based project to a fresh Nginx server often reveals subtle routing misconfigurations, especially when mixing legacy static files with new PHP frameworks. If you are running laravel within a subfolderâsuch as /mysite.com/2015âand encountering "Access Denied" or 500 errors, the issue almost always lies in how Nginx calculates the document root and script filename relative to your application's public directory.
When using Apache with XAMPP or Forge environments, the .htaccess file handles rewriting rules that might mask these underlying path discrepancies. Nginx, however, is explicit. It does not follow a virtual .htaccess file for every request; instead, it relies on precise configuration directives to pass requests to your index.php entry point while preserving the correct SCRIPT_FILENAME.
The Core Problem: Path Resolution
The most common mistake when configuring Laravel in a subfolder is assuming that the server root (/home/forge/am2.aminversiones.com) is sufficient for all requests. If your Laravel application resides in /home/forge/am2.aminversiones.com/2015/public, Nginx must be explicitly told to serve files from there when accessing /2015.
In the provided configuration example, the location ^~ /2015 block attempts to handle this using an alias directive. While alias works well for static assets, it requires careful handling of the fallback logic for dynamic routes. The original snippet uses:
location ^~ /2015 {
alias /home/forge/am2.aminversiones.com/2015/public;
try_files $uri $uri/ @2015;
}
Here, try_files checks if the URI exists as a file or directory. If not, it falls through to the internal location @2015. This is a robust pattern because it separates static asset handling from routing logic. However, the critical piece is how the internal rewrite locates the entry point.
The Rewrite Logic
The internal location block defined as:
location @2015 {
rewrite ^/2015/(.*)$ /2015/index.php/$1 last;
}
is the engine driving your routes. When a request comes in for /2015/about, Nginx strips the /2015 prefix, appends it to index.php, resulting in a backend request to /home/forge/am2.aminversiones.com/2015/public/index.php/about.
This works, but only if your public_html or document root is correctly aligned with the alias. If you are deploying directly into a Forge environment where the document root is the parent of the subfolder, this configuration often triggers a 403 Forbidden or 500 Internal Server Error because the PHP interpreter cannot locate the requested script relative to the configured SCRIPT_FILENAME.
Correcting the Configuration
To ensure stability across both static files and dynamic routes, you should consolidate the logic. Here is a refined approach that aligns with standard Forge setups:
server {
listen 80;
server_name am2.aminversiones.com;
root /home/forge/am2.aminversiones.com;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
charset utf-8;
# Global static assets
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# Laravel in subfolder
location ^~ /2015 {
alias /home/forge/am2.aminversiones.com/2015/public;
# Attempt to serve static assets first
try_files $uri $uri/ @laravel_subfolder;
}
# Internal rewrite for Laravel routes in subfolder
location @laravel_subfolder {
rewrite ^/2015/(.*)$ /2015/index.php/$1 last;
}
# Standard PHP handling
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
}
Key Considerations
- Alias vs. Root: Using
aliasinside a subfolder location is safer than changing the globalroot, as it prevents Nginx from looking for files in the wrong directory hierarchy. Ensure that the path topublicis absolute and matches your filesystem exactly. - Path Info: The
fastcgi_split_path_infodirective is crucial. It separates the script (index.php) from the query path (/about). Without this, Laravel's routing will fail because it expects the full URI segment after the entry point to be passed inPATH_INFO. - Trailing Slashes: Nginx treats
/2015and/2015/differently depending on whether the directory exists. Thetry_files $uri $uri/directive handles both cases gracefully by checking for existence before falling back to the rewrite rule.
Finalizing the Setup
After updating your configuration, always test the syntax with:
sudo nginx -t
If the test passes, reload Nginx without dropping active connections:
sudo nginx -s reload
Finally, verify that your Laravel application is aware of the base path. If you are using config/app.php or artisan commands within the subfolder, ensure that APP_BASE_PATH or relevant environment variables account for the /2015 prefix if you are accessing the app via a relative URL structure.
By isolating the subfolder logic with an internal rewrite location and ensuring the alias points strictly to the public directory, you eliminate the ambiguity that causes "Access Denied" errors in mixed environments. This setup allows your legacy content at the root to coexist seamlessly with your new Laravel functionality in /2015.