Nginx高级配置-自定义json格式日志

                                       作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

  在大数据运维工作中,我们经常会使用flume,filebeat相关日志收集工具取收集日志,但这些日志在收集前都日志基本上都是json格式的,通过flume收集日志到hdfs集群,开发人员就直接使用java,scala语言取处理日志,有的时候会使用到spark,fink等框架去处理日志。因此nginx配置为json格式还是非常有必要的。

  访问日志是记录客户端即用户的具体请求内容信息,全局配置模块中的error_log是记录nginx服务器运行时的日志保存路径和记录日志的level,因此有着本质的区别,而且Nginx的错误日志一般只有一个,但是访问日志可以在不同server中定义多个,定义一个日志需要使用access_log指定日志的保存路径,使用log_format指定日志的格式,格式中定义要保存的具体日志内容。

一.自定义默认格式日志

如果是要保留日志的源格式,只是添加相应的日志内容,则配置如下。

关于nginx日志使用的变量名称含义,博主推荐阅读:
  https://www.cnblogs.com/yinzhengjie/p/12046613.html

1>.编写主配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
worker_processes 4;
worker_cpu_affinity 00000001 00000010 00000100 00001000; events {
worker_connections 100000;
use epoll;
accept_mutex on;
multi_accept on;
} http {
include mime.types; default_type text/html; charset utf-8; log_format my_default_format '$remote_addr - $remote_user [$time_local] "$request"' '$status $body_bytes_sent "$http_referer"' '"$http_user_agent"' '"$http_x_forwarded_
for"' '$server_name:$server_port';
access_log logs/access.log my_default_format; include /yinzhengjie/softwares/nginx/conf.d/*.conf;
} [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#

2>.编写子配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/share.conf
server {
listen 80;
server_name node101.yinzhengjie.org.cn; location / {
root /yinzhengjie/data/web/nginx/static;
index index.html;
} location /nginx_status {
stub_status;
allow 172.30.1.108;
deny all;
} location /main {
index index.html;
default_type text/html;
set $name jason;
set $nginx_name $server_name;
echo "姓名: $name";
echo "************";
echo "Nginx服务器名称: $nginx_name";
} }
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#

3>.重新加载nginx的配置文件

[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 9297 1 0 Dec17 ? 00:00:00 nginx: master process nginx
nginx 11823 9297 0 12:50 ? 00:00:00 nginx: worker process
nginx 11824 9297 0 12:50 ? 00:00:00 nginx: worker process
nginx 11825 9297 0 12:50 ? 00:00:00 nginx: worker process
nginx 11826 9297 0 12:50 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 9297 1 0 Dec17 ? 00:00:00 nginx: master process nginx
nginx 11890 9297 1 12:57 ? 00:00:00 nginx: worker process
nginx 11891 9297 1 12:57 ? 00:00:00 nginx: worker process
nginx 11892 9297 1 12:57 ? 00:00:00 nginx: worker process
nginx 11893 9297 1 12:57 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#

4>.浏览器访问"http://node101.yinzhengjie.org.cn/main"并查看日志格式,如下图所示。

二.自定义json格式日志

  Nginx的默认访问日志记录内容相对比较单一,默认的格式也不方便后期做日志统计分析,生产环境中通常将nginx日志转换为json日志,然后配合使用ELK做日志收集-统计-分析。

1>.编辑主配置文件

[root@node101.yinzhengjie.org.cn ~]# cat -n /yinzhengjie/softwares/nginx/conf/nginx.conf
1 worker_processes 4;
2 worker_cpu_affinity 00000001 00000010 00000100 00001000;
3
4 events {
5 worker_connections 100000;
6 use epoll;
7 accept_mutex on;
8 multi_accept on;
9 }
10
11 http {
12 include mime.types;
13
14 default_type text/html;
15
16 charset utf-8;
17
18 log_format my_access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"uri":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' '"http_user_agent":"$http_user_agent",' '"status":"$status"}';
   19
20
21 access_log logs/access_json.log my_access_json;
22
23 include /yinzhengjie/softwares/nginx/conf.d/*.conf;
24 }
25
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#

2>.编辑子配置文件

[root@node101.yinzhengjie.org.cn ~]# cat -n /yinzhengjie/softwares/nginx/conf.d/share.conf
1 server {
2 listen 80;
3 server_name node101.yinzhengjie.org.cn;
4
5 location / {
6 root /yinzhengjie/data/web/nginx/static;
7 index index.html;
8 }
9
10 location /nginx_status {
11 stub_status;
12 allow 172.30.1.108;
13 deny all;
14 }
15
16 location /main {
17 index index.html;
18 default_type text/html;
19 set $name jason;
20 set $nginx_name $server_name;
21 echo "姓名: $name";
22 echo "************";
23 echo "Nginx服务器名称: $nginx_name";
24 }
25
26 }
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

3>.重新加载nginx配置文件

[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 9297 1 0 Dec17 ? 00:00:00 nginx: master process nginx
nginx 11890 9297 0 12:57 ? 00:00:00 nginx: worker process
nginx 11891 9297 0 12:57 ? 00:00:00 nginx: worker process
nginx 11892 9297 0 12:57 ? 00:00:00 nginx: worker process
nginx 11893 9297 0 12:57 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 9297 1 0 Dec17 ? 00:00:00 nginx: master process nginx
nginx 11946 9297 1 13:25 ? 00:00:00 nginx: worker process
nginx 11947 9297 1 13:25 ? 00:00:00 nginx: worker process
nginx 11948 9297 0 13:25 ? 00:00:00 nginx: worker process
nginx 11949 9297 1 13:25 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#

4>.浏览器访问"http://node101.yinzhengjie.org.cn/main"并查看日志格式,如下图所示。

Nginx 高级配置-自定义json格式日志的更多相关文章

  1. ELK之收集Nginx、Tomcat的json格式日志

    1.安装Nginx yum -y install nginx vim /etc/nginx/nginx.conf # 修改日志格式为json格式,并创建一个nginxweb的网站目录 log_form ...

  2. filebeat收集nginx的json格式日志

    一.在nginx主机上安装filebeat组件 [root@zabbix_server nginx]# cd /usr/local/src/ [root@zabbix_server src]# wge ...

  3. Nginx 核心配置-自定义日志路径及清空日志注意事项

    Nginx 核心配置-自定义日志路径及清空日志注意事项 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.关于日志清空注意事项 1>.nginx服务写访问日志是基于acces ...

  4. Nginx 高级配置-变量使用

    Nginx 高级配置-变量使用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.  nginx的变量可以在配置文件中引用,作为功能判断或者日志等场景使用,变量可以分为内置变量和自定义变 ...

  5. Nginx 高级配置--关于favicon.ico

    Nginx 高级配置--关于favicon.ico 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.浏览器会默认帮咱们访问官网的图标 1>.浏览器访问网站"htt ...

  6. Nginx 高级配置-https 功能

    Nginx 高级配置-https 功能 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.HTTPS工作过程 1>.SSL/TLS SSL(Secure Socket Lay ...

  7. Nginx 高级配置-实现多域名HTTPS

    Nginx 高级配置-实现多域名HTTPS 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Nginx支持基于单个IP实现多域名的功能 Nginx支持基于单个IP实现多域名的功能 ...

  8. Nginx 高级配置-压缩功能

    Nginx 高级配置-压缩功能 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Nginx压缩相关参数概述 1>.gzip on | off; Nginx支持对指定类型的文 ...

  9. Nginx 高级配置-第三方模块编译

    Nginx 高级配置-第三方模块编译 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 第三模块是对nginx 的功能扩展,第三方模块需要在编译安装Nginx 的时候使用参数--add ...

随机推荐

  1. html图片和html实体

    img标签 <img src="../imgs/msn1.jpg" alt="这是一张图片" title="图片" width=&qu ...

  2. flask 搭建简单restful接口,moco基础

    from flask import Flask, jsonify, abort, make_response app = Flask(__name__)app.config['JSON_AS_ASCI ...

  3. MySQL日记

    MySQL日记 MySQL——day01:https://www.cnblogs.com/noonjuan/diary/2019/07/24/11241543.html MySQL——day02:ht ...

  4. 字符串s倒序输出

    编程将字符串s倒序输出,要求利用函数递归实现. 输入格式要求:"%s" 提示信息:"input your string:\n" 输出格式要求:"%c& ...

  5. Python爬虫练习

    例一:爬取信息关于'gbk' codec can't encode character '\xa0' in position 6: illegal 错误提示: #初始化class 得到对象 draw= ...

  6. Linux性能优化实战学习笔记:第二十四讲

    一.磁盘 1.机械磁盘 2.固态磁盘 3.相同磁盘随机I/O比连续I/O慢很多 4.最小单位 5.接口 6.RAID陈列卡 7.网路存储 二.通用块层 1.概念 2.第一功能 3.第二功能 4.I/O ...

  7. css设置不可复制

    -moz-user-select:none; /* Firefox私有属性 */ -webkit-user-select:none; /* WebKit内核私有属性 */ -ms-user-selec ...

  8. 基于sign的pose-graph残差函数构建

    沿sign的法线方向优化车辆位姿,优化目标函数也即残差函数构建过程如下:

  9. 微软 Azure DevOps Server 2019 Update 1 (TFS 2019.1)

    1.概述 微软在2019年5月发布Azure DevOps Server 2019后不到2个月的时间里,就快速准备好了第一个升级包(2019 Update 1),并计划在几周后发布正式版本.也许你还没 ...

  10. mysql操作(精简版)

    一.数据库操作(建库.删库) 1.查看数据库:show databases; 2.创建数据库:DROP DATABASE 数据库名; 3.删除数据库:CREATE DATABASE 数据库名; 4.使 ...