Nginx部署

单节点部署

安装依赖包

1
yum -y install gcc pcre-devel zlib-devel openssl-devel git

下载最新稳定版本

1
wget http://nginx.org/download/nginx-1.14.2.tar.gz

http://nginx.org/en/download.html

下载nginx-module-vts

1
2
cd /tmp
git clone git://github.com/vozlt/nginx-module-vts.git

nginx的监控模块,能够输出json格式的监控数据

解压

1
tar zxvf nginx-1.14.2.tar.gz

编译安装

1
2
3
cd nginx-1.14.2
./configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=/tmp/nginx-module-vts
make && make install

做软链,方便后续执行

1
ln -s /opt/nginx/sbin/nginx /sbin/nginx

nginx+keepalive(高可用部署)

两个节点都执行:

安装依赖包

1
yum -y install gcc pcre-devel zlib-devel openssl-devel

下载最新稳定版本

1
wget http://nginx.org/download/nginx-1.14.2.tar.gz

解压

1
tar zxvf nginx-1.14.2.tar.gz

编译安装

1
2
3
cd nginx-1.14.2
./configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install

做软链,方便后续执行

1
ln -s /opt/nginx/sbin/nginx /sbin/nginx

nginx状态检测脚本

cat /opt/nginx/chk_nginx.sh

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
/sbin/nginx
sleep 2
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
systemctl stop keepalived
fi
fi

[root@n1 ~]# chmod 755 /opt/nginx/chk_nginx.sh

脚本检测nginx进程数,如果等于0,则启动nginx,2秒后再检测一次,如果nginx进程还是0,则停止keealived,此时备用节点会接管虚ip。

安装keepalived

1
yum install keepalived

配置keepalived

keepalived节点1配置

cat /etc/keepalived/keepalived.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
vrrp_script chk_nginx {
script "/opt/nginx/chk_nginx.sh"
interval 2
weight -5
fall 2
rise 1
}

vrrp_instance VI_1 {
state MASTER
interface em2
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass huayun888@
}
virtual_ipaddress {
1.1.1.1
}
track_script {
chk_nginx
}
}
keepalived节点2配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
cat /etc/keepalived/keepalived.conf

vrrp_script chk_nginx {
script "/opt/nginx/chk_nginx.sh"
interval 2
weight -5
fall 2
rise 1
}

vrrp_instance VI_1 {
state BACKUP
interface em2
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass huayun888@
}
virtual_ipaddress {
1.1.1.1
}
track_script {
chk_nginx
}
}
需要根据实际情况改动的配置

interface em2:指定HA监测网络的接口
1.1.1.1:虚ip

重启keepalived

1
systemctl restart keepalived

最后测试将节点1的keepalived停止或直接关掉节点1,看节点2是否会接管虚ip。

主配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#user  nobody;
worker_processes 20;
events {
worker_connections 10240;
}

http {
include mime.types;
default_type application/octet-stream;

log_format main_old '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'"$request_body"'
'$upstream_addr';

# log_format main escape=json '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for" "$request_time" "$upstream_response_time" "$request_body" "$upstream_addr"';

log_format main '{ "@timestamp": "$time_iso8601", '
'"remote_addr": "$remote_addr", '
'"remote_user": "$remote_user", '
'"body_bytes_sent": "$body_bytes_sent", '
'"request_time": "$request_time", '
'"status": "$status", '
'"request_uri": "$request_uri", '
'"request_method": "$request_method", '
'"http_referrer": "$http_referrer", '
'"body_bytes_sent": "$body_bytes_sent", '
'"upstream_addr": "$upstream_addr", '
'"upstream_response_time": "$upstream_response_time", '
'"http_user_agent": "$http_user_agent"}';

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;
server_tokens off;
server_names_hash_bucket_size 512;
client_max_body_size 510m;
client_header_buffer_size 128k;
large_client_header_buffers 4 128k;

#gzip on;
vhost_traffic_status_zone shared:vhost_traffic_status:128m;
vhost_traffic_status_filter_by_host on;
server {
listen 8008;
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
access_log off;
allow 127.0.0.1;
allow 218.6.71.54;
deny all;
}
}

