这是我的 nginx.conf 的相关部分,它对于位于用户主目录中的 php 文件不起作用:
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;
}
答案1
您可以使用嵌套位置块来处理.php
文件。例如。
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$ {
...
}
看这个文件了解更多信息。
编辑:我意识到我需要使用命名捕获,因为数字捕获超出了范围。