I'm creating a "userdir" configuration, where the webserver serves files inside each user's ~/www directory.

I have the following configuration snippet:

```
server {
    listen 443;
    listen [::]:433;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    server_name ~^(?<user>\w+)\.example\.com$;

    root /var/www/userdir;
    index index.html;
    error_page 404 = @not_found;
    add_header X-User $user;

    location / {
        root /home/$user/www;
        try_files $uri $uri/ =404;
    }

    location @not_found {
        if (-f /home/$user/www/404.html)
        {
            root /home/$user/www;
        }

        try_files /404.html =500;
    }
}

```

When a non-existing file is requested, the `@not_found` named location is entered. It first checks whether the user has a custom 404 page, and if (s)he has, it sets the document root to the user's directory before trying to serve 404.html.

However, the result of the configuration block is that if 404.html exists in the user dir, the generic nginx 404 page is returned; if not, /var/www/userdir/404.html is returned.