【nginx】nginx打点服务配置笔记

前言

  • 最近惰性太强,逼着自己做乱七八糟配置。。。我还是比较喜欢愉快的写代码。

nginx

  • 首先安装nginx
yum install -y nginx
  • 主要配置3个地方:
    利用nginx自带的empty_gif模块,配置一个空gif服务
    配置日志格式log_format
    配置日志存放路径access_log
    打开nginx配置文件:
vi /etc/nginx/nginx.conf
  • 复制粘贴:
# /etc/nginx/nginx.conf
user root;
worker_processes  1;

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

events {
    worker_connections  1024;
}

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

    log_format ferms '$time_iso8601     -       -       $remote_addr    $http_host      $status $request_time   $request_length $body_bytes_sent        15d04347-be16-b9ab-0029-24e4b6645950   -       -       9689c3ea-5155-2df7-a719-e90d2dedeb2c 937ba755-116a-18e6-0735-312cba23b00c       -       -       $request_uri    -       $http_user_agent        -       sample=-&_UC_agent=-&device_id=-&-      -       -       -';

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

    sendfile           on;
    keepalive_timeout  65;
    include /etc/nginx/conf.d/*.conf;

    server {
        listen 8888;
        server_name dig-server;

        if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})") {
            set $year $1;
            set $month $2;
            set $day $3;
            set $hour $4;
            set $minute $5;
        }
        access_log /var/log/nginx/ferms/$year$month-$day-$hour-$minute.log ferms;

        index index.htm index.html;
        root /etc/nginx/html;

        location = /dig.gif {
            empty_gif;
        }
    }
}
  • 创建html
mkdir -p /etc/nginx/html
vi /etc/nginx/html/index.html
<body>nginx</body>
  • 日志存放位置
mkdir -p /var/log/nginx/ferms
  • 建定时任务删除多余日志:
mkdir -p /root/shell
cd /root/shell
vi delete_nginx_logs.sh
#!/bin/bash
#Filename: delete_nginx_logs.sh
LOGS_PATH=/var/log/nginx
KEEP_DAYS=30
PID_FILE=/run/nginx.pid
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
if [ -f $PID_FILE ];then
    echo `date "+%Y-%m-%d %H:%M:%S"` Deleting logs...
    mv ${LOGS_PATH}/access.log ${LOGS_PATH}/access.${YESTERDAY}.log >/dev/null 2>&1
    mv ${LOGS_PATH}/access.json ${LOGS_PATH}/access.${YESTERDAY}.json >/dev/null 2>&1
    mv ${LOGS_PATH}/error.log ${LOGS_PATH}/error.${YESTERDAY}.log >/dev/null 2>&1
    kill -USR1 `cat $PID_FILE`
    echo `date "+%Y-%m-%d %H:%M:%S"` Logs have deleted.
else
    echo `date "+%Y-%m-%d %H:%M:%S"` Please make sure that nginx is running...
fi
echo

find $LOGS_PATH -type f -mtime +$KEEP_DAYS -print0 |xargs -0 rm -f
crontab -e
# 每天00点00分删除日志文件
00 00 * * * /bin/bash /root/shell/cut_nginx_logs.sh >>/root/shell/cut_nginx_logs.log 2>&1
  • 关闭安全模式
setenforce 0
  • 启动服务
systemctl start nginx.service
systemctl enable nginx.service
  • 验证:
    访问8888端口 和 8888/dig.gif?a=1 是否正常。
  • 查看nginx服务是否启动:
ps -e  | grep nginx
  • 查看是否生成日志:
cd /var/log/nginx/ferms/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

业火之理

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值