1. 程式人生 > >【學習筆記】啟動Nginx、檢視nginx程序、檢視nginx服務主程序的方式、Nginx服務可接受的訊號、nginx幫助命令、Nginx平滑重啟、Nginx伺服器的升級

【學習筆記】啟動Nginx、檢視nginx程序、檢視nginx服務主程序的方式、Nginx服務可接受的訊號、nginx幫助命令、Nginx平滑重啟、Nginx伺服器的升級



1.啟動nginx的方式:

cd /usr/local/nginx

ls

./nginx -c nginx.conf

2.檢視nginx的程序方式:

[[email protected] nginx]# ps –ef | grep nginx

[[email protected] nginx]# ps -ef | grep nginx

root2109410 07:52 ?00:00:00 nginx: master process ./nginx -c nginx.conf

nginx21095 210940 07:52 ?00:00:00 nginx: worker process

root2127030191 08:05 pts/100:00:00 grep nginx

3.檢視Nginx服務主程序的方式:

[[email protected] nginx]# cat nginx.pid

21094

這個程序號和上面的程序號是一樣的

4.Nginx服務可接受的訊號

訊號

作用

TERMINT

快速停止Nginx服務

QUIT

平緩停止Nginx服務

HUP

使用新的配置檔案啟動程序,之後平緩停止原有程序,也就是所謂的平滑重啟

USR1

重新開啟日誌檔案,常用於日誌切割,在相關章節中會對此進一步說明

USR2

使用新版本的Nginx檔案啟動服務,之後平緩停止原有Nginx程序,也就是所謂的平滑升級

WINCH

平緩停止worker process,用於Nginx伺服器平滑升級

nginx

服務主程序傳送訊號也有兩種方法。一種是使用nginx二進位制檔案

另一種方法是使用kill命令傳送訊號,其使用方法是:

Kill -9 PID

也可以通過發訊號的方式:

使用TERM訊號

[[email protected] nginx]# kill TERM 21094(其中21094master程序的pid,其中TERM可以換成INT QUIT)

-bash: kill: TERM: arguments must be process or job IDs

[[email protected] nginx]# ps -ef | grep nginx

root2141730190 08:16 pts/1

00:00:00 grep nginx

重啟命令

[[email protected] nginx]# ./nginx -c nginx.conf

[[email protected] nginx]# ps -ef | grep nginx

root2144010 08:18 ?00:00:00 nginx: master process ./nginx -c nginx.conf

nginx21441 214400 08:18 ?00:00:00 nginx: worker process

root2144530192 08:18 pts/100:00:00 grep nginx

5、使用nginx的幫助的方式

nginx -h

[[email protected] nginx]# ./nginx -h

nginx version: nginx/1.10.1

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: /usr/local/nginx/nginx.conf)

-g directives : set global directives out of configuration file

[[email protected] nginx]#

通過-v 用來顯示Nginx伺服器的版本號;

通過-V顯示版本號,同事顯示相關的配置資訊

通過-t檢查Nginx伺服器配置檔案是否有語法錯誤,可以與-c聯用,使輸出內容更詳細,這對查詢配置檔案中的語法錯誤很有幫助,如果檢查通過,將顯示類似下面的資訊

[[email protected] nginx]# pwd

/usr/local/nginx

[[email protected] nginx]# ls

client_body_tempfastcgi_params.defaultkoi-winnginxproxy_tempuwsgi_params

fastcgi.conffastcgi_templogsnginx.confscgi_paramsuwsgi_params.default

fastcgi.conf.defaulthtmlmime.typesnginx.conf.defaultscgi_params.defaultuwsgi_temp

fastcgi_paramskoi-utfmime.types.defaultnginx.pidscgi_tempwin-utf

[[email protected] nginx]# ./nginx -t -c nginx.conf

nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/nginx.conf test is successful

[[email protected] nginx]#

可以執行

[[email protected] nginx]# ./nginx -V

nginx version: nginx/1.10.1

built by gcc 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC)

built with OpenSSL 1.0.1c 10 May 2012

TLS SNI support enabled

configure arguments: --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=/home/toto/software/nginxdepents/pcre-8.37 --with-zlib=/home/toto/software/nginxdepents/zlib-1.2.8 --with-openssl=/home/toto/software/nginxdepents/openssl-1.0.1c --with-http_stub_status_module --user=nginx --group=nginx

[[email protected] nginx]#

殺死主程序的方式同樣可以是:

./nginx SIGNAL nginx.pid

主要最後面一個引數是nginx.pid的路徑

6Nginx服務的重啟

關於Nginx平滑重啟,平滑重啟是這樣一個過程,Nginx服務程序接收到訊號後,首先讀取新的Nginx配置檔案,如果配置語法正確,則啟動新的Nginx服務,然後平緩關閉舊的服務程序;如果新的Nginx配置有問題,將顯示錯誤,仍然使用舊的Nginx提供服務。

使用以下命令實現Nginx服務的平滑重啟

./nginx –g HUP [-c newConfFile]

HUP訊號用於傳送平滑重啟訊號。

newConfFile,可選項,用於指定新配置檔案的路徑

或者,使用新的配置檔案代替了舊的配置檔案後,使用:

Kill HUP `nginx.pid`(注意最後一個引數連邊的引號;引號裡面是nginx.pid的路徑)

也可以實現平滑重啟。

7Nginx伺服器的升級

如果要對當前的Nginx伺服器進行版本升級,應用新模組,最簡單的辦法是停止當前Nginx服務,然後開啟新的Nginx服務,但這樣就會導致在一段時間內,使用者無法訪問伺服器。為了解決這個問題,Nginx伺服器提供平滑升級的功能。

平滑升級的過程是這樣的,Nginx服務接收到USR2訊號後首先將舊的nginx.pid檔案(如果在配置檔案中更改過這個檔案的名字,也是相同的過程)新增.oldbin,變為nginx.pid.oldbin檔案;然後執行新版本Nginx伺服器的二進位制檔案啟動服務。如果新的服務啟動成功,系統中將由新舊兩個Nginx服務公用提供Web服務。如果新的服務啟動成功,系統中將有新舊兩個Nginx服務共同提供Web服務。之後,需要向舊的Nginx服務程序傳送WINCH訊號,使舊的Nginx服務平滑停止,並刪除nginx.pid.oldbin檔案。在傳送WINCH訊號之前,可以隨時停止新的Nginx服務。

注意:

為了實現Nginx伺服器的平滑升級,新的伺服器安裝路徑應該和舊的保持一致。因此建議使用者在安裝新伺服器之前先備份舊伺服器。如果由於某種原因無法保持新舊伺服器安裝路徑一致,則可以先使用以下命令將舊伺服器的安裝路徑更改為新伺服器的安裝路徑:

./nginx –p newInstallPath

其中,newInstallPath為新伺服器的安裝路徑。之後,備份舊伺服器,安裝新伺服器即可。

做好準備工作以後,使用以下命令實現Nginx服務的平滑升級。

./nginx –g USR2

其中USR2訊號用於傳送平滑升級訊號。或者,使用:

Kill USR2 `nginx.pid`注意,最後一個引數裡面是nginx.pid的路徑

通過ps –ef | grep nginx檢視新的Nginx伺服器啟動正常,再使用:

./nginx –g WINCH

其中,WINCH訊號用於傳送平滑停止舊服務訊號。或者,使用:

Kill WIN `nginx.pid`

這樣就不停止提供Web服務的前提下完成了Nginx伺服器的平滑升級。