我的nginx + fastcgi配置下载php文件而不是执行它们

11

我正在使用以下配置在Ubuntu 13.04上进行php5-fpm和nginx的全新安装:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    server_name localhost;

    location / {
            try_files $uri $uri/ /index.html;
    }

    location /doc/ {
            alias /usr/share/doc/;
            autoindex on;
            allow 127.0.0.1;
            allow ::1;
            deny all;
    }

    error_page 404 /404.html;

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

但是,我的web浏览器把php当作文本而不是执行结果。我应该查看哪里来排除故障?

3个回答

6
您的php代码直接显示,因为它没有被发送到php引擎,这意味着位置块被匹配并且php文件正在被提供,但是php文件没有被php块捕获,所以您的问题在于php块。
在该块中,您有两个fastcgi_pass,一个带有端口(9000),另一个是unix套接字,您不能同时使用两者,但是由于您已经用fastcgi标记了您的问题,因此我会假设您正在使用fastcgi,请尝试将此行注释掉。
#fastcgi_pass unix:/var/run/php5-fpm.sock;

那很奇怪。我能看到fastcgi正在运行,但使用任何一行连接都无法得到php输出。我重新安装了所有东西,并按照这个指南(http://askubuntu.com/questions/134666/what-is-the-easiest-way-to-enable-php-on-nginx),现在一切似乎都正常了。谢谢! - user479947

1
听起来你设置了错误的Content-Type头。你可以使用各种工具来检查。例如,在Chrome浏览器中打开开发者工具的“网络”选项卡,然后请求页面。您将在其中一个列中看到返回的“Content Type”,并且您可以单击左侧列中的请求以查看完整的响应标头。我怀疑你会发现返回的标头是"text/plain"或"application/octet-stream"而不是"text/html",这可能是你想要的。
Nginx通常根据扩展名设置默认的Content-Type头。这是通过types指令完成的,我没有在上面提到,因此您可能希望检查您的设置,以确认php扩展名映射到text/html。在应用程序中明确设置Content-Type头也可能有所帮助。

0
我通过更新我的nginx虚拟主机,将其更改为
default_type text/html;
成功解决了这个问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接