首页 > 其他 > 详细

Nginx常用技巧

时间:2019-08-28 22:40:15      阅读:84      评论:0      收藏:0      [点我收藏+]

隐藏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

Nginx常用技巧

原文:https://www.cnblogs.com/xumiao/p/11426716.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!