命令及選項
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.presentationpptx;application/vnd.openxmlformats-officedocument.spreadsheetml.sheetxlsx;application/vnd.openxmlformats-officedocument.wordprocessingml.documentdocx;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.4s22: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(三):命令與信號