学习Nginx(三):命令与信号

命令及选项

1. 显示帮助信息:

[root@RockyLinux9 ~]# nginx -h
nginx version: nginx/1.26.0
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
             [-e filename] [-c filename] [-g directives]

选项:
  -?,-h         : 显示帮助信息
  -v            : 显示版本信息
  -V            : 显示版本信息和编译配置项
  -t            : 检查测试配置文件
  -T            : 检查测试配置文件并输出内容
  -q            : 在配置文件测试期间禁止显示非错误消息
  -s signal     : 向主进程发送信号: stop, quit, reopen, reload
  -p prefix     : 设置配置路径,默认: /usr/local/nginx/
  -e filename   : 设置错误日志文件,默认: logs/error.log
  -c filename   : 设置配置文件,默认: conf/nginx.conf
  -g directives : 启动时指定全局配置项,而不修改配置文件,格式:k v;

2. 显示版本信息

[root@RockyLinux9 ~]# nginx -v
nginx version: nginx/1.26.0

3. 显示版本信息和编译配置项

[root@RockyLinux9 ~]# nginx -V
nginx version: nginx/1.26.0
built by gcc 11.4.1 20230605 (Red Hat 11.4.1-2) (GCC)
built with OpenSSL 3.0.7 1 Nov 2022
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module

4. 检查测试配置文件

