我正在将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 ~ ^/~(?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)"
这里出了什么问题?
最佳答案
在使用http://wiki.nginx.org/PHPFcgiExample上的示例配置作为基础后,我找到了一个解决方案.此解决方案还建议(与许多其他示例相反)将PHP.ini中的cgi.fix_pathinfo设置保持为1.
我没有找到完整的user_dir解决方案,而是选择了硬编码解决方案,因为我还不知道如何使$userdir_user变量在后续正则表达式中工作.
要寻找的第二件事是修复fastcgi_split_path_info.原因是否则〜用户部分被转换为给予PHP的路径.
server {
index index.PHP index.html index.htm;
location ~ ^/~user(?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;
}
}
}