如何让nginx显示错误日志?

Posted

技术标签:

【中文标题】如何让nginx显示错误日志?【英文标题】:How to let nginx show the error log? 【发布时间】:2016-06-06 17:24:49 【问题描述】:

由于我正在使用 docker 开发我的项目,并且安装了 nginx 的图像,如果发生任何错误,我只会看到这个:

502 Bad Gateway

nginx/1.4.6 (Ubuntu)

目前,查看发生了什么的唯一方法是检查storage/logs,这很好,但不应该在浏览器中查看日志错误,而不仅仅是查看502 Bad Gateway

在我的 .env 中:

APP_ENV=local
APP_DEBUG=true

有什么想法吗?

编辑

如果我的应用程序没有错误,我会在浏览器中正常看到它,只是当我在 Laravel 应用程序中出现错误时看到 502 Bad Gateway 错误

错误是:

[2016-02-24 11:59:38] local.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Class 'App\Http\Controllers\Redirect' not found' in /share/app/Http/Controllers/NodesController.php:68
Stack trace:
#0 /share/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(133): Symfony\Component\Debug\Exception\FatalErrorException->__construct('', '', '', '', '', '', '', '')
#1 /share/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(118): Illuminate\Foundation\Bootstrap\HandleExceptions->fatalExceptionFromError('', '')
#2 /share/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(0): Illuminate\Foundation\Bootstrap\HandleExceptions->handleShutdown()
#3 /share/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(76): App\Http\Controllers\NodesController->store('')
#4 /share/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(76): call_user_func_array('', '')
#5 /share/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(146): Illuminate\Routing\Controller->callAction('', '')
#6 /share/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(94): Illuminate\Routing\ControllerDispatcher->call('', '', '')
#7 /share/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): Illuminate\Routing\ControllerDispatcher->Illuminate\Routing\closure('')
#8 /share/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): call_user_func('', '')
#9 /share/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Routing\Pipeline->Illuminate\Routing\closure('')
#10 /share/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): call_user_func('', '')
#11 /share/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(96): Illuminate\Pipeline\Pipeline->then('')
#12 /share/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(54): Illuminate\Routing\ControllerDispatcher->callWithinStack('', '', '', '')
#13 /share/vendor/laravel/framework/src/Illuminate/Routing/Route.php(174): Illuminate\Routing\ControllerDispatcher->dispatch('', '', '', '')
#14 /share/vendor/laravel/framework/src/Illuminate/Routing/Route.php(140): Illuminate\Routing\Route->runController('')
#15 /share/vendor/laravel/framework/src/Illuminate/Routing/Router.php(724): Illuminate\Routing\Route->run('')
#16 /share/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): Illuminate\Routing\Router->Illuminate\Routing\closure('')
#17 /share/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): call_user_func('', '')
#18 /share/app/Http/Middleware/LocaleMiddleware.php(48): Illuminate\Routing\Pipeline->Illuminate\Routing\closure('')
#19 /share/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): App\Http\Middleware\LocaleMiddleware->handle('', '')
#20 /share/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array('', '')
#21 /share/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure('')
#22 /share/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func('', '')
#23 /share/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(64): Illuminate\Routing\Pipeline->Illuminate\Routing\closure('')
#24 /share/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle('', '')
#25 /share/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array('', '')
#26 /share/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure('')
#27 /share/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func('', '')
#28 /share/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\Routing\Pipeline->Illuminate\Routing\closure('')
#29 /share/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): Illuminate\View\Middleware\ShareErrorsFromSession->handle('', '')
#30 /share/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array('', '')
#31 /share/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure('')
#32 /share/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func('', '')
#33 /share/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(62): Illuminate\Routing\Pipeline->Illuminate\Routing\closure('')
#34 /share/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): Illuminate\Session\Middleware\StartSession->handle('', '')
#35 /share/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array('', '')
#36 /share/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure('')
#37 /share/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func('', '')
#38 /share/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\Routing\Pipeline->Illuminate\Routing\closure('')
#39 /share/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle('', '')
#40 /share/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array('', '')
#41 /share/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure('')
#42 /share/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func('', '')
#43 /share/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(59): Illuminate\Routing\Pipeline->Illuminate\Routing\closure('')
#44 /share/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): Illuminate\Cookie\Middleware\EncryptCookies->handle('', '')
#45 /share/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array('', '')
#46 /share/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure('')
#47 /share/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func('', '')
#48 /share/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Routing\Pipeline->Illuminate\Routing\closure('')
#49 /share/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): call_user_func('', '')
#50 /share/vendor/laravel/framework/src/Illuminate/Routing/Router.php(726): Illuminate\Pipeline\Pipeline->then('')
#51 /share/vendor/laravel/framework/src/Illuminate/Routing/Router.php(699): Illuminate\Routing\Router->runRouteWithinStack('', '')
#52 /share/vendor/laravel/framework/src/Illuminate/Routing/Router.php(675): Illuminate\Routing\Router->dispatchToRoute('')
#53 /share/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(246): Illuminate\Routing\Router->dispatch('')
#54 /share/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\closure('')
#55 /share/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): call_user_func('', '')
#56 /share/vendor/barryvdh/laravel-debugbar/src/Middleware/Debugbar.php(49): Illuminate\Routing\Pipeline->Illuminate\Routing\closure('')
#57 /share/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): Barryvdh\Debugbar\Middleware\Debugbar->handle('', '')
#58 /share/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array('', '')
#59 /share/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure('')
#60 /share/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func('', '')
#61 /share/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(44): Illuminate\Routing\Pipeline->Illuminate\Routing\closure('')
#62 /share/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle('', '')
#63 /share/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array('', '')
#64 /share/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure('')
#65 /share/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func('', '')
#66 /share/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Routing\Pipeline->Illuminate\Routing\closure('')
#67 /share/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): call_user_func('', '')
#68 /share/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(132): Illuminate\Pipeline\Pipeline->then('')
#69 /share/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(99): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter('')
#70 /share/public/index.php(54): Illuminate\Foundation\Http\Kernel->handle('')
#71 /share/public/index.php(0): main()
#72 main  