[root@RockyLinux9 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

5. 检查测试配置文件并输出内容

[root@RockyLinux9 ~]# nginx -T
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# configuration file /usr/local/nginx/conf/nginx.conf:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;
pid     run/nginx.pid;

events {
    worker_connections  1024;
}

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


    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

# configuration file /usr/local/nginx/conf/mime.types:

types {
    text/html                                        html htm shtml;
    text/css                                         css;
    text/xml                                         xml;
    image/gif                                        gif;
    image/jpeg                                       jpeg jpg;
    application/javascript                           js;
    application/atom+xml                             atom;
    application/rss+xml                              rss;

    text/mathml                                      mml;
    text/plain                                       txt;
    text/vnd.sun.j2me.app-descriptor                 jad;
    text/vnd.wap.wml                                 wml;
    text/x-component                                 htc;

    image/avif                                       avif;
    image/png                                        png;
    image/svg+xml                                    svg svgz;
    image/tiff                                       tif tiff;
    image/vnd.wap.wbmp                               wbmp;
    image/webp                                       webp;
    image/x-icon                                     ico;
    image/x-jng                                      jng;
    image/x-ms-bmp                                   bmp;

    font/woff                                        woff;
    font/woff2                                       woff2;

    application/java-archive                         jar war ear;
    application/json                                 json;
    application/mac-binhex40                         hqx;
    application/msword                               doc;
    application/pdf                                  pdf;
    application/postscript                           ps eps ai;
    application/rtf                                  rtf;
    application/vnd.apple.mpegurl                    m3u8;
    application/vnd.google-earth.kml+xml             kml;
    application/vnd.google-earth.kmz                 kmz;
    application/vnd.ms-excel                         xls;
    application/vnd.ms-fontobject                    eot;
    application/vnd.ms-powerpoint                    ppt;
    application/vnd.oasis.opendocument.graphics      odg;
    application/vnd.oasis.opendocument.presentation  odp;
    application/vnd.oasis.opendocument.spreadsheet   ods;
    application/vnd.oasis.opendocument.text          odt;
    application/vnd.openxmlformats-officedocument.presentationml.presentation
                                                     pptx;
    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
                                                     xlsx;
    application/vnd.openxmlformats-officedocument.wordprocessingml.document
                                                     docx;
    application/vnd.wap.wmlc                         wmlc;
    application/wasm                                 wasm;
    application/x-7z-compressed                      7z;
    application/x-cocoa                              cco;
    application/x-java-archive-diff                  jardiff;
    application/x-java-jnlp-file                     jnlp;
    application/x-makeself                           run;
    application/x-perl                               pl pm;
    application/x-pilot                              prc pdb;
    application/x-rar-compressed                     rar;
    application/x-redhat-package-manager             rpm;
    application/x-sea                                sea;
    application/x-shockwave-flash                    swf;
    application/x-stuffit                            sit;
    application/x-tcl                                tcl tk;
    application/x-x509-ca-cert                       der pem crt;
    application/x-xpinstall                          xpi;
    application/xhtml+xml                            xhtml;
    application/xspf+xml                             xspf;
    application/zip                                  zip;

    application/octet-stream                         bin exe dll;
    application/octet-stream                         deb;
    application/octet-stream                         dmg;
    application/octet-stream                         iso img;
    application/octet-stream                         msi msp msm;

    audio/midi                                       mid midi kar;
    audio/mpeg                                       mp3;
    audio/ogg                                        ogg;
    audio/x-m4a                                      m4a;
    audio/x-realaudio                                ra;

    video/3gpp                                       3gpp 3gp;
    video/mp2t                                       ts;
    video/mp4                                        mp4;
    video/mpeg                                       mpeg mpg;
    video/quicktime                                  mov;
    video/webm                                       webm;
    video/x-flv                                      flv;
    video/x-m4v                                      m4v;
    video/x-mng                                      mng;
    video/x-ms-asf                                   asx asf;
    video/x-ms-wmv                                   wmv;
    video/x-msvideo                                  avi;
}

6. 在配置文件测试期间禁止显示非错误消息

[root@RockyLinux9 ~]# nginx -q
[root@RockyLinux9 ~]# echo $?
0

7. 启动时指定全局配置项(需为配置文件中未设置项),格式:k v;

# 原配置显示进程
[root@RockyLinux9 ~]# ps -ef|grep nginx
root        4680       1  0 21:49 ?        00:00:00 nginx: master process nginx
nginx       4681    4680  0 21:49 ?        00:00:00 nginx: worker process

# 停止服务
[root@RockyLinux9 ~]# nginx -s stop

# 注释如下两行
[root@RockyLinux9 ~]# vim /usr/local/nginx/conf/nginx.conf
#worker_processes  1;   
#pid    run/nginx.pid;

# 新pid文件不存在
[root@RockyLinux9 ~]# ll /var/run/nginx.pid
ls: cannot access '/var/run/nginx.pid': No such file or directory

# 执行pid文件,指定进程数
[root@RockyLinux9 ~]# nginx -g "pid /var/run/nginx.pid; worker_processes 2;"
[root@RockyLinux9 ~]# ps -ef|grep nginx
root        4698       1  0 21:58 ?        00:00:00 nginx: master process nginx -g pid /var/run/nginx.pid; worker_processes 2;
nginx       4699    4698  0 21:58 ?        00:00:00 nginx: worker process
nginx       4700    4698  0 21:58 ?        00:00:00 nginx: worker process
[root@RockyLinux9 ~]# ll /var/run/nginx.pid
-rw-r--r--. 1 root root 5 May 10 21:58 /var/run/nginx.pid

信号

        向主进程发送信号

1. 快速关闭

    • nginx -s stop
    • kill -INT/TERM 主进程PID
#启动程序
[root@RockyLinux9 ~]# nginx

#查看进程
[root@RockyLinux9 ~]# ps -ef|grep nginx
root        4733       1  0 22:12 ?        00:00:00 nginx: master process nginx
nginx       4734    4733  0 22:12 ?        00:00:00 nginx: worker process
nginx       4735    4733  0 22:12 ?        00:00:00 nginx: worker process
root        4737    4591  0 22:12 pts/0    00:00:00 grep --color=auto nginx

# 快速关闭程序,无论进程是否有连接,都会直接断开
[root@RockyLinux9 ~]# nginx -s stop

# 确认
[root@RockyLinux9 ~]# ps -ef|grep nginx
root        4740    4591  0 22:12 pts/0    00:00:00 grep --color=auto nginx

[root@RockyLinux9 ~]# nginx
[root@RockyLinux9 ~]# ps -ef|grep nginx
root        1517       1  0 23:47 ?        00:00:00 nginx: master process nginx
nginx       1518    1517  0 23:47 ?        00:00:00 nginx: worker process
nginx       1519    1517  0 23:47 ?        00:00:00 nginx: worker process
root        1530    1408  0 23:52 pts/0    00:00:00 grep --color=auto nginx
[root@RockyLinux9 ~]# kill -INT 1517
[root@RockyLinux9 ~]# ps -ef|grep nginx
root        1535    1408  0 23:52 pts/0    00:00:00 grep --color=auto nginx


[root@RockyLinux9 ~]# nginx
[root@RockyLinux9 ~]# ps -ef|grep nginx
root        1537       1  0 May10 ?        00:00:00 nginx: master process nginx
nginx       1538    1537  0 May10 ?        00:00:00 nginx: worker process
nginx       1539    1537  0 May10 ?        00:00:00 nginx: worker process
root        1568    1408  0 00:05 pts/0    00:00:00 grep --color=auto nginx
[root@RockyLinux9 ~]# kill -TERM 1537
[root@RockyLinux9 ~]# ps -ef|grep nginx
root        1570    1408  0 00:05 pts/0    00:00:00 grep --color=auto nginx

2. 优雅的关闭

  • nginx -s quit
  • kill -QUIT 主进程PID
# 创建一个1G的测试文件
[root@RockyLinux9 ~]# dd if=/dev/zero of=/usr/local/nginx/html/Linuxjsz bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 3.11659 s, 345 MB/s

# 通过客户端访问的方式进行文件下载
[root@RockyLinux9 ~]# wget http://192.168.199.152/Linuxjsz
Connecting to 192.168.199.152:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1073741824 (1.0G) [application/octet-stream]
Saving to: ‘Linuxjsz’
Linuxjsz            100%[=====================>]   1.00G  147MB/s  in 7.4s

22:41:54 (139 MB/s) - ‘Linuxjsz’ saved [1073741824/1073741824]
# 查看进程情况,正常运行中
[root@RockyLinux9 ~]# ps -ef|grep nginx
root        4824       1  0 22:41 ?        00:00:00 nginx: master process nginx
nginx       4825    4824  0 22:41 ?        00:00:00 nginx: worker process
nginx       4826    4824  0 22:41 ?        00:00:00 nginx: worker process
root        4828    4591  0 22:41 pts/0    00:00:00 grep --color=auto nginx

# 下载过程中发送quit信号
[root@RockyLinux9 ~]# nginx -s quit

# 查看进程情况,此时其中一个worker进程已关闭,而存在进程处于即将关闭状态
[root@RockyLinux9 ~]# ps -ef|grep nginx
root        4824       1  0 22:41 ?        00:00:00 nginx: master process nginx
nginx       4825    4824  3 22:41 ?        00:00:00 nginx: worker process is shutting down
root        4831    4591  0 22:41 pts/0    00:00:00 grep --color=auto nginx

# 查看进程情况,此时nginx程序已关闭
[root@RockyLinux9 ~]# ps -ef|grep nginx
root        4833    4591  0 22:42 pts/0    00:00:00 grep --color=auto nginx

[root@RockyLinux9 ~]# ps -ef|grep nginx
root        1641       1  0 00:12 ?        00:00:00 nginx: master process nginx
nginx       1642    1641  0 00:12 ?        00:00:00 nginx: worker process
nginx       1643    1641  0 00:12 ?        00:00:00 nginx: worker process
root        1645    1408  0 00:12 pts/0    00:00:00 grep --color=auto nginx
[root@RockyLinux9 ~]# kill -QUIT 1641
[root@RockyLinux9 ~]# ps -ef|grep nginx
root        1641       1  0 00:12 ?        00:00:00 nginx: master process nginx
nginx       1643    1641  0 00:12 ?        00:00:00 nginx: worker process is shutting down
root        1756    1408  0 00:13 pts/0    00:00:00 grep --color=auto nginx

3. 重新加载配置文件,启动新的工作进程,优雅的关闭旧进程

  • nginx -s reload
  • kill -HUP 主进程PID
# 查看进程情况
[root@RockyLinux9 ~]# ps -ef|grep nginx
root        4751       1  0 22:22 ?        00:00:00 nginx: master process nginx
nginx       4752    4751  0 22:22 ?        00:00:00 nginx: worker process
nginx       4753    4751  0 22:22 ?        00:00:00 nginx: worker process
root        4810    4591  0 22:30 pts/0    00:00:00 grep --color=auto nginx

# 发送reload信号
[root@RockyLinux9 ~]# nginx -s reload

# 查看进程情况,发现主进程未发生变化,而两个worker进程的pid更改了,启动时间也发生了变化
[root@RockyLinux9 ~]# ps -ef|grep nginx
root        4751       1  0 22:22 ?        00:00:00 nginx: master process nginx
nginx       4812    4751  0 22:30 ?        00:00:00 nginx: worker process
nginx       4813    4751  0 22:30 ?        00:00:00 nginx: worker process
root        4815    4591  0 22:30 pts/0    00:00:00 grep --color=auto nginx

[root@RockyLinux9 ~]# ps -ef|grep nginx
root        1761       1  0 00:16 ?        00:00:00 nginx: master process nginx
nginx       1762    1761  0 00:16 ?        00:00:00 nginx: worker process
nginx       1763    1761  0 00:16 ?        00:00:00 nginx: worker process
root        1765    1408  0 00:16 pts/0    00:00:00 grep --color=auto nginx
[root@RockyLinux9 ~]# kill -HUP 1761
[root@RockyLinux9 ~]# ps -ef|grep nginx
root        1761       1  0 00:16 ?        00:00:00 nginx: master process nginx
nginx       1766    1761  0 00:17 ?        00:00:00 nginx: worker process
nginx       1767    1761  0 00:17 ?        00:00:00 nginx: worker process
root        1769    1408  0 00:17 pts/0    00:00:00 grep --color=auto nginx

4. 重新生成日志文件。可用于定期备份日志文件

  • nginx -s reopen
  • kill -USR1 主进程PID
# 查看日志文件
[root@RockyLinux9 ~]# ll -h /usr/local/nginx/logs/
total 12K
-rw-r--r--. 1 root root 4.0K May 10 22:23 access.log
-rw-r--r--. 1 root root 1.8K May 10 22:22 error.log
-rw-r--r--. 1 root root    5 May 10 22:22 nginx.pid

# 修改日志名称
[root@RockyLinux9 ~]# mv /usr/local/nginx/logs/access.log{,.bak}
[root@RockyLinux9 ~]# ll -h /usr/local/nginx/logs/
total 12K
-rw-r--r--. 1 root root 4.0K May 10 22:23 access.log.bak

# 通过浏览器访问页面,显示访问日志还是写入到了原文件中
[root@RockyLinux9 ~]# ll -h /usr/local/nginx/logs/
total 16K
-rw-r--r--. 1 root root 4.4K May 10 22:27 access.log.bak

# 发送reopen信号,新创建了一个日志文件,再访问页面,日志则被写入到了新文件中
[root@RockyLinux9 ~]# nginx -s reopen
[root@RockyLinux9 ~]# ll -h /usr/local/nginx/logs/
total 16K
-rw-r--r--. 1 nginx root    0 May 10 22:28 access.log
-rw-r--r--. 1 root  root 4.4K May 10 22:27 access.log.bak
[root@RockyLinux9 ~]# ll -h /usr/local/nginx/logs/
total 20K
-rw-r--r--. 1 nginx root  466 May 10 22:28 access.log
-rw-r--r--. 1 root  root 4.4K May 10 22:27 access.log.bak
  • 有关与kill命令其他信号控制参数,会在《Nginx升级和回滚》章节进行演示操作。

来自: 学习Nginx(三):命令与信号

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Linux技术宅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值