简体   繁体   中英

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

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;
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM