如何在nginx的访问日志中添加Laravel用户名?
How to add Laravel username to the access log of nginx?
有人知道如何将我的用户 Laravel 的用户名包含到访问日志中吗?
我在 Ubuntu 16.04.
上使用 Laravel 5.2 和 Nginx
我知道如何将数据添加到 Nginx 的访问日志中,但是,如何将信息(在本例中为用户名)从 Laravel 传递到 Nginx 以便将该信息添加到访问日志中?
谢谢
我这里可能是错的,但据我所知这是不可能的。访问日志由服务器 (nginx) 写入以在服务器上记录 activity,而不是记录在服务器上运行的应用程序的细节。
如果您需要记录哪些用户访问您的 Laravel 应用程序,您可能应该为此创建一个单独的日志文件。对于此 "application log" 中的 link 个条目(让我们这样称呼它)与访问日志中的条目,您可以编写一个算法,通过比较时间戳和 IP 地址来 links 这些条目。我很清楚这不是一个非常优雅的解决方案,但它可能是最快/最简单的解决方案之一。
我一直在做更多的研究,并在这里和那里找到了一些小片段来实现实际的解决方案。
描述
我想到的解决方案是使用中间件设置一个包含用户名的 header,然后创建一个包含 header 内容的日志格式,然后清除header 这样它就不会显示给用户。
注意:此代码使用Laravel 5.2,您可能需要为Laravel 5.3或其他版本稍作调整。
设置header
在 Laravel 中创建一个中间件,在 header 中设置用户名,如下所示:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request;
class UserHeader
{
protected $auth;
public function __construct(Guard $auth)
{
$this->auth = $auth;
}
public function handle(Request $request, Closure $next)
{
$response = $next($request);
$user = $this->auth->user();
if ($user) {
$response->headers->set('X-Username', $user->name);
} else {
$response->headers->set('X-Username', 'Anonymous');
}
return $response;
}
}
然后在app/Http/Kernel.php
中注册中间件
protected $middleware = [
// ....
UserHeader::class,
];
这会在响应中添加这样的 header:
X-Username: Administrator
使用header
现在,我们在 Nginx 中处理这个 header。
我们创建一个日志格式来使用该变量,我们在 /etc/nginx/nginx.conf
中的格式中包含 $sent_http_x_username
log_format with_user_combined '$remote_addr - $sent_http_x_username [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'$pipe';
然后我们在访问日志中使用该格式 /etc/nginx/sites-enabled/mysite.com
:
access_log /var/log/nginx/access-mysite.com.log with_user_combined;
可选
然后,我们清除 header,这样它就不会传递给用户:
more_clear_headers 'X-Username';
将 header 留在那里并不代表任何安全问题,只要您从未决定将该信息强加到您的逻辑的任何部分。
这也可以应用于包含在我们的日志中可能有用的其他类型的数据。
我希望这对其他人有帮助。
有人知道如何将我的用户 Laravel 的用户名包含到访问日志中吗? 我在 Ubuntu 16.04.
上使用 Laravel 5.2 和 Nginx我知道如何将数据添加到 Nginx 的访问日志中,但是,如何将信息(在本例中为用户名)从 Laravel 传递到 Nginx 以便将该信息添加到访问日志中?
谢谢
我这里可能是错的,但据我所知这是不可能的。访问日志由服务器 (nginx) 写入以在服务器上记录 activity,而不是记录在服务器上运行的应用程序的细节。
如果您需要记录哪些用户访问您的 Laravel 应用程序,您可能应该为此创建一个单独的日志文件。对于此 "application log" 中的 link 个条目(让我们这样称呼它)与访问日志中的条目,您可以编写一个算法,通过比较时间戳和 IP 地址来 links 这些条目。我很清楚这不是一个非常优雅的解决方案,但它可能是最快/最简单的解决方案之一。
我一直在做更多的研究,并在这里和那里找到了一些小片段来实现实际的解决方案。
描述
我想到的解决方案是使用中间件设置一个包含用户名的 header,然后创建一个包含 header 内容的日志格式,然后清除header 这样它就不会显示给用户。
注意:此代码使用Laravel 5.2,您可能需要为Laravel 5.3或其他版本稍作调整。
设置header
在 Laravel 中创建一个中间件,在 header 中设置用户名,如下所示:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request;
class UserHeader
{
protected $auth;
public function __construct(Guard $auth)
{
$this->auth = $auth;
}
public function handle(Request $request, Closure $next)
{
$response = $next($request);
$user = $this->auth->user();
if ($user) {
$response->headers->set('X-Username', $user->name);
} else {
$response->headers->set('X-Username', 'Anonymous');
}
return $response;
}
}
然后在app/Http/Kernel.php
protected $middleware = [
// ....
UserHeader::class,
];
这会在响应中添加这样的 header:
X-Username: Administrator
使用header
现在,我们在 Nginx 中处理这个 header。
我们创建一个日志格式来使用该变量,我们在 /etc/nginx/nginx.conf
$sent_http_x_username
log_format with_user_combined '$remote_addr - $sent_http_x_username [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'$pipe';
然后我们在访问日志中使用该格式 /etc/nginx/sites-enabled/mysite.com
:
access_log /var/log/nginx/access-mysite.com.log with_user_combined;
可选
然后,我们清除 header,这样它就不会传递给用户:
more_clear_headers 'X-Username';
将 header 留在那里并不代表任何安全问题,只要您从未决定将该信息强加到您的逻辑的任何部分。
这也可以应用于包含在我们的日志中可能有用的其他类型的数据。
我希望这对其他人有帮助。