我的服务器上设置了多个 nginx 虚拟主机。tershronalds.com 正在运行 vanilla wordpress 4.3,而 staging.ninjawars.net 正在运行自定义 php 代码库。两者都依赖于 php5-fpm。
如果没有触发 php 错误或异常,则两个站点似乎都可以正常运行。对于 tershronalds.com wordpress,几乎没有错误或异常,通常没有问题。staging.ninjawars.net 自定义 php 代码库使用 auto_prepend_file 预先加载各种库:
fastcgi_param PHP_VALUE "auto_prepend_file=/path/to/ninjawars/deploy/lib/base.inc.php \n session.cookie_domain=staging.ninjawars.net \n date.timezone=America/New_York \n default_charset=UTF-8 \n";
但是,如果自定义 php 代码库出错或发生异常,则 nginx 开始出现异常!它开始在虚拟主机之间共享配置,破坏 tershronalds.com wordpress 站点:
2015/09/04 13:37:58 [error] 13145#0: *17413 FastCGI sent in stderr:
"PHP message: PHP Fatal error: Cannot redeclare validate_username() (previously declared in /path/to/ninjawars/deploy/lib/control/lib_auth.php:317) in /path/to/tersh/www/wp-includes/user.php on line 1792" while reading response header from upstream, client: 10.183.252.21, server: tershronalds.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "tershronalds.com"
换句话说,nginx 发疯了,wordpress 网站开始崩溃,因为它包含来自 ninjawars 自定义代码库的代码,导致重复功能!
我曾想过尝试升级 nginx 以解决它,但 nginx 似乎是最新版本,与 php5-fpm 相同(也许)。虚拟主机配置实际上有效并且似乎有效...... ...直到发生 php 错误/异常。我还应该研究什么来尝试解决这种错误行为?
软件版本信息:
apt-cache policy nginx nginx: Installed: 1.4.6-1ubuntu3.3 Candidate: 1.4.6-1ubuntu3.3 Version table: *** 1.4.6-1ubuntu3.3 0
500 http://mirror.rackspace.com/ubuntu/ trusty-updates/main amd64 Packages
100 /var/lib/dpkg/status
1.4.6-1ubuntu3.1 0
500 http://mirror.rackspace.com/ubuntu/ trusty-security/main amd64 Packages
1.4.6-1ubuntu3 0
500 http://mirror.rackspace.com/ubuntu/ trusty/main amd64 Packages admin@megaman:~$ apt-cache policy php5-fpm php5-fpm: Installed: 5.5.9+dfsg-1ubuntu4.11 Candidate: 5.5.9+dfsg-1ubuntu4.11 Version table: *** 5.5.9+dfsg-1ubuntu4.11 0
500 http://mirror.rackspace.com/ubuntu/ trusty-updates/universe amd64 Packages
500 http://mirror.rackspace.com/ubuntu/ trusty-security/universe amd64 Packages
100 /var/lib/dpkg/status
5.5.9+dfsg-1ubuntu4 0
500 http://mirror.rackspace.com/ubuntu/ trusty/universe amd64 Packages
wordpress 版本是最新的 4.3,我并不认为这是一个相关因素。
编辑如果我关闭这个 nginx 虚拟主机配置,一切正常(当然,除了 staging.ninjawars.net 子域完全关闭)。
Staging.ninjawars.net:
server {
listen 80;
server_name staging.ninjawars.net www.ninjawars.net ninjawars.net nw.local nw.remote;
root /path/to/ninjawars/deploy/www;
error_page 404 /404.php;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
index index.php;
location / {
try_files $uri $uri/ =404; # /index.php?$args;
}
location ~ \.php$ {
fastcgi_param PHP_VALUE "auto_prepend_file=/path/to/ninjawars/deploy/lib/base.inc.php \n session.cookie_domain=staging.ninjawars.net \n date.timezone=America/New_York \n default_charset=UTF-8 \n";
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
这是更长的时间并将所有内容重定向到
无 www,https 域 tershronalds.com 站点:
server {
listen 80;
server_name tershronalds.com;
return 301 https://tershronalds.com$request_uri;
}
server {
listen 80;
server_name www.tershronalds.com;
return 301 https://tershronalds.com$request_uri;
}
server {
listen 80;
server_name tershronalds.net www.tershronalds.net tershart.net www.tershart.net;
return 302 https://tershronalds.com$request_uri;
# Temporarily redirect these domains for now.
}
server {
listen 443 ssl;
server_name tershronalds.com www.tershronalds.com tersh.remote;
root /path/to/tersh/www;
index index.php
# ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers various_cyphers_here
ssl_certificate /somecrt;
ssl_certificate_key /somekey;
#location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
#}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
error_page 401 https://tershronalds.com/forbidden.html;
# Standard wordpress includes !!!
include global/wordpress.conf;
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
我自己使用 Ubuntu 14.04 和 PHP 的经验表明,在启用opcache扩展的情况下,包含的文件中的某些操作
auto_prepend_file
可能会失败。尝试通过删除符号链接禁用扩展:/etc/php5/fpm/conf.d/05-opcache.ini(最初指向../../mods-available/opcache.ini)。它会降低性能,但不应该破坏任何东西。我对nginx知之甚少,所以我不能说您的每个站点 PHP 设置的方法是否正确,但是使用php.ini 部分可能会增加可读性,并且如果它与 PHP <-> nginx 连接有关,也会使您的问题消失。
更好的是,您可以创建单独的 PHP-FPM 守护程序实例,每个实例处理一个网站。这需要更多的 RAM,但完全分离了 Web 应用程序代码的处理。对每个应用程序进行足够严格的设置,这种方法提供了额外的安全性
open_basedir
。disable_functions
FPM 实例在/etc/php5/fpm/pool.d/下定义。