Nginx和FastCGI下载PHP文件而不是处理它们


Nginx and FastCGI downloads PHP files instead of processing them

我运行在Windows 7(64位)上,使用PHP 5.4.12和Nginx 1.5.8。

我已经阅读了许多关于设置和解决此问题的教程,即当从我的本地主机请求 PHP 文件时,它会将其下载为文件而不是显示 PHP 页面。下面是我的nginx.conf文件:

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    server {
        listen 8081;
        server_name localhost;
        access_log C:/nginx/logs/access.log;
        error_log C:/nginx/logs/error.log;
        root C:/nginx/html;
        fastcgi_param  REDIRECT_STATUS    200;
        location ~ '.php$ {
          fastcgi_pass   127.0.0.1:9000;
          fastcgi_index  index.php;
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
          include        fastcgi_params;
        }
    }
}

我正在通过命令提示符手动运行nginx.exe。我还尝试启动 php-cgi.exe首先在单独的命令提示符下手动启动,如下所示:

C:'php5.4.12'php-cgi.exe -b 127.0.0.1:9000

我请求的 php 文件在 C:/nginx/html 中,我请求它为:

http://localhost:8081/info.php

然后下载它。这个PHP文件的内容是:

<?php
phpinfo();
?>

我怎样才能让我的 PHP 脚本在这个环境中运行。有人有这方面的经验吗?

尝试将default_type application/octet-stream;更改为default_type text/html;也许你的php脚本没有设置内容MIME类型,它来自nginx。

尝试在此处放置" * "

location ~* '.php$ {

您的路径有问题,nginx不知道通过URL访问的路径是它应该通过"fastcgi_pass"的路径。因此,它提供了文件以供下载。

从以下位置检查错误日志:

C:/nginx/logs/error.log;

你有"C:/nginx/html/info.php;"吗?

我发现如果你在服务器上有端口 80 的 http2 指令。 http2只能在https下工作。因此,如果您删除http2,它应该可以解决您的问题。

对我来说

,端口 80 上也启用了 http2。禁用它解决了问题。