PHP Phalcon Micro API 端点返回 404,根 URL 除外

php phalcon micro api endpoint return 404 except root url

提问人:Naisarg Parmar 提问时间:10/25/2023 最后编辑:Naisarg Parmar 更新时间:10/26/2023 访问量:39

问:

我正在尝试使用 Php Phalcon Micro 设置非常简单的 2 个 api 端点。我在带有“/api/healthcheck”的端点路由上找不到 404。但是当我调用 root “/” 时,我得到了正确的响应。我要求在这个问题上指导我。提前谢谢你。

系统架构:Ec2 服务器位于 vpc 和公有子网下。Ubuntu:22.04,PHP:5.6,Apache 2.4.52

请求流程:domain.com -> nginx:80 -> apache2 virtualhost:8081 -> .htaccess 项目文件夹

API 端点 1st:

 Request: domain.com/
 Response: status 200
      Array{
        "id": "1",
        "version": "1"
      }

API 端点 2nd:

 Request: domain.com/api/healthcheck
 Response: status 404 not found

.htaccess域名

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]
</IfModule>

/var/www/domain.com/index.php

<?php
// Phalcon
use Phalcon\Loader;
use Phalcon\Mvc\Micro;
use Phalcon\Di\FactoryDefault;
use Phalcon\Db\Adapter\Pdo\Mysql as PdoMysql;

use Phalcon\Http\Response;
use Phalcon\Http\Request;

// Getting a request instance
$request = new Request();

// Set up Database Service
$di = new FactoryDefault();

// Set up the database service
$di->set('db', function () {
    return new PdoMysql(
        array(
            "host"     => DB_SERVER,
            "username" => DB_USER,
            "password" => DB_PASS,
            "dbname"   => DB_NAME
        )
    );
});

$app = new Micro($di);

$app->get(
    '/',
    function () use ($dbConn) {
        $sql = 'SELECT * FROM `dbversions` WHERE 1 Order By id desc LIMIT 1';
        $result_set = mysqli_query($dbConn, $sql);
        $row = mysqli_fetch_assoc($result_set);
        echo json_encode($row);
    }
);

$app->get(
    '/api/healthcheck',
    function () use ($dbConn) {
        $data = array("status"=> "success");
        echo json_encode($data);
    }
);

$app->handle();
?>

阿帕奇2:/etc/apache2/apache.conf

<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>

<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

虚拟主机:/etc/apache2/site-available/000-default.conf

<VirtualHost *:8081>
    ServerName domain.com
    ServerAdmin [email protected]
    DocumentRoot /var/www/domain.com/

    <Directory /var/www/domain.com>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

php8.1 软件包: sudo dpkg -l |awk '/^ii/{打印 $2}' |grep php8.1 依赖项 |tee php8.1-packages.tx

libapache2-mod-php8.1
php8.1
php8.1-cli
php8.1-common
php8.1-curl
php8.1-dev
php8.1-fpm
php8.1-mbstring
php8.1-mysql
php8.1-opcache
php8.1-phalcon5
php8.1-readline

php5.6 软件包: sudo dpkg -l |awk '/^ii/{打印 $2}' |grep php5.6 中文 |tee php5.6-packages.tx

libapache2-mod-php5.6
php5.6
php5.6-cli
php5.6-common
php5.6-curl
php5.6-dev
php5.6-fpm
php5.6-json
php5.6-mbstring
php5.6-mysql
php5.6-opcache
php5.6-phalcon3
php5.6-readline

Nginx 访问日志:sudo tail -f /var/log/nginx/access.log

100.250.01.01 - - [26/Oct/2023:01:38:12 +0000] "GET / HTTP/1.1" 200 85 "-" "PostmanRuntime/7.33.0"
100.250.01.01 - - [26/Oct/2023:01:38:23 +0000] "GET /api/healthcheck HTTP/1.1" 404 240 "-" "PostmanRuntime/7.33.0"

Apcahe2 访问日志:sudo tail -f /var/log/apache2/access.log

127.0.0.1 - - [26/Oct/2023:01:38:12 +0000] "GET / HTTP/1.0" 200 299 "-" "PostmanRuntime/7.33.0"
127.0.0.1 - - [26/Oct/2023:01:38:23 +0000] "GET /api/healthcheck HTTP/1.0" 404 450 "-" "PostmanRuntime/7.33.0"
php devops apache2 phalcon phalcon-routing

评论

0赞 brombeer 10/25/2023
/api/healthcheck或?(PHP5.6 死于~5年前)/api/healthcheck/
0赞 Naisarg Parmar 10/25/2023
@brombeer我认为问题出在访问上。可能与PHP无关。但仍然让我尝试使用 php7.* 或 php8.*。问题也不是因为 /api/healthcheck,错别字错误。
0赞 brombeer 10/25/2023
我认为问题在于,当您在没有尾部斜杠的情况下访问路由时,您的路由是尾部斜杠声明的。尝试在路由声明中使用'/api/healthcheck',
0赞 Naisarg Parmar 10/25/2023
我已将 php 更改为 8.1,但仍然有同样的问题。@brombeer
0赞 Arthur 10/26/2023
如果您使用的是php 7.*,则必须安装Phalcon 4;对于 PHP 8,请选择 Phalcon 5。并且 API 配置会有所不同。

答: 暂无答案