preface

Nginx and Apache log, but I do not recommend apache(purely personal hobby), here will not introduce apache log.

As a programmer, the only thing slightly more important than code is log analysis and query. Common logging and setting methods are listed below.

The configuration file

Nginx logs are classified into access_log and error_log

The Settings need to be in nginx.conf, the default source package compiled by install nginx directory should be in

/usr/local/nginx
Copy the code

If you do not know the specific directory where nginx is installed, you can use it

find / -name nginx.conf
Copy the code

or

nginx -V | grep prefix ------------- nginx version: Nginx /1.13.9 BUILT by GCC 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) Built with OpenSSL 1.0.2K-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx --with-http_ssl_moduleCopy the code

Enabling Access Logs

If your source package is installed by default, open it in the following path

vim /usr/local/nginx/nginx.conf
Copy the code

Find the following

http { include mime.types; default_type application/octet-stream; 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; . }Copy the code

Turn on the log_format comment to access_log. Log_format defines nginx log specifications.

Log_format Default specification parameter list

The name of the annotations
$remote_addr IP address of the client/user
$time_local Access time
$request Request mode + Request address
$status The request status code is the same as the HTTP status code
$body_bytes_sent The requested address size is calculated in bytes format
$http_referer The source of the request, where was it accessed
$http_user_agent User information (browser information)
$http_x_forwarded_for Forwarding IP address

Enabling error logging

If your source package is installed by default, open it in the following path

vim /usr/local/nginx/nginx.conf
Copy the code

Find the following

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
Copy the code

Delete the annotations. You can store the different error types separately, as shown in the following example

error_log logs/error.log notice;
Copy the code

Notice is an error type and nothing is written.

Thank you

Thank you for reading this. I will write more articles about logging operations and analysis later. I hope this will help you. thank you

communication

There is life, there is code.

Spread the positive energy of technology and continue to learn new knowledge.