Home > Backend Development > PHP Tutorial > Common Nginx logs and configuration methods in PHP

Common Nginx logs and configuration methods in PHP

不言
Release: 2023-04-03 14:02:02
Original
1800 people have browsed it

The content of this article is about common Nginx logs and setting methods. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Preface

As a programmer, something a little more important than coding is log analysis and query. Common logs and setting methods are listed below.

Configuration file

nginx is divided into two logs: access_log and error_log

The settings need to be in nginx.conf, and the default is passed The source code package compilation and installation nginx directory should be in the

/usr/local/nginx
Copy after login

directory. If you install it through yum or other methods and do not know or do not know the specific nginx installation directory, you can use

find / -name nginx.conf
Copy after login

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_module
Copy after login

Open access log

If your source code package is installed by default, open the path as follows

vim /usr/local/nginx/nginx.conf
Copy after login
Copy after login

Find the following content

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 after login

Change log_format Just open the comment of access_log. Log_format can define the log specifications of nginx.

log_format default specification parameter table

Name Annotation
$remote_addr Client/user IP address
$time_local Access time
$request Request method Request address
$status The request status code is consistent with the HTTP status code
$body_bytes_sent The requested address size is calculated in bytes format
$http_referer Request source, from what Local access
$http_user_agent User information (browser information)
$http_x_forwarded_for Forward IP address

Enable error log

If your source code package is installed by default, open the path as follows

vim /usr/local/nginx/nginx.conf
Copy after login
Copy after login

Find the following content

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
Copy after login

Just delete the annotation. You can store different error types separately. For example,

error_log logs/error.log notice;
Copy after login

notice is an error type. If you don’t write it, it will be all.

Recommended related articles:

Nginx common log splitting methods nginx apache nginx php nginx rewrite

nginx common configuration

The above is the detailed content of Common Nginx logs and configuration methods in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template