nginx平滑升級(jí)及nginx配置文件詳解
目錄
- nginx平滑升級(jí)及nginx配置文件
- nginx平滑升級(jí)并添加新功能
- nginx配置文件
- nginx.conf配置詳解
- 用于調(diào)試、定位問題的配置參數(shù)
- 正常運(yùn)行必備的配置參數(shù)
- 優(yōu)化性能的配置參數(shù)
- 網(wǎng)絡(luò)連接相關(guān)的配置參數(shù)
- fastcgi的相關(guān)配置參數(shù)
- nginx作為web服務(wù)器時(shí)使用的配置:http{}段的配置參數(shù)
- http{}段配置指令:
nginx平滑升級(jí)及nginx配置文件
nginx平滑升級(jí)并添加新功能
1.先獲取老版本的編譯信息
2.獲取新版本安裝包和功能包
3.配置新版本或功能,配置時(shí)加上老版本的編譯參數(shù),然后添加新功能模塊
4.進(jìn)行編譯,編譯完不進(jìn)行安裝操作
5.備份老版本程序,使用復(fù)制的方法。在停掉老版本程序的進(jìn)程,將新版本程序復(fù)制到老版本所在位置直接替換掉老版本程序并啟動(dòng)新版本程序
//查看老版本編譯信息 [root@nginx ~]# nginx -V nginx version: nginx/1.20.2 built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (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_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module //準(zhǔn)備好新版本安裝包和功能包 [root@nginx ~]# wget http://nginx.org/download/nginx-1.22.0.tar.gz [root@nginx ~]# yum -y install git [root@nginx ~]# git clone https://gitee.com/Their-own/nginx_module_echo.git [root@nginx ~]# ls anaconda-ks.cfg nginx-1.20.2 nginx-1.20.2.tar.gz nginx-1.22.0 nginx-1.22.0.tar.gz nginx_module_echo //解壓并編譯 [root@nginx ~]# tar xf nginx-1.22.0.tar.gz [root@nginx ~]# cd nginx-1.22.0 [root@nginx 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 \ --add-module=../nginx_module_echo //添加新功能使用-add-module=模塊目錄位置 [root@nginx nginx-1.22.0]# make [root@nginx nginx-1.22.0]# ls CHANGES CHANGES.ru LICENSE Makefile README auto conf configure contrib html man objs src [root@nginx nginx-1.22.0]# objs/nginx -v nginx version: nginx/1.22.0 [root@nginx nginx-1.22.0]# nginx -v nginx version: nginx/1.20.2 //一步到位 [root@nginx nginx-1.22.0]# cp /usr/local/nginx/sbin/nginx{,-bak};pkill nginx;\cp ./objs/nginx /usr/local/nginx/sbin/nginx;systemctl start nginx [root@nginx nginx-1.22.0]# nginx -v nginx version: nginx/1.22.0 [root@nginx nginx-1.22.0]# ss -anlt StateRecv-QSend-Q Local Address:Port Peer Address:Port Process LISTEN 0 1280.0.0.0:80 0.0.0.0:* LISTEN 0 1280.0.0.0:22 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:*
nginx配置文件
主配置文件:/usr/local/nginx/conf/nginx.conf
默認(rèn)啟動(dòng)nginx時(shí),使用的配置文件是:安裝路徑/conf/nginx.conf文件
可以在啟動(dòng)nginx時(shí)通過-c選項(xiàng)來指定要讀取的配置文件
nginx常見的配置文件及其作用
nginx.conf配置詳解
nginx.conf的內(nèi)容分為以下幾段:
- main配置段:全局配置段。其中main配置段中可能包含event配置段
- event {}:定義event模型工作特性
- http {}:定義http協(xié)議相關(guān)的配置
配置指令:要以分號(hào)結(jié)尾,語法格式如下:
derective value1 [value2 ...]; 支持使用變量: 內(nèi)置變量:模塊會(huì)提供內(nèi)建變量定義 自定義變量:set var_name value
用于調(diào)試、定位問題的配置參數(shù)
daemon {on|off}; //是否以守護(hù)進(jìn)程方式運(yùn)行nginx,調(diào)試時(shí)應(yīng)設(shè)置為off master_process {on|off}; //是否以master/worker模型來運(yùn)行nginx,調(diào)試時(shí)可以設(shè)置為off error_log 位置 級(jí)別; //配置錯(cuò)誤日志 error_log里的位置和級(jí)別能有以下可選項(xiàng):
正常運(yùn)行必備的配置參數(shù)
user USERNAME [GROUPNAME]; //指定運(yùn)行worker進(jìn)程的用戶和組 pid /path/to/pid_file; //指定nginx守護(hù)進(jìn)程的pid文件 worker_rlimit_nofile number; //設(shè)置所有worker進(jìn)程最大可以打開的文件數(shù),默認(rèn)為1024 worker_rlimit_core size; //指明所有worker進(jìn)程所能夠使用的總體的最大核心文件大小,保持默認(rèn)即可
優(yōu)化性能的配置參數(shù)
worker_processes n; //啟動(dòng)n個(gè)worker進(jìn)程,這里的n為了避免上下文切換,通常設(shè)置為cpu總核心數(shù)-1或等于總核心數(shù) worker_cpu_affinity cpumask ...; //將進(jìn)程綁定到某cpu中,避免頻繁刷新緩存 //cpumask:使用8位二進(jìn)制表示cpu核心,如: 0000 0001 //第一顆cpu核心 0000 0010 //第二顆cpu核心 0000 0100 //第三顆cpu核心 0000 1000 //第四顆cpu核心 0001 0000 //第五顆cpu核心 0010 0000 //第六顆cpu核心 0100 0000 //第七顆cpu核心 1000 0000 //第八顆cpu核心 timer_resolution interval; //計(jì)時(shí)器解析度。降低此值,可減少gettimeofday()系統(tǒng)調(diào)用的次數(shù) worker_priority number; //指明worker進(jìn)程的nice值 6.5 事件相關(guān)的配置:event{}段中的配置參數(shù) accept_mutex {off|on}; //master調(diào)度用戶請(qǐng)求至各worker進(jìn)程時(shí)使用的負(fù)載均衡鎖;on表示能讓多個(gè)worker輪流地、序列化地去響應(yīng)新請(qǐng)求 lock_file file; //accept_mutex用到的互斥鎖鎖文件路徑 use [epoll | rtsig | select | poll]; //指明使用的事件模型,建議讓nginx自行選擇 worker_connections #; //每個(gè)進(jìn)程能夠接受的最大連接數(shù)
網(wǎng)絡(luò)連接相關(guān)的配置參數(shù)
keepalive_timeout number; //長(zhǎng)連接的超時(shí)時(shí)長(zhǎng),默認(rèn)為65s keepalive_requests number; //在一個(gè)長(zhǎng)連接上所能夠允許請(qǐng)求的最大資源數(shù) keepalive_disable [msie6|safari|none]; //為指定類型的UserAgent禁用長(zhǎng)連接 tcp_nodelay on|off; //是否對(duì)長(zhǎng)連接使用TCP_NODELAY選項(xiàng),為了提升用戶體驗(yàn),通常設(shè)為on client_header_timeout number; //讀取http請(qǐng)求報(bào)文首部的超時(shí)時(shí)長(zhǎng) client_body_timeout number; //讀取http請(qǐng)求報(bào)文body部分的超時(shí)時(shí)長(zhǎng) send_timeout number; //發(fā)送響應(yīng)報(bào)文的超時(shí)時(shí)長(zhǎng)
fastcgi的相關(guān)配置參數(shù)
LNMP:php要啟用fpm模型
配置示例如下:
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; }
nginx作為web服務(wù)器時(shí)使用的配置:http{}段的配置參數(shù)
http{…}:配置http相關(guān),由ngx_http_core_module模塊引入。nginx的HTTP配置主要包括四個(gè)區(qū)塊,結(jié)構(gòu)如下:
http {//協(xié)議級(jí)別 include mime.types; default_type application/octet-stream; keepalive_timeout 65; gzip on; upstream {//負(fù)載均衡配置 ... } server {//服務(wù)器級(jí)別,每個(gè)server類似于httpd中的一個(gè)<VirtualHost> listen 80; server_name localhost; location / {//請(qǐng)求級(jí)別,類似于httpd中的<Location>,用于定義URL與本地文件系統(tǒng)的映射關(guān)系 root html; index index.html index.htm; } } }
http{}段配置指令:
server {}:定義一個(gè)虛擬主機(jī),示例如下:
server { listen 80; server_name www.idfsoft.com; root "/vhosts/web"; }
listen:指定監(jiān)聽的地址和端口
listen address[:port]; listen port; server_name NAME [...]; 后面可跟多個(gè)主機(jī),名稱可使用正則表達(dá)式或通配符
當(dāng)有多個(gè)server時(shí),匹配順序如下:
- 先做精確匹配檢查
- 左側(cè)通配符匹配檢查,如*.idfsoft.com
- 右側(cè)通配符匹配檢查,如mail.*
- 正則表達(dá)式匹配檢查,如~ ^.*.idfsoft.com$
- default_server
root path; 設(shè)置資源路徑映射,用于指明請(qǐng)求的URL所對(duì)應(yīng)的資源所在的文件系統(tǒng)上的起始路徑
alias path; 用于location配置段,定義路徑別名
index file; 默認(rèn)主頁面
index index.php index.html;error_page code […] [=code] URI | @name 根據(jù)http響應(yīng)狀態(tài)碼來指明特用的錯(cuò)誤頁面,例如 error_page 404 /404_customed.html
[=code]:以指定的響應(yīng)碼進(jìn)行響應(yīng),而不是默認(rèn)的原來的響應(yīng),默認(rèn)表示以新資源的響應(yīng)碼為其響應(yīng)碼,例如 error_page 404 =200 /404_customed.html
log_format 定義日志格式
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; //注意:格式名main可自己定義,但要一一對(duì)應(yīng),另外此處可用變量為nginx各模塊內(nèi)建變量
location區(qū)段,通過指定模式來與客戶端請(qǐng)求的URI相匹配
//功能:允許根據(jù)用戶請(qǐng)求的URI來匹配定義的各location,匹配到時(shí),此請(qǐng)求將被相應(yīng)的location配置塊中的配置所處理,例如做訪問控制等功能 //語法:location [ 修飾符 ] pattern {......} 常用修飾符說明:
查找順序和優(yōu)先級(jí):由高到底依次為
- 帶有=的精確匹配優(yōu)先
- 帶有^~修飾符的,開頭匹配
- 正則表達(dá)式按照他們?cè)谂渲梦募卸x的順序
帶有或*修飾符的,如果正則表達(dá)式與URI匹配 - 沒有修飾符的精確匹配
優(yōu)先級(jí)如下:
( location = 路徑 ) --> ( location ^~ 路徑 ) --> ( location ~ 正則 ) --> ( location ~* 正則 ) --> ( location 路徑 )
//如沒添加任何修飾符則按先后順序 [root@localhost conf]# vim nginx.conf //添加三個(gè)訪問頁面測(cè)試 location / { echo "haha"; } location /xixi { echo "xixi"; } location /hehe { echo "hehe"; } [root@localhost conf]# systemctl restart nginx.service [root@localhost conf]# curl 192.168.111.141 haha [root@localhost conf]# curl 192.168.111.141/xixi xixi [root@localhost conf]# curl 192.168.111.141/hehe hehe
// =精確匹配 [root@localhost conf]# vim nginx.conf location / { echo "haha; } location /xixi { echo "xixi"; } location = /xixi { echo "hehe"; } [root@localhost conf]# systemctl restart nginx.service //可以看到兩個(gè)目錄一樣,但是=優(yōu)先級(jí)大于沒有加=的,所以訪問的是hehe [root@localhost conf]# curl 192.168.111.141/xixi hehe
//添加 ~ 為區(qū)分大小寫 [root@localhost conf]# vim nginx.conf location / { echo "haha"; } location /xixi { echo "xixi"; } location ~ /xixi { echo "hehe"; } [root@localhost conf]# systemctl restart nginx.service //因?yàn)閰^(qū)分大小寫找不到資源所以輸出的是默認(rèn)haha [root@localhost conf]# curl 192.168.111.141/XIXI haha [root@localhost conf]# curl 192.168.111.141/xixi hehe
// ~* 為不區(qū)分大小寫 [root@localhost conf]# vim nginx.conf location / { echo "haha"; } location /xixi { echo "xixi"; } location ~* /xixi { echo "hehe"; } [root@localhost conf]# systemctl restart nginx.service //因?yàn)椴粎^(qū)分大小寫所以XIXI和xixi都能訪問到hehe [root@localhost conf]# curl 192.168.111.141/XIXI hehe [root@localhost conf]# curl 192.168.111.141/xixi hehe
// ^~ 為前綴匹配 [root@localhost conf]# vim nginx.conf location / { echo "haha"; } location ^~/xixi { echo "xixi"; } location ~ /xixi { echo "hehe"; [root@localhost conf]# systemctl restart nginx.service //前綴匹配如果訪問到了資源則停止搜索 [root@localhost conf]# curl 192.168.111.141/xixi xixi
到此這篇關(guān)于nginx平滑升級(jí)及nginx配置文件的文章就介紹到這了,更多相關(guān)nginx平滑升級(jí)內(nèi)容請(qǐng)搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!
相關(guān)文章:
