0

Nginx 配置,自定义日志格式 log_format

 1 month ago
source link: https://blog.51cto.com/wangshiyu/10244834
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Nginx 配置,自定义日志格式 log_format

精选 原创

JavaPub 2024-03-28 15:46:28 博主文章分类:nginx ©著作权

文章标签 bash nginx 文章分类 Nginx 服务器 yyds干货盘点 阅读数134

Table of Contents

nginx 记录日志,太方便。这里提供一种小技巧。

配置Nginx

server {
    listen       8080;
    server_name  localhost;
    location /proxy/server {
        default_type text/html;
        return 200 "success";
    }

}

log_format  access escape=json '{ "timestamp": "$msec", '
'"remote_addr": "$remote_addr", '
'"user_agent": "$http_user_agent", '
'"request_body": "$request_body"'
' }';

server {
     listen 443 ssl;
     server_name analysis.drago.plus;
     ssl_certificate /etc/letsencrypt/live/analysis.drago.plus/fullchain.pem;
     ssl_certificate_key /etc/letsencrypt/live/analysis.drago.plus/privkey.pem;
     ssl_session_timeout 5m;
     ssl_protocols TLSv1.2 TLSv1.3;
     ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
     ssl_prefer_server_ciphers on;

    location /eventtrack {
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
        add_header Access-Control-Allow-Headers 'Referer,st,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';

        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

        if ($request_method = 'OPTIONS') {
            return 204;
        }
        proxy_pass http://localhost:8080/proxy/server;

        access_log  /event_track_data/host.access.log  access;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

第一个

我希望的格式,$remote_addr 就是值,可以通过下面这个表查找,不够再去官网继续找。

log_format  access escape=json '{ "timestamp": "$msec", '
'"remote_addr": "$remote_addr", '
'"user_agent": "$http_user_agent", '
'"request_body": "$request_body"'
' }';
参数                      说明                                         示例
$remote_addr             客户端地址                                    211.28.65.253
$remote_user             客户端用户名称                                --
$time_local              访问时间和时区                                18/Jul/2012:17:00:01 +0800
$request                 请求的URI和HTTP协议                           "GET /article-10000.html HTTP/1.1"
$http_host               请求地址,即浏览器中你输入的地址(IP或域名)     www.wang.com 192.168.100.100
$status                  HTTP请求状态                                  200
$upstream_status         upstream状态                                  200
$body_bytes_sent         发送给客户端文件内容大小                        1547
$http_referer            url跳转来源                                   https://www.baidu.com/
$http_user_agent         用户终端浏览器等信息                           "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; GTB7.0; .NET4.0C;
$ssl_protocol            SSL协议版本                                   TLSv1
$ssl_cipher              交换数据中的算法                               RC4-SHA
$upstream_addr           后台upstream的地址,即真正提供服务的主机地址     10.10.10.100:80
$request_time            整个请求的总时间                               0.205
$upstream_response_time  请求过程中,upstream响应时间                    0.002

第二个

使用定义的格式, access , 写到文件 /event_track_data/host.access.log 里。

        access_log  /event_track_data/host.access.log  access;

Nginx 使用:  https://blog.csdn.net/qq_40374604/category_8745762.html

  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK