nginx--日志类型

目录

1.语法

2.常用的 nginx 内置变量

3.测试

4. /etc/nginx/nginx.conf


1.语法

# 官方文档
https://nginx.org/en/docs/http/ngx_http_log_module.html#access_log

error.log
access.log
依赖log_format生成日志
log_format语法
Syntax: log_format name [escape=defaultljson] 
stringDefault: log_format combined "..
Context:http 配置位置在http下


在如下示例中,使用 log_format 指令,定义名为 main 的日志格式,然后在 access_log 中通过 main 引用:
http {
	# 通过变量的方式拼接日志内容
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_s    ent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

}

2.常用的 nginx 内置变量

$arg_PARAMETER:获取 URL 中参数名为 PARAMETER 的值。
$request_uri:获取完整的请求 URI。
$http_user_agent:获取请求客户端的 User Agent 信息。
$remote_addr:获取请求客户端的 IP 地址。
$server_name:获取请求的服务端名称。
$server_port:获取请求的服务端端口。
$request_method:获取请求使用的 HTTP 方法,如 GET、POST 等。

3.测试

# 编辑配置文件 添加 '$request_method - $request_uri'
vim /etc/nginx/nginx.conf


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

access_log  /var/log/nginx/access.log  main;

# 检查配置文件语法是否正确
[root@localhost ~]# nginx -tc /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

# 重启nginx
sudo systemctl restart nginx.service
# 请求本机
curl 127.0.0.1
# 查看日志
tail -f /var/log/nginx/access.log
# 日志打印
GET - /127.0.0.1 - - [12/Apr/2023:15:16:46 +0800] "GET / HTTP/1.1" 200 251 "-" "curl/7.29.0" "-"

4. /etc/nginx/nginx.conf



user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

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

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值