简体   繁体   English

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

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

I've setup nginx configuration to run php with fcgi on Windows. 我已经设置nginx配置以在Windows上使用fcgi运行php。 Here is my config: 这是我的配置:

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

I've checked if fcgi server is running and it's all OK: 我检查了fcgi服务器是否正在运行,一切正常:

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

Then I request the file using curl to prevent file caching: 然后我使用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();

And it outputs files content instead of running it. 并且它输出文件内容而不是运行它。 I've tried several solutions offered in other topics: 我尝试了其他主题中提供的几种解决方案:

  1. Changed default_type application/octet-stream to default_type text/html in the config 在配置中将default_type application/octet-stream更改为default_type text/html
  2. Changed location ~ / .php$ to location ~* / .php$ location ~ / .php$更改为location ~* / .php$

But it doesn't work. 但这是行不通的。 How can I debug it? 我该如何调试?

The

location ~ /.php$ {

configuration line does not look correct. 配置行看起来不正确。 It means: a location that ends with a / character followed by any character followed by php . 这意味着:以/字符结尾的位置,后跟任何字符,然后是php Eg: foobar/zphp . 例如: foobar/zphp

What you meant probably is 你的意思可能是

location ~ \.php$ {

which means: a location that ends with .php . 这意味着:以.php结尾的位置。 Eg: /index.php 例如: /index.php

The same fix would be applied to the other section where you also confused the \\ with / . 相同的解决方案将应用于您还将\\/混淆的另一部分。

for run nginx in windows for php project and run index.php files by default you can add this code to 在Windows中为php项目运行nginx并默认运行index.php文件,您可以将此代码添加到

/NGINX_FOLDER/conf/nginx.conf

code: 码:

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 is my default nginx port and 9000 is my tcp socket port, you can change it Suitable for your situation.❤ 80是我的默认nginx端口,而9000是我的tcp套接字端口,您可以根据自己的情况进行更改.❤

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

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