【问题标题】:Laravel - 404 Not FoundLaravel - 404 未找到
【发布时间】:2020-05-25 15:47:50
【问题描述】:

我最近几天才开始使用 Laravel,但我的 URL 路由有问题,我的 http://localhost:8017/laravel 看起来不错,但 http://localhost:8017/laravel/foo 返回

404 未找到

我的操作系统 Ubuntu 16.04、Laravel 5.4

routes/web.php

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

// oedin
Route::get('foo', function () {
    return 'Hello World';
});

nginx 配置

server {
    listen 8017 default_server;
    listen [::]:8017 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    #root /var/www/html;
    root /home/oedin/webdev;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm;

    server_name localhost;

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

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        #try_files $uri /index.php =404;
        #fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        #fastcgi_index index.php;
        #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #include fastcgi_params;

    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
}

【问题讨论】:

标签: php laravel nginx


【解决方案1】:

如果您想使用 nginx 为您的应用程序提供服务,建议您创建一个新的 nginx vhost 配置,其中包含您的项目根文件夹。

不过,php artisan serve 可以让生活更轻松,此时不需要 nginx 服务器。

【讨论】:

【解决方案2】:

如果你附加 index.php,我想它会起作用。

http://localhost:8017/laravel/index.php/foo

【讨论】:

  • 这很奇怪,您是否更改了公共目录下 index.php 文件中的任何内容?
【解决方案3】:

问题与服务器有关,您需要 Apache 中的模式覆盖之类的东西。在 nginx 中没有 equilivment 但尝试这样的事情

How to convert Apache's `AllowOverride all` to nginx

【讨论】:

  • 之前我用的是 Apache,但是我的 pm 决定用 nginx
【解决方案4】:

你可以试试这个:

Route::group(['namespace' => 'Laravel', 'prefix' => 'laravel', 'middleware' => 'laravel'], function () {

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

    // oedin
    Route::get('foo', function () {
        return 'Hello World';
    });
});

你应该在终端运行php artisan serv 命令

your URL like http://localhost:8000/laravel/foo

希望这对你有用!!!

【讨论】:

    【解决方案5】:

    这可能是由于查询错误,比如我的情况,试图通过错误报告显示错误,日志中没有任何内容,但我尝试在查询中捕获错误异常,得到 SQL 错误..!

    希望对您有所帮助,

    使用 try-catch 语句包装您希望捕获异常的代码行

    enter code here
    
    try
    {
    //write your codes here
    }
    catch(Exception $e)
    {
       dd($e->getMessage());
    }
    

    不要忘记在控制器顶部包含异常类

    使用异常;

    【讨论】:

      猜你喜欢
      • 2021-03-21
      • 2019-10-20
      • 2021-02-27
      • 1970-01-01
      • 2021-08-06
      • 1970-01-01
      • 2020-10-15
      • 2020-03-01
      • 2019-02-01
      相关资源
      最近更新 更多