EDIT2

nginx配置:

server 
        client_max_body_size 500M;
        listen   80 default_server;

        root /share/public/;
        index index.php index.html index.htm;

        location / 
             try_files $uri $uri/ /index.php$is_args$args;
        

        # pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
        location ~ \.php$ 
                try_files $uri /index.php =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
                add_header Cache-Control no-cache;
        

【问题讨论】:

你的 laravel 日志中的实际错误是什么? 如果您的/var/log/nginx/error.log 日志中的错误类似于:<date> [error] <id>#0: *16 upstream sent too big header while reading response header from upstream... 那么也许this SO answer 可以帮助您。 任何与 laravel 相关的错误,例如:[2016-02-24 11:59:38] local.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Class 'App\ /share/app/Http/Controllers/NodesController.php:68 中的 Http\Controllers\Redirect '未找到' 好的,/var/log/nginx/error.log的错误是什么? 这就是为什么我们对 nginx 在您的/var/log/nginx/error.log 中记录的错误感到好奇的原因(除了 PHP 错误,如果它们也存在的话。)例如 docker exec -it <your-container-id> cat /var/log/nginx/error.log(您可以获取 docker 容器id 或 name 通过docker ps) 或者只是进入容器并查看文件内容。 【参考方案1】:

502 Bad Gateway 是由于网络服务器配置不正确造成的。您的请求由 Nginx 处理,它无法将其重定向到 PHP 或 Laravel。

此错误的原因很可能是您的 Nginx 配置中缺少或不正确的代理转发到 PHP。

这些错误仅记录在 Nginx 错误日志 (/var/log/nginx/error.log) 中,而不记录在 Laravel 日志中,因为您的应用和 php 没有处理请求。

【讨论】:

这就是为什么我想知道问题中的以下内容:Currently, the only way to see what's going on is checking storage/logs which is fine @Ciccio 也让我感到困惑,但是 PHP 和 Laravel 永远不会处理坏网关,所以他永远不会在存储日志目录中看到任何问题注册,相信我 ;) 啊,好吧,这是有道理的。我忘记了应用程序中触发502的此类异常。仍然感觉好像Nginx配置不正确。代理的 nginx 配置是什么?你能发一下@simo【参考方案2】:

错误发生在 nginx 而不是 PHP 中,这就是您在浏览器中看不到它们的原因。

您可以检查 /var/log/nginx/ 文件夹中的 nginx 日志是否有错误,并清空 storage/logs 文件夹以确保其中没有新错误。

修复 nginx 错误后,您可以配置 PHP 以根据 https://***.com/a/5438125/50475 向浏览器显示所有错误

【讨论】:

以上是关于如何让nginx显示错误日志?的主要内容,如果未能解决你的问题,请参考以下文章

配置 Nginx 的访问日志和错误日志

Django + nginx + gunicorn 给出 502 错误。日志信息很少[关闭]

nginx日志详解

nginx 错误日志在哪

nginx php-fpm记录php错误日志怎么配置

nginx访问错误日志详解