Nginx - location with root in other directory and PHP

Nginx - location with root in other directory and PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Serving PHP from Aliased Directories in Nginx

Configuring Nginx to serve static assets like CSS and JS files while routing dynamic requests through a PHP handler is straightforward within a single web root. However, attempting to replicate Apache's Alias directive or mount a subfolder with its own distinct root—such as your /cut example—introduces specific complexities regarding how Nginx resolves file paths and handles the internal rewrite process.

The core issue usually stems from a mismatch between the logical URL path, the physical document root, and the SCRIPT_FILENAME passed to FastCGI. When you define a location /cut block with a different root, but then use try_files to fall back to an internal location like @php, you often lose the context of the original request path. Nginx rewrites the URI to /dev/public/index.php before passing it to PHP-FPM, meaning your script receives $SCRIPT_FILENAME pointing to the wrong physical file, or worse, it attempts to execute a non-existent file if the rewrite logic isn't precise.

Consider this scenario: you have /var/www/dev/public as your main root and /var/www/cut/public as a separate directory that should act like an alias for /cut. A common mistake is using try_files $uri $uri/ @php; inside the aliased location block. While this attempts to serve static files from the new root, it fails when the file doesn't exist or isn't a directory. The @php handler then rewrites the URI. If the rewrite logic assumes the standard web root, it will construct a path like /var/www/dev/public/index.php, which does not exist for requests originating from /cut.

The robust solution involves separating the static file serving logic from the dynamic routing logic within the specific location block, rather than relying solely on global rewrites. You must ensure that when a request hits /cut/some-file.css, Nginx serves it directly from /var/www/cut/public/some-file.css without touching the PHP handler.

To achieve this, you can use a prefix match or an exact match with the root directive set specifically for that subdirectory. Here is how you structure the configuration to handle your specific folder layout:

server {
    listen   80;
    server_name _;

    # Main application root
    root /var/www/dev/public;
    index index.php index.html;

    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;

    # Serve the /cut subdirectory from its own physical location
    location ^~ /cut {
        # Rewrite to serve static files from the correct root
        # This ensures /cut/assets/app.css is served directly
        rewrite ^/cut/(.*)$ /cut/public/$1 last;
    }

    # Handle requests within /cut that need PHP processing
    location ^~ /cut/public {
        root /var/www;
        
        # Try static files first, then fall back to the specific index.php for this folder
        try_files $uri $uri/ /cut/public/index.php$is_args$args;

        location ~* \.php(/|$) {
            fastcgi_pass php:9000;
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            
            # CRITICAL: Ensure SCRIPT_FILENAME reflects the aliased path
            fastcgi_param DOCUMENT_ROOT /var/www/cut/public;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            
            include fastcgi_params;
        }
    }

    # Main application handling
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # Global PHP handler for the main root (optional if covered above)
    location ~* \.php(/|$) {
        fastcgi_pass php:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        
        fastcgi_param DOCUMENT_ROOT /var/www/dev/public;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        
        include fastcgi_params;
    }
}

The key distinction here is avoiding a global try_files that blindly routes everything to the main index.php. Instead, the /cut/public location explicitly sets its own DOCUMENT_ROOT. Without this, Nginx might inherit the parent root directive or fail to calculate the correct filename for the PHP handler.

By rewriting the initial request from /cut/file.css to /cut/public/file.css, you ensure static assets are served correctly. For PHP scripts, the try_files directive attempts to locate the file in /var/www/cut/public. If it fails or hits a .php extension, the fallback /cut/public/index.php$is_args$args executes the script with the correct physical path resolved via the local DOCUMENT_ROOT.

This approach mimics Apache's behavior by decoupling the URL structure from the filesystem structure while maintaining strict control over how FastCGI parameters are constructed. It ensures that other_other_file.php inside the cut directory is executed against its actual location, not a phantom path in your main public folder.

Our network