nginx提供index.php而不是执行它

时间:2015-11-08 09:22:13

标签: php windows nginx fastcgi

我已经设置了nginx配置,以便在Windows上使用fcgi运行php。这是我的配置:

server {
    charset utf-8;
    client_max_body_size 128M;

    listen 8888;
    server_name api.domain.local;

    root        /projects/domain.server.new/app/rest/web;
    index       index.php;

    error_log   /webservers/nginx/logs/api.domain.local.error.log;

    location / {
        # Redirect everything that isn't a real file to index.php
        try_files $uri $uri/ /index.php?$args;
    }

    #uncomment to avoid processing of calls to non-existing static files by Yii
    location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
        try_files $uri =404;
    }
    error_page 404 /404.html;

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass   127.0.0.1:9123;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location ~ //.(ht|svn|git) {
        deny all;
    }
}

我已经检查过fcgi服务器是否正在运行并且一切正常:

E:\Webservers\Nginx>netstat -aon | findstr :9123
  TCP    127.0.0.1:9123         0.0.0.0:0              LISTENING       2644

然后我使用curl请求文件以防止文件缓存:

$ curl -i localhost:8888

HTTP/1.1 200 OK
Server: nginx/1.6.1
Date: Sun, 08 Nov 2015 09:15:20 GMT
Content-Type: application/octet-stream
Content-Length: 960
Last-Modified: Tue, 27 Oct 2015 17:33:50 GMT
Connection: keep-alive
ETag: "562fb57e-3c0"
Accept-Ranges: bytes

<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/../../vendor/autoload.php');
require(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../../common/config/bootstrap.php');
require(__DIR__ . '/../config/bootstrap.php');

$config = yii\helpers\ArrayHelper::merge(
    require(__DIR__ . '/../../common/config/main.php'),
    require(__DIR__ . '/../../common/config/main-local.php'),
    require(__DIR__ . '/../config/main.php'),
    require(__DIR__ . '/../config/main-local.php')
);

$application = new yii\web\Application($config);
$application->run();

它输出文件内容而不是运行它。我尝试了其他主题提供的几种解决方案:

  1. 在配置
  2. 中将default_type application/octet-stream更改为default_type text/html
  3. location ~ / .php$更改为location ~* / .php$
  4. 但它不起作用。我该如何调试呢?

2 个答案:

答案 0 :(得分:1)

HomeActivity

配置行看起来不正确。它表示:以location ~ /.php$ { 字符结尾的位置,后跟任意字符,后跟/。例如:php

你的意思可能是

foobar/zphp

表示:以location ~ \.php$ { 结尾的位置。例如:.php

相同的修补程序将应用于您将/index.php\混淆的其他部分。

答案 1 :(得分:0)

在Windows中为php项目运行nginx并默认运行index.php文件,您可以将此代码添加到

/NGINX_FOLDER/conf/nginx.conf

代码:

http{
server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.php index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
            include fastcgi.conf;
        }
    }
}

80是我的默认nginx端口,而9000是我的tcp套接字端口,您可以根据自己的情况进行更改。❤