PHPexception会导致nginx将虚拟主机泄漏到彼此之中

我有多个在我的服务器上设置的nginx虚拟主机。 tershronalds.com运行的是vanilla wordpress 4.3,而staging.ninjawars.net正在运行一个自定义的PHP代码库。 两者都依赖于php5-fpm。

如果没有PHP错误或exception被触发,这两个网站似乎运作良好。 在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代码库出错或发生exception,nginx开始行为不端! 它开始共享虚拟主机之间的configuration,打破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自定义代码库的代码,导致重复的function!

我曾想过尝试升级nginx来解决它,但nginx似乎是在它的最新版本,并与php5-fpm(也许)相同。 虚拟主机configuration实际上工作,似乎有效……直到一个PHP错误/exception发生。 我还应该考虑尝试解决这个问题的行为?

软件版本信息:

 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的虚拟主机configurationclosures,一切工作正常(当然,除了staging.ninjawars.net子域被完全closures)。

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; } } 

这是更长的时间和redirect的一切

无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的经验表明, auto_prepend_file包含的文件中的某些操作可能会失败, 启用opcache扩展。 尝试通过删除符号链接来禁用扩展名: /etc/php5/fpm/conf.d/05-opcache.ini (最初指向../../mods-available/opcache.ini )。 它会降低性能,但不应该破坏任何东西。

我对nginx知之甚less,所以我不能说每个站点的PHP设置的方法是否正确,但是使用php.ini部分可能会增加可读性,并且如果它与PHP < – > nginx连接有关,也会使问题消失。

更好的是,您可以创build单独的PHP-FPM守护程序实例,每个守护进程处理一个网站。 这需要稍微更多的内存,但完全分离处理您的Web应用程序代码。 通过open_basedirdisable_functions为每个应用设置足够的紧密度,这种方法提供了额外的安全性。 FPM实例在/etc/php5/fpm/pool.d/下定义。