Nginx笔记

nginx的信号控制

参数 说明
TERM, INT Quick shutdown
QUIT Graceful shutdown (即等待请求结束后再关闭)
HUP 配置文件改变后,会开新的进程去读取新的配置文件,然后再优雅的关闭旧进程
USR1 Reopen the log file 重读日志,在日志按月/日分割时有用
UER2 Upgrade Executable on the fly 平滑的升级
WINCH 优雅的关闭旧的进程(配合USR2来进行升级)

HUP 使用方法:

ps aux | grep nginx

nginx 进程信息:

root     29449  0.0  0.0  20004   648 ?        Ss   14:31   0:00 nginx: master process ./nginx
nobody   29450  0.0  0.0  20448  1244 ?        S    14:31   0:00 nginx: worker process
root     30211  0.0  0.0 103264   848 pts/0    S+   20:41   0:00 grep nginx

使用命令重读配置文件:(不会关闭nginx)

kill -HUP 29449

使用USR1切割日志

[root@localhost logs]# cd /usr/local/nginx/logs
[root@localhost logs]# ls
access.log  error.log  nginx.pid

access.log就是nginx的默认日志文件,切割日志的时候,需要先对access.log文件重命名,然后创建一个access.log文件,使用USR1命令进行重读日志文件。

ps: 当修改access.log的文件名后,(例如,我们修改为在文件名后加上日志切割日期:access.log.201804231900),创建access.log文件,如果不使用USR1命令进行更新,nginx依然会向access.log.201804231900文件中写入日志,而不会向access.log文件中写入。
因为在linux系统中,是将文件挂载在文件节点inode上面的,修改文件名,并没有修改其指向的inode节点。

我们先来看看nginx的日志文件:

[root@localhost nginx]# ll logs/                          
总用量 12                                                    
-rw-r--r--. 1 root root 1966 4月  23 19:07 access.log      
-rw-r--r--. 1 root root 1939 4月  23 19:07 error.log       
-rw-r--r--. 1 root root    6 4月  22 14:31 nginx.pid          

打开浏览器,请求nginx服务器里面的资源,


打开浏览器,请求nginx服务器里面的资源

然后再查看日志文件:

[root@localhost nginx]# ll logs/                          
总用量 12                                                    
-rw-r--r--. 1 root root 3446 4月  23 19:07 access.log      
-rw-r--r--. 1 root root 1939 4月  23 19:07 error.log       
-rw-r--r--. 1 root root    6 4月  22 14:31 nginx.pid    

如图,红框标记的,日志文件大小增加了:

日志文件大小增加

现在,我们修改nginx的配置文件access.logaccess.log.201804231900,然后查看日志文件。

[root@localhost nginx]# mv ./logs/access.log ./logs/access.log.201804231900
[root@localhost nginx]# ll logs/
总用量 12
-rw-r--r--. 1 root root 3446 4月  23 19:07 access.log.201804231900
-rw-r--r--. 1 root root 1939 4月  23 19:07 error.log
-rw-r--r--. 1 root root    6 4月  22 14:31 nginx.pid

刷新浏览器,再次查看日志文件:

[root@localhost nginx]# ll logs/
总用量 16
-rw-r--r--. 1 root root 5111 4月  23 19:18 access.log.201804231900
-rw-r--r--. 1 root root 1939 4月  23 19:07 error.log
-rw-r--r--. 1 root root    6 4月  22 14:31 nginx.pid

修改日志文件名后查看日志

创建access.log文件:

[root@localhost nginx]# touch ./logs/access.log
[root@localhost nginx]# ls
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
创建access.lo`文件

查看日志文件:

[root@localhost nginx]# ll ./logs/
总用量 16
-rw-r--r--. 1 root root    0 4月  23 19:20 access.log
-rw-r--r--. 1 root root 5111 4月  23 19:18 access.log.201804231900
-rw-r--r--. 1 root root 1939 4月  23 19:07 error.log
-rw-r--r--. 1 root root    6 4月  22 14:31 nginx.pid

刷新浏览器,再次查看。我们会发现,增长的依然是access.log.201804231900,日志文件并没写入access.log

[root@localhost nginx]# ll ./logs/
总用量 16
-rw-r--r--. 1 root root    0 4月  23 19:20 access.log
-rw-r--r--. 1 root root 5111 4月  23 19:18 access.log.201804231900
-rw-r--r--. 1 root root 1939 4月  23 19:07 error.log
-rw-r--r--. 1 root root    6 4月  22 14:31 nginx.pid
[root@localhost nginx]# ll ./logs/
总用量 16
-rw-r--r--. 1 root root    0 4月  23 19:20 access.log
-rw-r--r--. 1 root root 6591 4月  23 19:29 access.log.201804231900
-rw-r--r--. 1 root root 1939 4月  23 19:07 error.log
-rw-r--r--. 1 root root    6 4月  22 14:31 nginx.pid

如图所示,日志文件并没写入access.log

日志查看

切换日志
找到文件进程ID,然后使用信号量,进行更新。

[root@localhost nginx]# ps aux| grep nginx
root       884  0.0  0.0 103260   840 pts/0    S+   19:36   0:00 grep nginx
root     29449  0.0  0.0  20004   648 ?        Ss   Apr22   0:00 nginx: master process ./nginx
nobody   29450  0.0  0.0  20448  1544 ?        S    Apr22   0:00 nginx: worker process
[root@localhost nginx]# kill -USR1 29449

[root@localhost nginx]# kill -USR1 29449就是命令的使用方法,如果不想查找进程id,可以使用命令kill -USR1 './logs/nginx.pid',效果一样。

查看日志:

[root@localhost nginx]# kill -USR1 29449
[root@localhost nginx]# ll ./logs/
总用量 16
-rw-r--r--. 1 nobody root    0 4月  23 19:20 access.log
-rw-r--r--. 1 root   root 6591 4月  23 19:29 access.log.201804231900
-rw-r--r--. 1 nobody root 1939 4月  23 19:07 error.log
-rw-r--r--. 1 root   root    6 4月  22 14:31 nginx.pid

刷新浏览器,再次查看:

[root@localhost nginx]# ll ./logs/
总用量 20
-rw-r--r--. 1 nobody root  740 4月  23 19:38 access.log
-rw-r--r--. 1 root   root 6591 4月  23 19:29 access.log.201804231900
-rw-r--r--. 1 nobody root 1939 4月  23 19:07 error.log
-rw-r--r--. 1 root   root    6 4月  22 14:31 nginx.pid

日志重读成功

我们可以看出,access.log的文件大小在增加,而access.log.201804231900的文件大小并没有变化。说明日志切割成功。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 使用理由 虚拟主机 虚拟主机使用的是特殊的软硬件技术,它把一台运行在因特网上的服务器主机分成一台台“虚拟”的主机,...
    hopevow阅读 5,829评论 0 10
  • 基本上作为第三方开发者最可能开发的就是三种类型的模块,即handler,filter和load-balancer。...
    三生纸书阅读 2,350评论 0 0
  • 指令上下文 nginx.conf中的配置信息,根据其逻辑上的意义,对它们进行了分类,也就是分成了多个作用域,或者称...
    三生纸书阅读 2,256评论 0 0
  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 14,401评论 2 33
  • 主流服务器比较 Tomcat:开源、运行servlet和jsp web应用软件基于java的web应用软件容器。对...
    CountryMars阅读 3,173评论 0 0

友情链接更多精彩内容