前言
- 最近惰性太强,逼着自己做乱七八糟配置。。。我还是比较喜欢愉快的写代码。
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;
}
}
}
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/