【问题标题】:Slim endpoint works with php's own server but not nginxSlim 端点适用于 php 自己的服务器,但不适用于 nginx
【发布时间】:2016-07-27 00:31:26
【问题描述】:

我用一个基本的 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;
    }
}

我哪里错了?

【问题讨论】:

    标签: php nginx slim restful-url


    【解决方案1】:

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

    取自他们的Documentation:

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

    【讨论】:

      猜你喜欢
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多