OpenResty 部署配置和日志切割

用户头像
wong
关注
发布于: 2020 年 05 月 26 日

操作系统用的是 CentOS,OpenResty 从以前的编译安装改为了现在的 yum 安装,官方的包编译的很好,直接用。



安装独立仓库

#centos
yum install -y yum-utils
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo



安装OpenResty

yum install -y openresty



OpenResty配置

#默认部署路径 /usr/local/openresty
#配置文件路径 /usr/local/openresty/nginx/conf
#nginx.conf内容
user nginx;
worker_processes auto;
error_log logs/error.log;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" "$request_time" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$upstream_response_time"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
server_tokens off;
gzip on;
gzip_proxied any;
gzip_comp_level 4;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_vary on;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_redirect off;
proxy_connect_timeout 300;
proxy_read_timeout 300;
proxy_send_timeout 300;
proxy_buffer_size 16k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 128k;
keepalive_timeout 65;
include vhost/*.conf;
#include deny.conf;
}
# vhost文件夹中的proxy.conf,实现传递客户端真实ip到远端服务
#proxy.conf内容
server {
listen 8080;
access_log logs/access_8080.log main;
location / {
try_files $uri @proxy;
}
location @proxy {
internal;
proxy_set_header Host $http_host;
proxy_set_header X-Forward-For $remote_addr;
proxy_pass http://ip:port;
}
}



启动停止

# 检查配置
/etc/init.d/openresty configtest
# 启动
/etc/init.d/openresty start
# 停止
/etc/init.d/openresty stop



日志切割脚本



#!/bin/bash
source /etc/profile
logs_path=/usr/local/openresty/nginx/logs
pid_file=${logs_path}/nginx.pid
year=$(date +"%Y")
month=$(date +"%m")
day=$(date +"%d")
#echo $year
#echo $month
#echo $day
nowdir=${logs_path}/${year}/${month}
mkdir -p $nowdir
cd $logs_path
for i in `ls *.log`
do
#echo $i
#echo "---"
#echo $year/$month/${year}${month}${day}_${i}
mv $i $year/$month/${year}${month}${day}_${i}
#echo "---"
done
#echo `cat ${pid_file}`
kill -USR1 `cat ${pid_file}`
cd $nowdir
pwd
if [ $? -eq 0 ];then
for j in `ls *.log`
do
tar zcvf $j.tar.gz $j && rm -f $j
done
fi



新增计划任务(每天切割一次)

crontab -l
0 0 * * * /usr/local/bin/cut_nginx_log



备注

可能需要创建一个nginx系统账号,把nginx账号权限赋给openresty文件夹,再启动。

发布于: 2020 年 05 月 26 日 阅读数: 31
用户头像

wong

关注

人生充满黑色幽默 2017.10.24 加入

一名工程师

评论

发布
暂无评论
OpenResty 部署配置和日志切割