How can I make this nested location configuration use the correct path to call a php program?



PHP Snippet 1:

server {
  listen 443 ssl http2 default_server;
  listen [::]:443 ssl http2 default_server;

  server_name example.com;
  root /var/sites/example.com;

  access_log /var/log/nginx/example_com_access.log main;
  access_log /var/log/nginx/example_com_access.log scripts;
  error_log /var/log/nginx/example_com_error.log info;

  try_files index.html index.htm /index.html =404;

  location ~ ^/(?<username>[^/]+?)(?<userpath>/.*)?$ {
    alias /home/$username/www$userpath;

    location ~ ^.+?\.php(?:/.*)?$ {
      include fastcgi_params;

      fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
      fastcgi_split_path_info ^(.+\.php)(/.*)$;

      # By default, the variable PATH_INFO is not set under PHP-FPM
      # But some apps need it. If you have a “Bad Request” error, double check this var!
      # NOTE: the separate $path_info variable is required. For more details, see:
      # https://trac.nginx.org/nginx/ticket/321

      set $path_info $fastcgi_path_info;
      fastcgi_param PATH_INFO $path_info;

      fastcgi_param SCRIPT_FILENAME $request_filename;
      fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    }
  }
}