Ayush
Ayush

Reputation: 709

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

I made a very basic slim app with just a basic hello world GET endpoint.

<?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();

The /hello/world endpoint works as it is supposed to when I run it with PHP's built in server. But not with nginx. I get a 404 not found.

My nginx_vhost (/etc/nginx/sites-available/nginx_vhost) file looks like this :

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;
    }
}

Where am I going wrong?

Upvotes: 2

Views: 202

Answers (1)

Darren
Darren

Reputation: 13128

You need to modify your nginx_vhost file to allow arguments to be passed to Slim as required.

Taken from their Documentation:

server {
    #..... 

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

    #....
}

Upvotes: 1

Related Questions