我正在将 Moodle (moodle.org) 从 Apache 主机移至运行 Nginx 的 Ubuntu 12.04 LTS 主机。主机的设置方式意味着它将运行相当多的域,其中每个域(或其他站点)都将驻留在用户目录中。
我正在运行带有 php5-fpm 的 Nginx。我发现了相当多在用户目录中运行 php-fpm 的配置,这些配置都有效。然而问题是 Moodle 在 PHP 中大量使用斜线参数,导致许多 URL 看起来像这样:
/home/[user]/public_html/theme/image.php/standard/theme/1377637305/favicon
我正在运行这个 Nginx 配置:
server {
#listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /usr/share/nginx/www;
index index.html index.htm index.php;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
location ~ ^/~(?<userdir_user>.+?)(?<userdir_uri>/.*)?$ {
alias /home/$userdir_user/public_html$userdir_uri;
index index.html index.htm index.php;
autoindex off; ## to allow autoindex a la apache
include php5_generic;
}
}
## php5_generic
location ~ \.php {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
# fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
我遇到的问题是,此配置确实适用于普通的 php 文件,但不适用于使用斜杠参数的 HTTP GET 请求。Nginx 错误日志报告 php-fpm 引发了如下错误:
*615 open() "/home/[user]/public_html/theme/image.php/standard/core/1377637305/moodlelogo" failed (20: Not a directory),
或者
5 FastCGI sent in stderr: "Access to the script '/home/[user]/public_html/lib/javascript.php/1377637305/lib/javascript-static.js' has been denied (see security.limit_extensions)"
这里出了什么问题?
答案1
使用示例配置后,我找到了解决方案http://wiki.nginx.org/PHPFcgiExample作为基础。此解决方案还建议(与许多其他示例相反)将 php.ini 中的 cgi.fix_pathinfo 设置保持为 1。
我没有采用完整的 user_dir 解决方案,而是采用了硬编码解决方案,因为我还不知道如何让 $userdir_user 变量在后续正则表达式中发挥作用。
第二件要寻找的事情是修复 fastcgi_split_path_info。原因是否则 ~user 部分将被转换为提供给 PHP 的路径。
server {
index index.php index.html index.htm;
location ~ ^/~user(?<userdir_uri>/.*)?$ {
alias /home/user/public_html$userdir_uri;
#autoindex on;
#If this doesn't work, set cgi.fix_pathinfo=1 in /etc/php5/fpm/php.ini
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^/~user/(.+?\.php)(/.*)$;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
}
}