【问题标题】:Luracast\Restler: NGINX 404 not foundLuracast\Restler:未找到 NGINX 404
【发布时间】:2018-10-10 17:27:27
【问题描述】:

我正在尝试在 nginx + php7.1 上安装 luracast restler,但我总是得到以下输出。 (在 localhost 上使用 apache + php 7.1 一切正常)

{
    "error": {
        "code": 404,
        "message": "Not Found"
    },
    "debug": {
        "source": "Routes.php:431 at route stage",
        "stages": {
            "success": [
                "get"
            ],
            "failure": [
                "route",
                "negotiate",
                "message"
            ]
        }
    }
}

当我尝试 resources.json 时,我得到了这个:

{
    "apiVersion": "1",
    "swaggerVersion": "1.1",
    "basePath": "https://api.example.com/api",
    "produces": [
        "application/json"
    ],
    "consumes": [
        "application/json"
    ],
    "apis": []
}

NGINX.conf:

server {
  listen *:443 ssl http2;
  server_name api.example.com;
  keepalive_timeout 70;

  root /var/www/public;

  # Security
  add_header Strict-Transport-Security "max-age=31536000; includeSubdomains" always;
  add_header X-Frame-Options SAMEORIGIN;
  add_header X-Content-Type-Options nosniff;
  add_header X-XSS-Protection "1; mode=block";

  fastcgi_param HTTPS on;

  location /api/ {
     try_files $uri $uri/ /api/index.php$is_args$args;
  }

  location ~ \.php$ {
     fastcgi_pass   127.0.0.1:9000;
     fastcgi_index  index.php;
     fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
     include fastcgi.conf;
  }

  location ~ \.(aspx|jsp|cgi)$ {
     return 410;
  }

  location ~ /\.ht {
     deny  all;
  }

  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
  ssl_prefer_server_ciphers on;

  ssl_certificate /etc/letsencrypt/live/api.example.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/api.example.com/privkey.pem;
}

index.php:

<?php

require_once 'vendor/restler.php';

use Luracast\Restler\Restler;
use Luracast\Restler\Defaults;

Luracast\Restler\Resources::$hideProtected = false;
Defaults::$crossOriginResourceSharing = true;
Defaults::$accessControlAllowOrigin   = '*';
Defaults::$accessControlAllowMethods  = 'GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD';

$r = new Restler();

$r->addAPIClass('Resources');
$r->addAPIClass('Explorer');

$r->addAPIClass('Controller\User');
$r->addAPIClass('Controller\Place');
$r->addAPIClass('Controller\Event');
$r->addAPIClass('Controller\Post');
$r->addAPIClass('Controller\Login');
$r->addAPIClass('Controller\Register');

$r->addAuthenticationClass('Controller\AccessControl');

$r->handle();

在 localhost 上使用 apache 一切正常,我得到以下输出:

{
    "apiVersion": "1",
    "swaggerVersion": "1.1",
    "basePath": "http://localhost/api",
    "produces": [
        "application/json"
    ],
    "consumes": [
        "application/json"
    ],
    "apis": [
        {
            "path": "/resources/user.{format}",
            "description": "Class User"
        },
        {
            "path": "/resources/place.{format}",
            "description": "Class Place"
        },
        {
            "path": "/resources/event.{format}",
            "description": "Class Event"
        },
        {
            "path": "/resources/post.{format}",
            "description": "Class Post"
        },
        {
            "path": "/resources/login.{format}",
            "description": "Class Login"
        },
        {
            "path": "/resources/register.{format}",
            "description": "Class Register"
        }
    ]
}

我还尝试了 composer update 来更新生成的自动加载路由。但不起作用。有人有答案吗?

最好的问候

【问题讨论】:

    标签: php api nginx restler


    【解决方案1】:

    Restler 工作正常,这就是你得到 JSON 输出的原因 :)

    但是您的课程似乎没有加载。

    尝试手动添加它们,看看是否能解决问题!

    那你可以试试/user/{id}看看能不能用!

    【讨论】:

    • 对我不起作用。当我尝试路由时,例如 api.example.com/api/user/705 它不起作用,但正如我在 localhost 上所说的,使用 apache 一切正常。 (localhost/api/user/705)
    • 看起来它根本没有加载你的类。像资源这样的 Restler 类,加载得很好。你确定类是可加载的吗?如果您使用composer,请确保您在服务器上运行composer install
    • 更新了我上面的答案
    • 尝试作曲家安装/更新和转储自动加载。但也不适合我。
    • 看看是否可以在 index.php 中手动包含带有require 的 API 类
    猜你喜欢
    • 1970-01-01
    • 2013-12-27
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 2016-12-27
    相关资源
    最近更新 更多