php - NotFoundHttpException on every route with nginx

108

I try to configure nginx to handle all /{{app}}/query requests dynamically to laravel applications like this:

server {
    listen       80;
    server_name  localhost;
    root   /usr/share/nginx/apps;

    location / {
        index index.php;
        try_files $uri @rewriteapp;
    }

    location @rewriteapp {
        rewrite ^/([^/]+)(.*)$ /$1/public/index.php?$2 last;
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

Currently nginx routes my requests to the correct directory and file. For example:

www.domain.com/test/item/1/edit

... routes to:

/path/to/my/apps/test/public/index.php?/item/1/edit

I'm also able to echo out var_export($_GET) and see that query:

array(
    '/item/1/edit' => '',
)

But Laravel allways says:

NotFoundHttpException in RouteCollection.php line 161:
in RouteCollection.php line 161
at RouteCollection->match(object(Request)) in Router.php line 823
at Router->findRoute(object(Request)) in Router.php line 691
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 57

But why?

The routes looks like this:

Route::get('/', function () {
    return view('welcome');
});

Route::get('/item', function() {
    return 'Item';
});

php artisan route:list returns:

+--------+

My Laravel Version is 5.2.33 I use Ubuntu 16.04 Server with PHP 7.0.6 (PHP-FPM) as Host.

Is it a nginx config issue? Or a Laravel issue? Or is it a Linux, maybe permission related topic?

61

Answer

+---------+------+---------+
873

Answer

--+ | Domain | Method | URI | Name | Action | Middleware | +--------+
194

Answer

+---------+------+---------+
660

Answer

--+ | | GET|HEAD | / | | Closure | web | | | GET|HEAD | item | | Closure | web | +--------+
532

Answer

+---------+------+---------+
680

Answer

--+
960

Answer

Solution:

Point application root to public folder and it will work

275

Answer

Solution:

I found following solution, after some research and trying:

server {

    listen       80;
    server_name  localhost;

    location = / {

        # redirect if no app is specified
        return 301 http://www.domain.com; 
    }

    location ~* ^/([^/]+)(.*)\.(jpe?g|png|svg|gif|ico|doc|docx|xls|xlsx|pdf|txt)$ {

        # handle static files
        alias /usr/share/nginx/apps/$1/public$2.$3;
    }

    location ~* ^/([^/]+)(.*)$ {

        # handle laravel requests
        root /usr/share/nginx/apps/$1/public;

        try_files /index.php =404;

        include         fastcgi_params;
        fastcgi_pass    127.0.0.1:9000;
        fastcgi_param   REQUEST_URI  $2;
        fastcgi_param   SCRIPT_FILENAME  $document_root/index.php;
    }
}

People are also looking for solutions to the problem: php - opendir() failed to open dir, not impemented in

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.