0

Here is the relevant part of my nginx.conf, which does not work for php files that are located in the home directories of the users:

  location ~ ^/~(.+?)(/.*)?$ {
     alias /usr/home/$1/www$2;
     autoindex on;
    }

   # Serve user directories php files
    location ~ ^/~(.+?)(/.*\.php)$ {
        alias /usr/home/$1/www;
        try_files $2 =404;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_intercept_errors on;
        include fastcgi_params;
        fastcgi_param SCRIPT_NAME /~$1$fastcgi_script_name;
        }

    location ~ \.php$ {
         try_files $uri =404;
         fastcgi_split_path_info ^(.+\.php)(/.+)$;
         fastcgi_pass unix:/var/run/php-fpm.sock;
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME $request_filename;
         include fastcgi_params;       
        }
qazqolang
  • 1
  • 1

1 Answers1

2

You can use a nested location block to handle the .php files. For example.

location ~ ^/~(?<user>.+?)(?<path>/.*)?$ {
    alias /usr/home/$user/www$path;
    autoindex on;

    location ~ \.php$ {
        if (!-f $request_filename) { return 404; }

        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_intercept_errors on;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME $request_filename;
    }
}

location ~ \.php$ {
    ...
}

See this document for more.

EDIT: I realised I needed to use named captures as the numeric captures go out of scope.

Richard Smith
  • 11,859
  • 2
  • 18
  • 26