[solved] Nginx with Slim - php file gets downloaded


#1

Hey people,
this might be a rather silly question since I’m new to Slim but I haven’t been able to work this out for the last few hours.

In short I’m just trying to implement the basic code-example with the nginx webserver on a raspberry pi with a dynamic DNS configured.

My structure and code looks like this:

/home/some_user/slim_project/public/index.php

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require '../vendor/autoload.php';

$app = new \Slim\App;
$app->get('/hello/{name}', function (Request $request, Response $response, 
array $args) {
    $name = $args['name'];
    $response->getBody()->write("Hello, $name");

    return $response;
});
$app->run();

and the nginx config:

/etc/nginx/sites-enabled/default

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /home/some_user/slim_test/public/;

        server_name mySlimTest1.ddns.net;

        location / {
                if (!-e $request_filename){
                        rewrite ^(.*)$ /index.php break;
                }
                try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        }
}

Whatever I try, all that happens is the php-file getting downloaded or in some cases I get a 404 or Page Not Found when I add the exemplary ‘/hello/Name’ to the end of the server url
(mySlimTest1.ddns.net/hello/Name or mySlimTest1.ddns.net/index.php/hello/Name).

I am obviously missing something here. Any help would be a appreciated.


#2

The issue was that the rewrite ... break caused the URL to be processed in the same location. rewrite ... last should have been used instead when redirecting to a .php from a different location block.