參考資料: Nginx中文文檔: http://www.nginx.cn/nginxchscommandline Nginx的啟動、停止、平滑重啟、信號控制和平滑升級:http://zachary-guo.iteye.com/blog/1358312 命令行參數: 常用命令: -c filename ...
參考資料:
Nginx中文文檔: http://www.nginx.cn/nginxchscommandline
Nginx的啟動、停止、平滑重啟、信號控制和平滑升級:http://zachary-guo.iteye.com/blog/1358312
命令行參數:
常用命令:
-c filename:設置配置文件。
-t :不運行,而僅僅測試配置文件。nginx 將檢查配置文件的語法的正確性,並嘗試打開配置文件中所引用到的文件。
-s :傳遞一個信號,stop快速關閉,quit從容關閉,reopen重新打開日誌文件、用於切換日誌文件,reload重載配置文件。
-v :顯示nginx的版本。
-V:顯示nginx的版本,編譯器版本和配置參數
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[root@localhost /]# alias nginx='/usr/local/nginx/sbin/nginx'
[root@localhost /]# alias

[root@localhost /]# nginx -h nginx version: nginx/1.11.13 Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives] Options: -?,-h : this help -v : show version and exit -V : show version and configure options then exit -t : test configuration and exit -T : test configuration, dump it and exit -q : suppress non-error messages during configuration testing -s signal : send signal to a master process: stop, quit, reopen, reload -p prefix : set prefix path (default: /usr/local/nginx/) -c filename : set configuration file (default: conf/nginx.conf) -g directives : set global directives out of configuration file [root@localhost /]#
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
控制信號:
可以使用信號系統來控制主進程。預設,nginx 將其主進程的 pid 寫入到 /usr/local/nginx/logs/nginx.pid 文件中。通過傳遞參數給 ./configure 或使用 pid 指令,來改變該文件的位置。
主進程可以處理以下的信號:
TERM,INT | 快速關閉 |
QUIT | 從容關閉 |
HUP |
重新配置 用新的配置開始新的工作進程 從容關閉舊的工作進程 |
USR1 | 重新打開日誌文件,在切割日誌時用途較大 |
USR2 | 平滑升級可執行程式 |
WINCH | 從容關閉工作進程 |
啟動:
啟動代碼格式:nginx安裝目錄地址 -c nginx配置文件地址
[root@localhost ~]# alias nginx='/usr/local/nginx/sbin/nginx' [root@localhost ~]# nginx -c /usr/local/nginx/conf/nginx.conf [root@localhost ~]# ps -ef|grep nginx root 2073 1 0 10:37 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 2074 2073 0 10:37 ? 00:00:00 nginx: worker process root 2076 2022 0 10:37 pts/0 00:00:00 grep nginx [root@localhost ~]#
停止:
從容停止:
# ` 字元是數字鍵盤 1 字元左邊的那個字元
[root@localhost ~]# kill -QUIT `cat /usr/local/nginx/logs/nginx.pid`
快速停止:
[root@localhost ~]# kill -TERM `cat /usr/local/nginx/logs/nginx.pid`
或
[root@localhost ~]# kill -INT `cat /usr/local/nginx/logs/nginx.pid`
強行停止:
[root@localhost ~]# kill -9 nginx
重啟:
1.普通重啟:關閉進程,修改配置後,重啟進程
2.重新載入配置文件,不重啟進程,不會停止處理請求
3.平滑更新nginx二進位,不會停止處理請求
=========================================================================
註意:在重載前,測試一下配置文件:
# -t 參數將檢查配置文件的語法是否正確,預設會檢查 /usr/local/nginx/conf/nginx.conf 文件
[root@localhost ~]# /usr/local/nginx/sbin/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
# 如果要對指定配置文件進行語法檢查,可以繼續添加 -c 參數
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf 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 [root@localhost ~]#
=========================================================================
nginx的平滑重啟:
如果改變了nginx的配置文件,想重啟nginx,同樣可以發送系統型號給nginx主進程的方式來進行,重啟之前,要確認配置文件是否正確
[root@localhost ~]# kill -HUP 【nginx主進程號】
當 nginx 接收到 HUP 信號時,它會嘗試先解析配置文件,如果成功,就應用新的配置文件(例如,重新打開日誌文件或監聽的套接字)。之後,nginx 運行新的工作進程並從容關閉舊的工作進程。通知工作進程關閉監聽套接字,但是繼續為當前連接的客戶提供服務。所有的客戶端的服務完成後,舊的工作進程被關閉。如果新的配置文件應用失敗,nginx 將繼續使用舊的配置文件進行工作。
nginx的平滑升級:
當需要將正在運行中的nginx升級、添加/刪除伺服器模塊時,可以在不中斷服務的情況下,使用新的nginx可執行程式替換舊的:
1.使用新的可執行程式替換舊的可執行程式,對於編譯安裝的nginx,可以將新版本編譯安裝到nginx安裝路徑中,替換之前,備份一下舊的可執行文件
2.發送以下指令:
[root@localhost ~]# kill -USR2 【舊版本的nginx主進程號】
3.舊版本的nginx的主進程將重命名它的pid文件為.oldbin(例如:/usr/local/nginx/logs/nginx.pid.oldbin),然後執行新版本的nginx可執行程式,依次啟動新的主進程和新的工作進程。
4.此時,新、舊版本的nginx實例會同時運行,共同處理輸入的請求,要逐步停止舊版本的nginx,必鬚髮送WINCH信號給舊的主進程,然後,它的工作進程就開始從容關閉:
[root@localhost ~]# kill -WINCH 【舊版本的nginx主進程號】
5.一段時間後,舊的工作進程(worker process)處理了所有已連接的請求後退出,僅由新的工作進程來處理輸入的請求了。
6.這時候,可以決定是使用新版本,還是恢復到舊版了:
kill -HUP 【舊的主進程號】:nginx將在不重載配置文件的情況下啟動它的工作進程
kill -QUIT 【新的主進程號】:從容關閉其工作進程(worker process)
kill -TERM 【新的主進程號】:強制退出
kill 【新的主進程號或舊的主進程號】:如果因為某些原因新的工作進程不能退出,則向其發送kill信號
新的主進程退出後,舊的主進程會移除.oldbin首碼,恢復為它的.pid文件,這樣,一切就都恢復到升級之前了。
如果嘗試升級成功,而你也希望保留新的伺服器時,可發送QUIT信號給舊的主進程,使其退出而只留下新的伺服器運行。