server {
listen 80;
server_name _;
location / {
return 444;
}

}
include vhost/*.conf;
}

反向代理配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
upstream  kibana  {
server 127.0.0.1:5601;
}

server {
listen 80;
server_name elk.abc.cn;

location / {
auth_basic "Please Input user and password";
auth_basic_user_file /opt/nginx/conf/passwd.db; #openssl passwd -crypt password
proxy_pass http://kibana;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header Access-Control-Allow-Origin *;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
proxy_set_header Accept-Encoding 'gzip';
gzip on;
gzip_min_length 1000;
gzip_buffers 4 8k;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/json;
}
access_log logs/kibana.log main;
}

日志切割

使用logrotate

[root@n1 tools]# cat /etc/logrotate.d/nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/opt/nginx/logs/*.log
{
daily
rotate 60
missingok
dateext
compress
delaycompress
notifempty
sharedscripts
postrotate
[ -f /opt/nginx/logs/nginx.pid ] && kill -USR1 `cat /opt/nginx/logs/nginx.pid`
endscript
}

配置说明

  • daily:日志文件将按天轮循
  • weekly:日志文件将按周轮循
  • monthly:日志文件将按月轮循
  • missingok :#在日志轮循期间,任何错误将被忽略,例如“文件无法找到”之类的错误
  • notifempty: #如果是空文件的话,不进行转储
  • nocompress:不需要压缩时,用这个参数
  • copytruncate:用于还在打开中的日志文件,把当前日志备份并截断
  • nocopytruncate:备份日志文件但是不截断
  • create mode owner group:转储文件,使用指定的文件模式创建新的日志文件
  • nocreate:不建立新的日志文件
  • delaycompress 和 compress 一起使用时,转储的日志文件到下一次转储时才压缩
  • nodelaycompress 覆盖 delaycompress 选项,转储同时压缩。
  • size size 当日志文件到达指定的大小时才转储,Size 可以指定 bytes (缺省)以及KB (sizek)或者MB (sizem).
  • dateext:定义日志文件后缀是日期格式,也就是切割后文件是:xxx.log-20160402.gz这样的格式。如果该参数被注释掉,切割出来是按数字递增,即前面说的 xxx.log-1这种格式
  • delaycompress:总是与compress选项一起用,delaycompress选项指示logrotate不要将最近的归档压缩,压缩将在下一次轮循周期进行。这在你或任何软件仍然需要读取最新归档时很有用
  • create 640 nginx adm:以指定的权限和用户属性,创建全新的日志文件,同时logrotate也会重命名原始日志文件。
  • rotate count 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
配置参数 说明
compress 通过gzip压缩转储以后的日志
nocompress 不压缩
copytruncate 用于还在打开中的日志文件,把当前日志备份并截断
nocopytruncate 备份日志文件但是不截断
create mode owner group 转储文件,使用指定的文件模式创建新的日志文件
nocreate 不建立新的日志文件
delaycompress 和 compress 一起使用时,转储的日志文件到下一次转储时才压缩
nodelaycompress 覆盖 delaycompress 选项,转储同时压缩。
errors address 专储时的错误信息发送到指定的Email 地址
ifempty 即使是空文件也转储,这个是 logrotate 的缺省选项。
notifempty 如果是空文件的话,不转储
mail address 把转储的日志文件发送到指定的E-mail 地址
nomail 转储时不发送日志文件
olddir directory 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
noolddir 转储后的日志文件和当前日志文件放在同一个目录下
prerotate/endscript 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行
daily 指定转储周期为每天
weekly 指定转储周期为每周
monthly 指定转储周期为每月
rotate count 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
tabooext [+] list 让logrotate不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave, v, 和 ~
size size 当日志文件到达指定的大小时才转储,bytes(缺省)及KB(sizek)或MB(sizem)
missingok 在日志轮循期间,任何错误将被忽略,例如“文件无法找到”之类的错误。

设置定时

1
59 23 * * * root /usr/sbin/logrotate -f /etc/logrotate.d/nginx

使用脚本

cat /opt/cutlog.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
LOGS_PATH=/opt/nginx/logs
LOGLIST=$LOGS_PATH/loglist
>$LOGLIST
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
for i in `ls $LOGS_PATH |grep "log$"`;
do
mv ${LOGS_PATH}/${i} ${LOGS_PATH}/${i}_${YESTERDAY}
echo ${LOGS_PATH}/${i}_${YESTERDAY} >>$LOGLIST
done

kill -USR1 $(cat ${LOGS_PATH}/nginx.pid)

while read log;
do
gzip $log
done<$LOGLIST