Skip to content

Nginx subdir

Mark Friedrich edited this page Nov 17, 2018 · 8 revisions

Subdirectory config for Nginx

In case you want to install Pathfinder in a subdirectory https://www.[DOMAIN].com/subdir rather than on a TLD https://www.[DOMAIN].com, you need to adjust your Nginx config and *.ini files.

1.environment.ini config file changes
  • Add subdir name /subdir to BASE property.
  • URL still points to the TLD.
BASE      =   /subdir
URL       =   {{@SCHEME}}://local.pathfinder
2.pathfinder.conf changes

Here are the required adjustments need to be done in your existing pathfinder.conf file: wiki: pathfinder.conf

Info: Read this article for a more detailed information how it works.

  1. No changes made to default location / {..} section. (no longer used by Pathfinder)
  2. New location /subdir {..} section points to https://www.[DOMAIN].com/subdir URL
  3. alias points to your Pathfinder project directory (change this to your project directory)
  4. First trick: try_files checks the existence of the requested URL against a named location location @subdir {..}
  5. location @subdir {..} rewrites any incoming request (e.g. /subdir/setup or /subdir/img/1.jpg) to /subdir/index.php by searching for a new location matching the changed URI (see flag: last).
  6. New rewrite URL /subdir/index.php matches location ~ \.php$ {..} inside the location /subdir {..} block
  7. SCRIPT_FILENAME $request_filename is important. This changes file path for the current request, based on the alias directive, and the request URI: /subdir/index.php becomes index.php. Pathfinder´s URL rooting engine expects index.php as SCRIPT_FILENAME, otherwise internal root aliases can not be matched against an incoming request.
  8. The default location ~ \.php$ {..} is no longer used by Pathfinder (maybe by an other app on your server) and SCRIPT_FILENAME is still set to $document_root$fastcgi_script_name (default).

Hint: GET parameters don´t get lost during rewrite.

server {
    listen [::]:443 ssl http2;
    listen 443 ssl http2 backlog=16384 reuseport;

    server_name    www.pathfinder-w.space;
    root           /var/www;

    location / {                                                               ## [1]
        index      index.php;
        try_files  $uri $uri/ /index.php?$query_string;
    }

    location /subdir {                                                         ## [2]
        alias  	   /var/www/pathfinder-w.space/public_html;                    ## [3]
        try_files  $uri $uri/ @subdir;                                         ## [4]
	
        location ~ \.php$ {                                                    ## [6]
            try_files        $uri =404;
            fastcgi_pass     unix:/var/run/php-fpm/php-fpm.sock;               
            fastcgi_index    index.php;
            fastcgi_param    SCRIPT_FILENAME  $request_filename;               ## [7]
            include          fastcgi_params;
        }
    }

    location @subdir {
        rewrite /subdir/(.*)$ /subdir/index.php last;                          ## [5]
    }

    location ~ \.php$ {
        try_files        $uri =404;
        fastcgi_pass     unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index    index.php;
        fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;   ## [8]
        include          fastcgi_params;
    }
}