Nginx常用技巧

隐藏nginx服务信息头

  • 隐藏nginx版本信息(nginx.conf)
http {
  server_tokens off;
}
  • 隐藏nginx标识

nginx源码目录:/nginx-1.15.1/src/http/ngx_http_header_filter_module.c

修改 48、49行代码:

static u_char ngx_http_server_string[] = "Server: XXX" CRLF;
static u_char ngx_http_server_full_string[] = "Server: XXX" CRLF;

重新编译nginx,关于编译参数可以使用nginx -V查看

$ ./configure  --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_geoip_module --with-http_stub_status_module --with-http_sub_module --with-stream --with-stream=dynamic

$ make 

make之后在objs目录下就多了个nginx,这个就是新编译后的版本程序了,接着我们备份原有nginx程序

$ mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

拷贝到编译好的objs下的nginx到sbin目录下

$ cp objs/nginx /usr/local/nginx/sbin/nginx

停止并启动nginx

$ pkill -9 nginx
$ /usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf

按天数切割access.log

cut_nginx_log.sh

#!/bin/bash
##零点执行该脚本

##nginx日志文件所在的目录
LOGS_PATH=/data/log/nginx

##获取昨天的yyyy-MM-dd
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)

##移动文件
mv ${LOGS_PATH}/access.log ${LOGS_PATH}/access_${YESTERDAY}.log

##向nginx主进程发送USR1信号,USR1信号是重新打开日志文件
kill -USR1 `ps axu | grep "nginx: master process" | grep -v grep | awk '{print $2}'`

##删除7天前的日志
cd ${LOGS_PATH}
find . -mtime +7 -name "access_*" | xargs rm -f

exit 0

  • 执行 crontab -e

  • 添加定时脚本,每天凌晨0点执行任务

 0 0 * * * sh /var/log/nginx/cut_nginx_log.sh
posted @ 2019-08-28 21:45  程序员yj  阅读(210)  评论(0)    收藏  举报