PHP Apps in a Subdirectory in Nginx

I needed a way to install a PHP app (e.g., WordPress) into a subdirectory and I kept hitting a brick wall. I couldn’t make heads or tails of what I was missing. This quick tutorial solved my problems. Hopefully it helps someone else!

TL;DR, TL;DW


location /nested {
    alias /var/www/nested/public;
    try_files $uri $uri/ @nested;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }
}

location @nested {
    rewrite /nested/(.*)$ /nested/index.php?/$1 last;
}