zoukankan      html  css  js  c++  java
  • Nginx命令行及演示:重载、热部署、日志切割

    Nginx命令行:

      01 格式:nginx 参数 信号

      02 帮助: -?  -h

      03 使用指定的配置文件:-c

      04 指定配置指令: -g

      05 指定运行目录: -p

      06 发送信号: -s

        立刻停止服务: stop

        优雅地停止服务: quit

        重载配置文件: reload

        重新开始记录日志文件: reopen

      07 测试配置文件是否语法错误: -t  -T

      08 打印nginx的版本信息、编译信息等: -v  -V

    重载配置文件:

    [root@Alen ~]# cd /opt/nginx
    [root@Alen nginx]# ls
    client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
    [root@Alen nginx]# ./sbin/nginx 
    [root@Alen nginx]# ps -ef | grep nginx
    root       2888      1  0 19:47 ?        00:00:00 nginx: master process ./sbin/nginx
    nobody     2889   2888  0 19:47 ?        00:00:00 nginx: worker process
    root       2891   2865  0 19:47 pts/0    00:00:00 grep --color=auto nginx
    [root@Alen nginx]# netstat -anpult | grep nginx
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2888/nginx: master 
    [root@Alen nginx]# vim conf/nginx.conf
    [root@Alen nginx]# netstat -anpult | grep 800
    [root@Alen nginx]# ./sbin/nginx -s reload
    [root@Alen nginx]# netstat -anpult | grep 800
    tcp        0      0 0.0.0.0:800             0.0.0.0:*               LISTEN      2888/nginx: master  

      启动Nginx之后,查看端口,发现它占用的是默认的80端口,这时,通过修改配置文件,将它的端口改为800。再次查看端口,由于没有重启服务,它的新配置是不生效的。这时,可以对它进行重载配置文件的操作,让它由旧的配置平滑地过度到新的配置。重载配置文件后,可以看到nginx占用的端口变成了800.

    热部署:

    # 备份原有的二进制文件
    [root@Alen nginx]# cd sbin/ && ls
    nginx
    [root@Alen sbin]# cp nginx nginx.old
    [root@Alen sbin]# ls
    nginx  nginx.old
    # 用源码包中的二进制文件覆盖现用的二进制文件
    [root@Alen sbin]# cd /opt/nginx-1.16.1/objs/
    [root@Alen objs]# ls
    autoconf.err  nginx    ngx_auto_config.h   ngx_modules.c  src
    Makefile      nginx.8  ngx_auto_headers.h  ngx_modules.o
    [root@Alen objs]# cp nginx /opt/nginx/sbin/
    cp:是否覆盖"/opt/nginx/sbin/nginx"? y
    # 注意观察主进程的进程号
    [root@Alen objs]# ps -ef | grep nginx
    root       2888      1  0 19:47 ?        00:00:00 nginx: master process ./sbin/nginx
    nobody     2929   2888  0 19:57 ?        00:00:00 nginx: worker process
    root       2988   2865  0 20:06 pts/0    00:00:00 grep --color=auto nginx
    # 向主进程发送信号进行版本升级
    [root@Alen objs]# kill -USR2 2888
    # 此时nginx会新起一个nginx主进程,新老进程同时工作,只是旧的主进程已经不再监听800端口了
    [root@Alen objs]# ps -ef | grep nginx
    root       2888      1  0 19:47 ?        00:00:00 nginx: master process ./sbin/nginx
    nobody     2929   2888  0 19:57 ?        00:00:00 nginx: worker process
    root       2989   2888  0 20:07 ?        00:00:00 nginx: master process ./sbin/nginx
    nobody     2990   2989  0 20:07 ?        00:00:00 nginx: worker process
    root       2992   2865  0 20:07 pts/0    00:00:00 grep --color=auto nginx
    # 给旧的nginx主进程发送信号,请它优雅地关闭所有worker进程
    [root@Alen objs]# kill -WINCH 2888
    # 此时,所有的请求都已经迁移到新升级的nginx中了,旧的worker进程已经退出,但旧的master进程依然存在。因为可能会出现一些问题,需要把新版本回退到老版本,此时还可以向旧的主进程发送reload命令,让它重新把worker进程拉起来。然后再把新的master进程关掉,所以旧的master进程是不会自动退出的,好允许做版本回退。
    [root@Alen objs]# ps -ef | grep nginx
    root       2888      1  0 19:47 ?        00:00:00 nginx: master process ./sbin/nginx
    root       2989   2888  0 20:07 ?        00:00:00 nginx: master process ./sbin/nginx
    nobody     2990   2989  0 20:07 ?        00:00:00 nginx: worker process
    root       2994   2865  0 20:08 pts/0    00:00:00 grep --color=auto nginx

    日志切割:

    # 手动备份原有日志
    [root@Alen objs]# cd /opt/nginx/logs/
    [root@Alen logs]# mv access.log access.log.bak
    [root@Alen logs]# ls
    access.log.bak  error.log  nginx.pid  nginx.pid.oldbin
    # 发送信号会重新生成新的日志文件
    [root@Alen logs]# ../sbin/nginx -s reopen
    [root@Alen logs]# ls
    access.log  access.log.bak  error.log  nginx.pid  nginx.pid.oldbin
    # 编写计划任务
    [root@Alen logs]# crontab -e
    # 查看计划任务
    [root@Alen logs]# crontab -l
    0 0 1 * * bash /root/rotate.sh

      此处计划任务中脚本分两步,一为将日志备份,以时间戳命名,可使用系统变量date;二为发送信号,生成新日志。日志切割的频率按照日志生成的速度自行定义。

  • 相关阅读:
    17.10.30
    17.10.28&29
    PDO和Mysqli的区别
    ajax--->请求异常 jQuery提示parsererror错误解决办法
    ajax--->http头信息的content-type是application/x-www-form-urlencoded或application/json区别
    php--->把json传来的stdClass Object类型转array
    mysql--->mysql查看数据库操作记录
    js解析json时报:Unexpected token s in JSON at position 0
    code review工具之codebrag安装使用
    linux-->yii2报yiidbException错
  • 原文地址:https://www.cnblogs.com/Axiao-47/p/12296630.html
Copyright © 2011-2022 走看看