Laravel + AngularJS Nginx routing
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.