Issue
I am using PHP to develop a website, specifically using YII 2 framework. The web server is using Nginx
, PHP engine
is php-fpm. The question is I want to track which user accessed which webpage
URL address with parameters. Yii2
itself has a logging system, but that's mainly for error and debug purpose. I know nginx has logging system can record access log, but I need to record the exact userid, username which is in my database, I think nginx
cannot record this.
I think of to record these info into another Mysql table when everytime user access a page, but I am worried the table may grow very quickly, seems using relational database may not be a good idea.
Should I use Hadoop? Or other common practice to solve this kind of problem? Thanks for any advice.
Solution
If you want to
track which user accessed which webpage URL address with parameters,
nginx access log is a better choice because mysql will hurt your performance a lot.
Assume user id is a parameter uid
in query string, you can custom nginx's access log by adding a field $arg_uid
. If user id is part of uri, you may need config location with a regexp to capture it.
If user id can not be obtained from request directly, there must be a token correlated with one and only one user which can be retrived from mysql or redis. In this case, there are 2 methods:
- return user id as a header for example X-UID in php, then add variable
$upstream_http_X_UID
in access log. If you do not want to return this header to clients, add a directiveproxy_hide_header X-UID;
to delete this header from php response. - build nginx with nginx-lua module and write a piece of lua script for
log_by_lua
directive in which query the user id from mysql or redis according to that token. Then custom nginx's access log by adding the user id variable.
According to your specific scene, choose the best solution. I hope this helps you.
Answered By - mononoke
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.