我最近设置了一个运行于FastCGI进程的PHP的nginx服务器。 服务器正在运行HTML文件,但PHP文件正在下载,而不是显示和PHP代码不处理。
这是我在nginx.conf中的:
server { listen 80; server_name pubserver; location ~ \.php$ { root /usr/share/nginx/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name; include fastcgi_params; } }
命令netstat -tulpn | grep :9000 netstat -tulpn | grep :9000显示以下内容,指示php-fastcgi正在运行并监听端口9000:
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 2663/php-cgi
如果这是我的服务器在CentOS 6上运行的重要性,并且我使用Fedora项目的软件库安装了nginx和PHP。
PHP短标签不是问题。 即使只包含HTML代码的PHP文件也是如此。
我包括一个索引文件的位置,将nginx.conf中的所有path改为绝对path,现在文件如下所示:
server { listen 80; server_name onedesigns; root /usr/share/nginx/html; location / { index index.html index.htm index.php; } location ~ \.php$ { include /etc/nginx/fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name; } }
随机猜测,但你使用短的开放标签,并禁用您的php.ini文件?
如果你的代码使用<? 而不是<?php然后尝试改变。
如果没有,那么检查返回的头文件,看它是否有一个X-Powered-By头文件,并返回PHP。
编辑:你在这里添加了一个答案,而不是一个答复,所以我从来没有得到通知。 尽pipe如此,你还是设法自己动手。 该代码很可能正在处理,但你只是没有检查下载的文件的内容。
现在,这是为什么发生。 在你的Nginxconfiguration中,你有以下的default_type octet-stream ,你的PHP不会返回一个内容types,因此nginx使用八位字节stream。 只是删除该指令,它应该工作。
正如@Martin所指出的那样,由浏览器接收的默认数据types不是 text / html或浏览器可以显示的东西。 但数据types设置是用php,因为它是一个FastCGI脚本。 如果脚本不输出内容types头,nginx将返回八位字节stream。
你应该能够执行脚本:
curl http://server/script.php
这将起作用,因为curl接受/ ,而浏览器如chrome,firefox等对不同的内容types有不同的规则。
你可能想检查你的php.ini来检查默认的内容types。
另外,您可以使用检索标题信息
curl -I http://server/script.php
尝试改变位置:
location ~ ^(.+\.php)(.*)$ {
试试这个:
http{ # ..... index index.php index.html index.htm; # .... } server { listen 80; server_name pubserver; root html; location ~ \.php$ { include fastcgi_params; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass php; } }
希望这个帮助。