Slim 端点适用于 php 自己的服务器,但不适用于 nginx

Slim endpoint works with php's own server but not nginx

我用一个基本的 hello world GET 端点制作了一个非常基本的超薄应用程序。

<?php

require 'vendor/autoload.php';

$app = new Slim\App();

$app->get('/hello/{name}', function ($request, $response, $args) {
    $response->write("Hello, " . $args['name']);
    return $response;
});

$app->run();

当我 运行 使用 PHP 的内置服务器时,/hello/world 端点正常工作。 但不是用 nginx。我收到 404 未找到。

我的 nginx_vhost (/etc/nginx/sites-available/nginx_vhost) 文件如下所示:

server {
    listen 80;
    server_name localhost;

    root /var/www/;
    index index.php index.html;

    # Important for VirtualBox
    sendfile off;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~* \.php {
        include fastcgi_params;

        fastcgi_pass unix:/var/run/php5-fpm.sock;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_cache off;
        fastcgi_index index.php;
    }
}

我哪里错了?

您需要修改您的 nginx_vhost 文件以允许根据需要将参数传递给 Slim。

摘自他们的 Documentation:

server {
    #..... 

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

    #....
}