繁体   English   中英

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

[英]nginx serves index.php instead of executing it

我已经设置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. 在配置中将default_type application/octet-stream更改为default_type text/html
  2. location ~ / .php$更改为location ~* / .php$

但这是行不通的。 我该如何调试?

location ~ /.php$ {

配置行看起来不正确。 这意味着:以/字符结尾的位置,后跟任何字符,然后是php 例如: foobar/zphp

你的意思可能是

location ~ \.php$ {

这意味着:以.php结尾的位置。 例如: /index.php

相同的解决方案将应用于您还将\\/混淆的另一部分。

在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套接字端口,您可以根据自己的情况进行更改.❤

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM