How to fix nginx 404 not found for a Laravel app?

Rahadian Wiratama

All my routes got 404 Not Found. I can open Laravel page if I access the localhost/mylaravel, but if I access localhost/mylaravel/login, I got 404 not found page. If I change route home on /home and access it, I got 404 not found.

I'm using Laravel Framework 5.6.33

This is my routes file:

<?php

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

And this is my nginx config at /etc/nginx/sites-available/default

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name localhost;
    charset utf-8;
    root /var/www/html/;
    index index.php index.html index.htm index.nginx-debian.html;

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

    location ~ \.php$ {
    try_files $uri $uri/ =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$; 
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass    unix:/var/run/php/php7.2-fpm.sock;
    fastcgi_index   index.php;
    include fastcgi_params;
    }

    location ~ /\. {
        deny all;
    }

    location ~* ^.+\.(css|js|jpg|jpeg|gif|png|txt|ico|swf|xml|woff|woff2|ttf|mp3|svg|csv|xls|xlsx|eot|otf)$ {
    access_log off;
    expires modified +90d;
    }
}
Alexander

Your mistake is at /etc/nginx/sites-available/default

The following line:

root /var/www/html/;

should be

root /var/www/html/public;

Your webroot should be set to the public directory within the Laravel project, because that is the entry point of the framework

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related