6

I have the following issue, I need to configure Nginx, so on any URL user accesses, it will keep the uri (example domain.com/some/url/), but pass to laravel only / and let Angular handle the routing.

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

And when accessing /api/{anything} Laravel will kick in.

For now I return index.html from public folder until I find solution Here is My Config:

location / {
    index index.html;
    try_files $uri $uri/ /index.html;
}
location /api {
    index index.php;
    try_files $uri $uri/ /index.php?$query_string;
}

I know I can make a route like:

Route::get('{anything?}', function(){
    return view('index');
});

But is to broad.

Update:

location / {
    rewrite ^/(.*)$ / break;
    index index.php;
    try_files $uri $uri/ /index.php;
}
location /api {
    index index.php;
    try_files $uri $uri/ /index.php?$query_string;
}
9
  • Are you looking for NGINX to execute the index.blade.php script on all requests? If so you need fastcgi configured. Commented Jan 26, 2017 at 2:09
  • I have fastcgi, the question is not about how to process php files, but about how to return always / route from laravel but keep URI for angular Commented Jan 26, 2017 at 7:28
  • You can strip the URL with a simple rewrite rule: rewrite ^/(.*)$ / last; Commented Jan 27, 2017 at 23:25
  • @FaisalMemon where should I put it? I tried and had redirect cycle. Commented Jan 28, 2017 at 13:43
  • Sorry, change 'last' to 'break'. It should go in the location where you want it to execute. Commented Jan 28, 2017 at 16:14

1 Answer 1

6
+50

You can't achieve you goal with simple rewrite. Laravel always knows about the real URI.

The key point is that you need to handle all requests with just one route. Laravel uses $_SERVER['REQUEST_URI'] variable to route and it is passed to Laravel from fastcgi. The variable REQUEST_URI is set in fastcgi_params file from nginx's $request_uri variable:

fastcgi_param  REQUEST_URI        $request_uri;

So you need to pass REQUEST_URI as / to Laravel to handle request /bla/bla as it is /.

Just add one line to your config:

location ~ \.php$ {
    # now you have smth like this
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;

    # add the following line right after fastcgi_params to rewrite value of the variable
    fastcgi_param  REQUEST_URI       /;
}

If you have /api/ as well, you need some edits for the line:

set $request_url $request_uri;
if ($request_uri !~ ^/api/(.*)$ ) {
    set $request_url /;
}
fastcgi_param  REQUEST_URI $request_url;

Nginx warns that if is an evil, that's just a first idea.

To sum up:

/ goes to Laravel / route.

/api/* go to Laravel api routes.

Another requests go to Laravel / route.

Sign up to request clarification or add additional context in comments.

4 Comments

Nice, but this also breaks my api routes. They all have prefix /api/
So you have / for angular and /api/(.*) for api. Ok, check the update.
Thank you works, but what you think is better this solution or just create a laravel route with any url?
I prefer Laravel routes. You can read about it here (answer for laravel5) - stackoverflow.com/questions/13297278/… P.S. There is a dirty workaround - without changing nginx remove Laravel route for / and place your view to resources/views/errors/404.blade.php. So api calls will go to api, any other request will go to this view. It just gives 404 error header :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.