nginx安装及输出json格式日志

该博客介绍了如何在Linux环境中安装Nginx,包括安装依赖如gcc、pcre、zlib和openssl,然后编译并安装Nginx。接着,配置Nginx的日志格式为JSON,以便于数据分析。最后,展示了如何启动Nginx服务以及检查其运行状态,并通过访问服务器来生成访问日志。
日志产生演示
准备nginx环境
安装gcc编译器
# yum -y install gcc
安装pcre库:nginx的http模块使用pcre来解析正则表达式
# yum install -y pcre pcre-devel
nginx使用zlib对http包的内容进行gzip
# yum install -y zlib zlib-devel
安装openssl
# yum install -y openssl openssl-devel
安装nginx

编译安装

$ wget http://nginx.org/download/nginx-1.9.9.tar.gz 
$ tar -zxvf nginx-1.9.9.tar.gz /var/soft
在nginx解压目录下编译安装
# ./configure
# make
# make install

配置nginx

切到安装目录
# cd /usr/local/nginx
# vi conf/nginx.conf

启动

启动
# ./sbin/nginx
是否启动
# ps -ef | grep nginx
显示如下
root      23030      1  0 01:37 ?        00:00:00 nginx: master process ./sbin/nginx
nobody    23031  23030  0 01:37 ?        00:00:00 nginx: worker process
root      23035  20666  0 01:38 pts/1    00:00:00 grep --color=auto nginx

产生访问日志

# tail -f /usr/local/nginx/logs/access.log
浏览器访问服务器
sing-node
配置nginx

日志输出为json格式

    log_format log_json '{"access_time": "$time_local", '
                      '"remote_addr": "$remote_addr", '
                      '"host": "$host", '
                      '"scheme": "$scheme", '
                      '"request_uri": "$request_uri", '
                      '"request_method": "$request_method", '
                      '"http_cookie": "$http_cookie", '
                      '"args": "$args", '
                      '"status_code": $status, '
                      '"body_bytes_sent": $body_bytes_sent, '
                      '"agent": "$http_user_agent", '
                      '"x_forwarded":$http_x_forwarded_for", '
                      '"request_time": "$request_time", '
                      '"http_referer": "$http_referer", '
                      '"remote_user": "$remote_user" '
                  ' }';

    access_log  logs/access.log log_json;

重启

# /usr/local/nginx/sbin/nginx -s reload
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值