Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel + AngularJS Nginx routing

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;
}
like image 695
Froxz Avatar asked Jan 25 '17 18:01

Froxz


People also ask

What is Nginx in Laravel?

Laravel nginx is the php based free and open-source web server which was used to run the application. nginx is very useful and important to run the laravel applications. nginx web server consumes less CPU and memory. This is a guide to laravel nginx.

Where does routing happen in Nginx?

However, the routing happens within the application and not at the web server. Since the routes are not visible to the web server they will not be served by the web server. This tutorial will show you how to configure NGINX for your Angular or React applications.

How to develop angular in Laravel on Linux?

If Linux is your preferred development method, there’s a Laravel Valet for Linux version too. Developing Angular locally: if you’re in a terminal you can cd into the angular project folder and run: For building the project for production: if you’re in a terminal you can cd into the angular project folder and run:

Where are routes in Laravel?

All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by your application's App\Providers\RouteServiceProvider. The routes/web.php file defines routes that are for your web interface.


1 Answers

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.

like image 62
shukshin.ivan Avatar answered Oct 23 '22 01:10

shukshin.ivan