欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

nginx log format log_format logformat domain name 域名 有大用

We use following nginx site configure file in our production env.

log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" $request_time';



server {
        root /srv/www/web;
        server_name *.test.com;
        access_log /var/log/nginx/xxx.test.com.access.log main;

Both "http://a.test.com/ping" and "http://b.test.com/ping" http request will be record in file xxx.test.com.access.log.

But there is a problem, nginx don't store "domain name" in xxx.test.com.access.log.

"http://a.test.com/ping" and "http://b.test.com/ping" share the same request "Get /ping".

How can I record "a.test.com" or "b.test.com" in nginx log?


3 Answers 正确答案 

Try adding the $host variable in log_format:  $host

log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$host" "$request" '
            '$status $body_bytes_sent "$http_referer" '
            '"$http_user_agent" $request_time';

http://wiki.nginx.org/HttpCoreModule#.24host:

$host

This variable is equal to line Host in the header of request or name of the server processing the request if the Host header is not available.

This variable may have a different value from $http_host in such cases: 1) when the Host input header is absent or has an empty value, $host equals to the value of server_name directive; 2) when the value of Host contains port number, $host doesn't include that port number. $host's value is always lowercase since 0.8.17.

shareimprove this answeredited Apr 14 '16 at 14:38robsch4,70733768answered Jan 15 '14 at 13:44Tan Hong Tat4,20821521

If you want to log the full requested url, then my method is

log_format main '$http_x_forwarded_for - $remote_user [$time_local] '
'"$request_method $scheme://$host$request_uri $server_protocol" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $request_time';

So splitting the $request into its constituent parts and popping $host in the middle. Also lets you see if the request is http is https.

shareimprove this answeredited Jun 17 '16 at 9:21answered Jun 17 '16 at 8:57user15122018123

You can also try adding $http_host

$host is a variable of the Core module.

$host

This variable is equal to line Host in the header of request or name of the server processing the request if the Host header is not available.

This variable may have a different value from $http_host in such cases: 1) when the Host input header is absent or has an empty value, $host equals to the value of server_name directive; 2)when the value of Host contains port number, $host doesn't include that port number. $host's value is always lowercase since 0.8.17.

$http_host is also a variable of the same module but you won't find it with that name because it is defined generically as $http_HEADER (ref).

$http_HEADER

The value of the HTTP request header HEADER when converted to lowercase and with 'dashes' converted to 'underscores', e.g. $http_user_agent, $http_referer...;


Summarizing:

  • $http_host equals always the HTTP_HOST request header.

  • $host equals $http_hostlowercase and without the port number (if present), except when HTTP_HOST is absent or is an empty value. In that case, $host equals the value of the server_name directive of the server which processed the request.

shareimprove this answeranswered Aug 17 '16 at 10:58Pankaj Singhal3,00831931

来自  https://stackoverflow.com/questions/21135719/full-record-url-in-nginx-log

普通分类: