nginx教程二,nginx重载、版本更换、日志切割

一、nginx基本命令

1. 启动nginx,如果不使用-c参数指定配置文件路径,则默认使用conf目录下的nginx.conf文件

./nginx -c conf/nginx.conf

2. 可以使用-t参数校验配置文件是否正确

nginx -t -c /root/nginx/conf/nginx.conf

3. 如果修改了配置文件,使用-s reload命令重启nginx

nginx -c conf/nginx.conf -s reload

二、热部署,升级nginx版本

1. 查看当前nginx版本

[root@localhost nginx]# sbin/nginx -v
nginx version: nginx/1.14.2

2. 查看当前nginx进程

[root@localhost nginx]# ps -ef | grep nginx
root     18103     1  0 18:35 ?        00:00:00 nginx: master process sbin/nginx
root     20794 18103  0 20:39 ?        00:00:00 nginx: worker process
root     20801  3475  0 20:56 pts/1    00:00:00 grep --color=auto nginx

3. 通过教程一中步骤,先下载1.16.1版本nginx,执行configure操作,注意prefix指向的目录要一样,然后执行make命令,但不要make install,将objs下的nginx二进制文件移到当前运行中的nginx目录下

#在当前nginx目录下备份nginx二进制文件
mv nginx nginx.old
#到新nginx版本make后的objs中,复制nginx二进制文件到当前运行nginx目录的sbin下
cp objs/nginx /root/nginx/sbin

4. 通过kill向nginx的master进程发出版本更新指令

kill -USR2 18103

5. 查看目前运行的nginx进程,会有新旧两个,同时其对应的work进程也在运行中

[root@localhost sbin]# ps -ef | grep nginx
root     18103     1  0 18:35 ?        00:00:00 nginx: master process sbin/nginx
root     20794 18103  0 20:39 ?        00:00:00 nginx: worker process
root     23351 18103  0 22:00 ?        00:00:00 nginx: master process sbin/nginx
root     23352 23351  0 22:00 ?        00:00:00 nginx: worker process
root     23354  1112  0 22:00 pts/0    00:00:00 grep --color=auto nginx

6. 优雅关闭旧版本nginx work进程

kill -WINCH 18103

7. 查看目前运行的nginx进程,旧版本的nginx master进程依然运行中,这是为了版本回退用的

[root@localhost sbin]# ps -ef | grep nginx
root     18103     1  0 18:35 ?        00:00:00 nginx: master process sbin/nginx
root     23351 18103  0 22:00 ?        00:00:00 nginx: master process sbin/nginx
root     23352 23351  0 22:00 ?        00:00:00 nginx: worker process
root     23370  1112  0 22:03 pts/0    00:00:00 grep --color=auto nginx

8. 查看最新nginx版本

[root@localhost sbin]# ./nginx -v
nginx version: nginx/1.16.1

 三、日志切割

1. 先备份相关日志文件,如access.log文件

mv access.log access.log.bak

2. 执行reopen命令进行日志切割,然后access.log文件就会自动生成

 ./nginx -s reopen

3. 生产环境中可以通过crontab定时工具来实现日志文件切割,下面写一个简单的定时任务例子,注意cron中的最小单位为分钟

  • 编写一个脚本文件cronBash.sh,执行的任务是每分钟往文件cronDate.txt中写入当前时间,
    #!/bin/bash
    echo 'hello world'
    echo `date '+%F %T'` >> /root/cronDate.txt
  • 设置脚本为可执行文件
    chmod +x cronBash.sh
  • crontab -e命令编辑定时任务文件,保存后就可以执行了
    */1 * * * * /root/cronBash.sh
  • crontab -l可以查看定时任务列表
posted @ 2021-02-14 22:09  jiapeng  阅读(478)  评论(0编辑  收藏  举报