Nginx中平滑更新及httpd段配置
文章目錄
- Nginx中平滑更新及httpd段配置
-
- 平滑更新
-
- **nginx 平滑更新**
- http段中server
-
- **nginx 錯誤頁面配置**
- location 段
平滑更新
随着nginx越來越流行,并且nginx的優勢也越來越明顯,也使得nginx的版本疊代也越來越快,而随之帶來對nginx更新的工作也是難所避免的
nginx 友善地幫助我們實作了平滑更新。其原理簡單概括,就是:
- (1)在不停掉老程序的情況下,啟動新程序。
- (2)老程序負責處理仍然沒有處理完的請求,但不再接受處理請求。
- (3)新程序接受新請求。
- (4)老程序處理完所有請求,關閉所有連接配接後,停止。 這樣就很友善地實作了平滑更新。一般有兩種情況下需要更新 nginx,一種是确實要更新 nginx 的版本,另一種是要為 nginx 添加新的子產品
nginx 平滑更新
#目前ngixn版本和即将更新的nginx源碼包
[[email protected] ~]# ls
anaconda-ks.cfg nginx-1.22.0.tar.gz to.sh
[[email protected] ~]# nginx -v
nginx version: nginx/1.20.2
#解壓
[[email protected] ~]# tar -xf nginx-1.22.0.tar.gz
[[email protected] ~]# ls
anaconda-ks.cfg nginx-1.22.0 nginx-1.22.0.tar.gz to.sh
[[email protected] ~]# unzip nginx_module_echo-master.zip
#沒有unzip就yum裝unzip
[[email protected] ~]# ls
anaconda-ks.cfg nginx-1.22.0.tar.gz nginx_module_echo-master.zip
nginx-1.22.0 nginx_module_echo-master to.sh
#進入解壓後目錄并檢視之前編譯參數
[[email protected] ~]# cd nginx-1.22.0/
[[email protected] nginx-1.22.0]# nginx -V
nginx version: nginx/1.20.2
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-15) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
#開始編譯,并且在原先的基礎上加入需要新加入的子產品
[[email protected] nginx-1.22.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --add-module=/root/nginx_module_echo-master
#最後的--add-module就是新加入的子產品
#需要注意,這裡編譯完成後不要make install,隻更新,不覆寫配置檔案
[[email protected] nginx-1.22.0]# make
#備份老版本的nginx的二進制檔案,期間nginx不會停止服務
[[email protected] nginx-1.22.0]# ls
CHANGES CHANGES.ru LICENSE Makefile README auto conf configure contrib html man objs src
[[email protected] nginx-1.22.0]# cd objs/
[[email protected] objs]# ls
Makefile autoconf.err nginx.8 ngx_auto_headers.h ngx_modules.o
addon nginx ngx_auto_config.h ngx_modules.c src
[[email protected] objs]# mv /usr/local/nginx/sbin/nginx{,-2}
[[email protected] objs]# ls /usr/local/nginx/sbin/
nginx-2
#複制新的nginx二進制檔案,進入新的nginx源碼包
[[email protected] objs]# ls
Makefile autoconf.err nginx.8 ngx_auto_headers.h ngx_modules.o
addon nginx ngx_auto_config.h ngx_modules.c src
[[email protected] objs]# pwd
/root/nginx-1.22.0/objs
[[email protected] objs]# cp nginx /usr/local/nginx/sbin/
[[email protected] objs]# ls /usr/local/nginx/sbin/
nginx nginx-2
#測試新版本的nginx是否正常
[[email protected] objs]# /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
#這裡使用直接pkill掉程序,秒更新,再啟用。如果服務一秒都不能斷掉,那就使用給nginx發送平滑遷移信号(若不清楚pid路徑,請檢視nginx配置檔案) 如: kill -USR2 `cat /var/run/nginx.pid`
[[email protected] objs]# pkill nginx
[[email protected] objs]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
#啟動nginx
[[email protected] objs]# systemctl restart nginx.service
[[email protected] objs]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[[email protected] objs]# nginx -v
nginx version: nginx/1.22.0
[[email protected] objs]# nginx -V
nginx version: nginx/1.22.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-15) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --add-module=/root/nginx_module_echo-master
#上面是示範,大可直接把所有指令都結合在一起用;間隔
例如在完成編譯後更新版本:
[[email protected] objs]# mv /usr/local/nginx/sbin/nginx{,-2}; cp nginx /usr/local/nginx/sbin/;pkill nginx;systemctl restart nginx.service
這樣就可以減小nginx關閉時間
http段中server
nginx 錯誤頁面配置
nginx錯誤頁面包括404 403 500 502 503 504等頁面,隻需要在server中增加以下配置即可:
#error_page 404 403 500 502 503 504 /404.html;
location = /404.html {
root /usr/local/nginx/html;
}
注意:
/usr/local/nginx/html/ 路徑下必須有404.html這個檔案!!!
404.html上如果引用其他檔案的png或css就會有問題,顯示不出來,因為其他檔案的通路也要做配置; 為了簡單,可以将css嵌入檔案中,圖檔用base編碼嵌入;
location 段
location區段,通過指定模式來與用戶端請求的URI相比對
//功能:允許根據使用者請求的URI來比對定義的各location,比對到時,此請求将被相應的location配置塊中的配置所處理,例如做通路控制等功能
//文法:location [ 修飾符 ] pattern {......}
常用修飾符說明:
修飾符 | 功能 |
---|---|
= | 精确比對 |
~ | 正規表達式模式比對,區分大小寫 |
~* | 正規表達式模式比對,不區分大小寫 |
^~ | 字首比對,類似于無修飾符的行為,也是以指定子產品開始,不同的是,如果模式比對,那麼就停止搜尋其他模式了,不支援正規表達式 |
@ | 定義命名location區段,這些區段用戶端不能通路,隻可以由内部産生的請求來通路,如try_files或error_page等 |
查找順序和優先級:由高到底依次為
- 帶有=的精确比對優先
- 正規表達式按照他們在配置檔案中定義的順序
- 帶有^~修飾符的,開頭比對
- 帶有或*修飾符的,如果正規表達式與URI比對
- 沒有修飾符的精确比對
#這裡前面裝了一個echo子產品,使用echo子產品驗證效果
server {
listen 80;
server_name localhost;
location / {
echo "沒有修飾符比對 a";
}
location ~ ^/y*.x$ {
echo "正規表達式區分大小寫比對 b ";
}
location = /tang {
echo "精準比對 c ";
}
location ~* ^/y*.x$ {
echo "正規表達式不區分大小寫比對 d ";
}
}