日志收集
配置nginx日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" $http_host '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$request_body" '
'$upstream_addr' '"$upstream_status" "$upstream_cache_status" ' '"$upstream_http_content_type" $upstream_response_time > $request_time';
配置日志存放目录
access_log /data/nginx/logs/access.log main;
反代参数优化
proxy_headers_hash_bucket_size 1024;
proxy_headers_hash_max_size 512;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
#设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffer_size 16k;
#proxy_buffers缓冲区,网页平均在32k以下的设置
proxy_buffers 4 64k;
#高负荷下缓冲大小(proxy_buffers*2)
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
#配置反代缓存临时目录
proxy_temp_path /data/temp_dir;
#配置反代缓存目录
proxy_cache_path /data/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
压缩配置
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 9;
#指定压缩类型
gzip_types text/plain application/x-javascript text/css application/xml;
#加vary头
gzip_vary on;
指定referer
map $http_referer $ref {
default $http_referer;
~(https:\/\/aaa.com)(.*) https://bbb.com$2;
~https://aaa.com https://bbb.com;
}
通过map映射实现修改referer的目的
日志分割
#!/bin/bash
base_path='/data/nginx/logs' # 保存日志的目录
log_path=$(date -d yesterday +"%Y%m")
day=$(date -d yesterday +"%Y-%m-%d") # 日期作为日志的标识
mkdir -p $base_path/$log_path # 根据年月管理日志
mv $base_path/access.log $base_path/$log_path/access_$day.log
mv $base_path/error.log $base_path/$log_path/error_$day.log
echo $base_path/$log_path/access_$day.log
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid` # 信号通知nginx重新开始记录日志