Hello all
I've built an API that completely works in an Apache webserver but returns error 500's outside of the v1/auth
and v1/user
endpoints in a Nginx server.
The API is based on Micro routing.
I've tried setting up Nginx like stated in the Phalcon installation docs but it didn't fix my issues. The following Nginx conf.d code managed to allow me to connect with the auth or user endpoint which before that would return my custom forbidden access
(see boostrap public/index.php file):
location / {
# Matches URLS `$_GET['_url']`
try_files $uri $uri/ /index.php?_url=$uri&$args;
}
Nginx returns this in the error log:
2017/10/19 12:31:23 [error] 14178#14178: *3 FastCGI sent in stderr: "PHP message: PHP Fatal error: Class 'Ot\Controllers\Api\V1\Web\Bq\AnnouncementsController' not found in /home/runcloud/webapps/ot-api-1/public/index.php on line 49" while reading response header from upstream, client: 84.195.193.177, server: _, request: "POST /v1/bq/announcements/filter HTTP/1.1", upstream: "fastcgi://unix:/var/run/ot-api-1.sock:", host: "186.238.131.39"
The bootstrap /public/index.php file:
<?php
use Phalcon\Mvc\Micro;
use Phalcon\Di\FactoryDefault;
error_reporting(E_ALL);
define('BASE_PATH', dirname(__DIR__));
define('APP_PATH', BASE_PATH . '/app');
try {
/**
* The FactoryDefault Dependency Injector automatically registers
* the services that provide a full stack framework.
*/
$di = new FactoryDefault();
/**
* Read services
*/
include APP_PATH . "/config/services.php";
/**
* Get config service for use in inline setup below
*/
$config = $di->getConfig();
/**
* Include Autoloader
*/
include APP_PATH . '/config/loader.php';
/**
* Handle and deploy the application
*/
$application = new Micro();
$application->setDI($di);
//Loading all API routes
include APP_PATH . '/config/routes.php';
foreach($collections as $collection) {
$application->mount($collection);
}
$application->notFound(function () use ($application) {
$application->response->setStatusCode(404, "Not Found")->sendHeaders();
echo "Forbidden access!";
});
$application->handle();
} catch (\Exception $e) {
echo $e->getMessage() . '<br>';
echo '<pre>' . $e->getTraceAsString() . '</pre>';
}
The working auth endpoint micro collection:
<?php
use Phalcon\Mvc\Micro\Collection as MicroCollection;
$general_auth = new MicroCollection();
$general_auth->setHandler('Ot\Controllers\Api\V1\AuthController', true);
$general_auth->setPrefix('/v1/auth');
$general_auth->post('/', 'indexAction');
return $general_auth;
A micro collection endpoint that returns error 500:
<?php
use Phalcon\Mvc\Micro\Collection as MicroCollection;
$bq_announcements = new MicroCollection();
$bq_announcements->setHandler('Ot\Controllers\Api\V1\Web\Bq\AnnouncementsController', true);
$bq_announcements->setPrefix('/v1/Bq/announcements');
$bq_announcements->get('/', 'indexAction');
$bq_announcements->get('/url', 'indexAction');
$bq_announcements->post('/filter', 'filterAction');
return $bq_announcements;
Thanks in advance for your help!