Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Nginx access log (access_log) configuration and detailed information explanation

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Nginx access log (access_log) configuration and detailed information explanation

By accessing the log, you can know the user's address, which parts of the site are the most popular, the user's browsing time, and optimize the browsers used by most users.

Introduction to Nginx access Log (access_log)

Nginx will record the log information that each user visits to us in the designated log file for the webmaster to analyze the user's browsing behavior and so on. This function is also responsible for the ngx_http_log_module module.

Access log parameters

The Nginx access log is mainly controlled by two parameters.

Log_format # is used to define the log format (multiple log formats can be defined with different names)

Access_log # is used to specify the path to the daily solstice file and what log format to use for logging

Default values for lof_format:

# log_format main'$remote_addr-$remote_user [$time_local] "$request"'

#'$status $body_bytes_sent "$http_referer"'

#'"$http_user_agent"$http_x_forwarded_for"'

Default values for access_log:

# access_log logs/access.log main

The syntax format of log_format and parameter syntax are described as follows:

Log_format

Keyword format label log format

Keyword: where the keyword error_log cannot be changed

Format tags: format tags give a unique name to a set of log formats

Log format: formatting logs

Log_format format variables:

$remote_addr # record the client address of visiting the website

$remote_user # remote client user name

$time_local # record access time and time zone

$request # user's http request start line information

$status # http status code, which records the status code returned by the request, such as 200,301,404, etc.

$body_bytes_sent # number of response body bytes sent by the server to the client

$http_referer # records the connection from which the request was accessed, and hotlink protection can be set according to this parameter.

$http_user_agent # record client access information, such as browser, mobile client, etc.

$http_x_forwarded_for # when there is a proxy server at the current end, set the web node to record the configuration of the client address. This parameter takes effect only if the proxy server also makes relevant x_forwarded_for settings.

The syntax format of access_log and parameter syntax are described as follows:

Access_log

Keyword log file format label

Keyword: where the keyword error_log cannot be changed

Log files: you can specify any directory where logs are stored

Format label: applies the specified log format to the log file

Other grammars:

Access_log off; # closes access_log, that is, no access log is recorded

Access_log path [format [buffer=size [flush=time]] [if=condition]]

Access_log path format gzip [= level] [buffer=size] [flush=time] [if=condition]

Access_log syslog:server=address [, parameter=value] [format [if=condition]]

Description:

Buffer=size # is the size of the buffer where access logs are stored

Flush=time # is the time when the buffer log is flushed to disk

Gzip [= level] # indicates the compression level

[if = condition] # indicates other conditions

In general, these parameters do not need to be configured, and these parameters can only be taken into account in extreme optimization.

The label segment location of the lof_format parameter:

Http

The label segment location of the access_log parameter:

Http, server, location, if in location, limit_except

Reference: http://nginx.org/en/docs/http/ngx_http_log_module.html

Introduction to the procedure of Nginx configuration access log (1) create log_format statement

Vi conf/nginx.conf

# vi edit the nginx main configuration file and add the log_format format labeled main (within the http tag, it can be called within all server tags)

Contents of the file:

Worker_processes 1

Error_log logs/error.log error

Events {

Worker_connections 1024

}

Http {

Include status.conf

Include mime.types

Default_type application/octet-stream

Sendfile on

Keepalive_timeout 65

Log_format main'$remote_addr-$remote_user [$time_local] "$request"'

'$status $body_bytes_sent "$http_referer"'

'"$http_user_agent"$http_x_forwarded_for"'

Access_log logs/access.log main

Server {

Listen 80

Server_name localhost

Rewrite ^ /. * http://www.abc.com permanent

}

Include vhost/*.conf

}

(2) insert access_log statement

Vi conf/vhost/www.abc.com.conf

# vi Editing Virtual Host configuration File

Contents of the file:

Server {

Access_log / data/log/www

Listen 80

Server_name abc.com www.abc.com

Location / {

Root / data/www/www

Index index.html index.htm

}

Error_log logs/error_www.abc.com.log error

Access_log logs/access_www.abc.com.log main

# what's new, ↑

}

(3) restart the service

Restart after confirming that it is correct, as follows:

Nginx-t

# results show that ok and success can be restarted if there is no problem

Nginx-s reload

(4) View the access log file

Ll logs/access_www.abc.com.log

-rw-r--r-- 1 root root 2305 Jun 13 18:25 logs/access_www.abc.com.log

Check whether the file is produced, and the configuration is successful when the file is generated.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report