【问题标题】:How to fix nginx 404 not found for a Laravel app?如何修复 Laravel 应用程序未找到的 nginx 404?
【发布时间】:2019-01-22 22:51:26
【问题描述】:

我所有的路线都得到了404 Not Found。如果我访问 localhost/mylaravel,我可以打开 Laravel 页面,但如果我访问 localhost/mylaravel/login,我会得到 404 not found 页面。如果我在 /home 上更改回家路线并访问它,我得到 404 not found。

我正在使用 Laravel 框架 5.6.33

这是我的路线文件:

<?php

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

这是我在 /etc/nginx/sites-available/default 的 nginx 配置

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

【问题讨论】:

  • 谁在扔404? nginx还是laravel?考虑到 nginx 根目录应该指向 public/ 目录,就像这样:root /var/www/html/public/; if 您的应用程序目录恰好位于那里。最后,看起来您在同一个服务器块中配置了 2 个应用程序实例,您可以将其重构为 2 个不同的服务器块并避免很多麻烦。
  • @alariva nginx 抛出 404 not found page
  • 指向 public 怎么样?可以发ls -l /var/www/html/ 吗?
  • 我已经指向了public。有2个目录。 mylaravel 和 mylaravel-html
  • ls 的输出呢,可以分享一下吗?

标签: php laravel nginx


【解决方案1】:

你的错误在/etc/nginx/sites-available/default

下面一行:

root /var/www/html/;

应该是

root /var/www/html/public;

你的 webroot 应该设置为 Laravel 项目中的公共目录,因为那是框架的入口点

【讨论】:

    猜你喜欢
    • 2019-08-25
    • 2018-11-06
    • 2015-08-18
    • 2022-01-14
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    相关资源
    最近更新 更多