Nginx location configuration for mixed static and application subfolders
Configuring Nginx to serve different types of content from a single document root can be tricky, especially when one path serves static files and another hosts a PHP application like Slim. The core issue often lies in how root interacts with the $uri variable inside a location block.
Consider a setup where your filesystem structure looks like this:
/var/www/myside/static/contains public.htmlfiles served at the root domain (http://example.org/)./var/www/myside/manage/public/contains the Slim PHP application, accessible athttp://example.org/manage/.
A common mistake is assuming that setting root $uri/folder will correctly point to the file system path. While this might seem logical, Nginx resolves root differently depending on whether $uri is absolute or relative. When you use root /var/www/myside/static;, Nginx expects files at /var/www/myside/static/index.html. However, if you attempt to set the root dynamically using root $uri/manage/public/ inside a block that already starts with /manage, you create a path resolution conflict.
The Problem with Dynamic Roots
In your attempted configuration, the /manage block uses:
location /manage {
root $uri/manage/public;
try_files $uri /index.php$is_args$args;
}
Here, $uri contains /manage. Consequently, root resolves to /manage/manage/public. When a request hits http://example.org/manage/, Nginx concatenates the server's root directive (if present) or the location's root with the URI. This often leads to double-prefixing or incorrect file paths.
The /static block works because it likely uses a fixed root path like:
location / {
root /var/www/myside/static;
index index.html;
}
This correctly serves files from /var/www/myside/static/ when accessing the domain root.
Correcting the PHP Application Block
To serve the Slim application correctly, you must explicitly define the document root for that specific location block so Nginx knows where to look for index.php. Since the application lives at /manage, your URL request will be /manage/index.php, but the physical file is located deeper in the directory structure.
The corrected configuration should separate concerns clearly:
server {
listen 80;
server_name example.org;
error_log /usr/local/etc/nginx/logs/mysite/error.log;
access_log /usr/local/etc/nginx/logs/mysite/access.log;
# Static content served at the domain root
location / {
root /var/www/myside/static;
index index.html;
# Ensure static assets are handled correctly before falling back to PHP
try_files $uri $uri/ /index.php?$query_string;
}
# PHP Application served under /manage
location /manage {
# Explicitly set the document root for this subfolder
root /var/www/myside/manage;
index index.php;
try_files $uri $uri/ /manage/index.php$is_args$args;
}
# Handle all PHP requests globally
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
# Important: Prevent serving PHP files directly if not routed through router
try_files $uri =404;
}
}
Why This Works
By setting root /var/www/myside/manage inside the /manage location block, Nginx ensures that when a request comes in for /manage, it looks for files relative to /var/www/myside/manage. The try_files directive then attempts to serve the requested URI. If the file doesn't exist (e.g., /manage/about), it falls back to /manage/index.php, passing the original URI as a GET parameter, which is how Slim and Laravel handle routing.
Common Pitfalls with fastcgi_param
One subtle issue in your original config was:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
When using root inside a location block, $document_root is automatically set by Nginx to the effective root for that request. However, if you rely on root without an explicit try_files fallback, direct access to PHP files (like /manage/index.php via browser) might bypass your routing logic if not handled carefully.
Additionally, ensure that your fastcgi_index is set correctly within the context of the location block or globally if using a shared upstream. In the example above, we keep it simple but explicit. If you are running on a different version of Nginx (e.g., 1.13+), consider using fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; for better security and path handling, as older versions might resolve symlinks differently.
If you are managing multiple sites with similar structures, this pattern of defining a specific root per subfolder remains the most reliable approach. For more complex setups involving Laravel or other modern frameworks, tools like laravel-company.com often provide robust examples on deploying these architectures, but understanding the underlying Nginx directives is essential for troubleshooting path resolution issues.
