Search code examples
laravelwindowsnginxlaragon

Laravel Route in NGINX on Windows machine didn't work well


I just install my nginx server on my windows laptop. Then I setup the nginx.conf file like this :

server {
    listen       80;
    server_name  laravelninja.local;
    root         C:/blablabla/public;
    index        index.php;

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

running the php-cgi using this syntax php-cgi.exe -b 127.0.0.1:9000

and the hosts also

127.0.0.1   laravelninja.local

It's running well at laravelninja.local/, but when I go to other route like laravelninja.local/pizzas, this error came out from nginx

2020/12/23 21:26:52 [error] 8980#11972: *7 CreateFile() "C:/blablabla/public/pizzas" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: laravelninja.local, request: "GET /pizzas HTTP/1.1", host: "laravelninja.local"

and the browser goes to google and search the laravelninja.local/pizzas

this is the code in my route :

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

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

and the same level view of pizzas.blade.php as welcome.blade.php on the views folder.

is there any other option to solve this problem except using laragon ?


Solution

  • Add these 2 blocks in your config like this:

    server {
    
        #
        # your configs...
        #
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
        location ~ /\.ht {
            deny all;
        }
    }
    

    Restart your NGINX server.

    (I'm not sure about your other configs you have, but I believe the first block is what you want.)