【问题标题】:Routing with Phalcon using Nginx + php5-fpm, file not found使用 Nginx + php5-fpm 使用 Phalcon 路由,找不到文件
【发布时间】:2014-11-01 01:58:33
【问题描述】:

我一直在努力解决这个问题。我无法正确添加路由,否则 nginx 无法正常使用 phalcon。

phalcon tuts 都是基于 apache 的,这无济于事。

问题:我可以访问http://localhost 并获得 index.php,但是当我尝试访问 localhost/signup 时出现“找不到文件”。

index.php 试试 {

//Register an autoloader
$loader = new \Phalcon\Loader();
$loader->registerDirs(array(
    '../application/controllers/',
    '../application/models/'
))->register();

$di = new Phalcon\DI\FactoryDefault();

//Setup the view component
$di->set('view', function(){
    $view = new \Phalcon\Mvc\View();
    $view->setViewsDir('../application/views/');
    return $view;
});

$di->set('url', function(){
    $url = new Phalcon\Mvc\Url();
    $url->setBaseUri('/');
    return $url;
});

$di->set('router', function() {
    $router = new \Phalcon\Mvc\Router();
    $router->setUriSource(Phalcon\Mvc\Router::URI_SOURCE_SERVER_REQUEST_URI);
    $router->add(
        '/signup',
        array(
            "controller" => "signup",
            "action" => "index"
        )
    );

    return $router;
});

//Handle the request
$application = new \Phalcon\Mvc\Application($di);
echo $application->handle()->getContent();

} catch(\Phalcon\Exception $e) {
  echo "PhalconException: ", $e->getMessage();
}

nginx 配置

server {
listen   80;
server_name  Tikarta-berkshelf;
root /var/www/;
index public/index.php;
access_log  /var/log/nginx/localhost.access.log;

try_files $uri $uri/ @rewrite;

location @rewrite {
    rewrite ^/(.*)$ /index.php?_url=/$1;
}


location ~ \.php$ {
#try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;

fastcgi_index public/index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}

}

SignupController.php

<?php


class SignupController extends \Phalcon\Mvc\Controller
{

public function indexAction()
{
    echo "hello";
}

}

【问题讨论】:

    标签: nginx routing phalcon


    【解决方案1】:

    错误的文档根目录是这里的问题。 Nginx 配置应该是

    server {
        listen   80;
        server_name  Tikarta-berkshelf;
        root /var/www/public;
        index index.php;
        access_log  /var/log/nginx/localhost.access.log;
    
        location / {
        index  index.php index.html index.htm;
    
        # if file exists return it right away
        if (-f $request_filename) {
        break;
        }
    
        # otherwise rewrite it
        if (!-e $request_filename) {
        rewrite ^(.+)$ /index.php?_url=/$1 last;
        break;
        }
        }
        location ~ \.php$ {
        #try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    
        fastcgi_index index.php;
        include fastcgi_params;
        }
        location ~ /\.ht {
        deny all;
        }
    }
    

    【讨论】:

    • 通过发布问题,您会收到来自外太空的一部分精神力量射线,帮助您自己找到答案。事实。
    • 大声笑,是的,当您尝试向某人解释问题所在时,它被称为橡皮鸭现象,并且答案变得显而易见。你最好在桌子上放一只橡皮鸭,然后问它哈哈。
    猜你喜欢
    • 1970-01-01
    • 2013-08-28
    • 2015-11-17
    • 2014-08-17
    • 1970-01-01
    • 2021-04-02
    • 2012-12-05
    • 2014-08-04
    • 1970-01-01
    相关资源
    最近更新 更多