【问题标题】:nginx serves index.php instead of executing itnginx 服务 index.php 而不是执行它
【发布时间】:2016-02-09 02:46:04
【问题描述】:

我已经设置了 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$

但它不起作用。如何调试?

【问题讨论】:

    标签: php windows nginx fastcgi


    【解决方案1】:

    为了在windows中运行nginx for php项目并默认运行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 socket 端口,你可以改变它适合你的情况。❤

    【讨论】:

      【解决方案2】:

      location ~ /.php$ {
      

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

      你的意思可能是

      location ~ \.php$ {
      

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

      同样的修复将应用于您还将\/ 混淆的其他部分。

      【讨论】:

      • 谢谢,我已经用修复更新了我的问题中的配置,但文件仍然返回。如果还有什么遗漏的话,你能看看吗?
      • @Maximus 你在相应行的~ 之后有奇怪的*。甚至不确定这是否有意义。
      • @Maximus 只需关注一些文章。删除任何额外内容并关注blog.chrismeller.com/…
      • @Maximus btw,你每次修改配置都要重启nginx吗?
      • 是的,当然,我愿意nginx -s reload。谢谢,我会按照文章说明进行操作
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-30
      相关资源
      最近更新 更多