色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

nginx平滑升級及nginx配置文件詳解

瀏覽:160日期:2023-03-13 15:37:28
目錄
  • nginx平滑升級及nginx配置文件
    • nginx平滑升級并添加新功能
  • nginx配置文件
    • nginx.conf配置詳解
    • 用于調(diào)試、定位問題的配置參數(shù)
    • 正常運行必備的配置參數(shù)
    • 優(yōu)化性能的配置參數(shù)
    • 網(wǎng)絡(luò)連接相關(guān)的配置參數(shù)
    • fastcgi的相關(guān)配置參數(shù)
    • nginx作為web服務(wù)器時使用的配置:http{}段的配置參數(shù)
      • http{}段配置指令:

nginx平滑升級及nginx配置文件

nginx平滑升級并添加新功能

1.先獲取老版本的編譯信息

2.獲取新版本安裝包和功能包

3.配置新版本或功能,配置時加上老版本的編譯參數(shù),然后添加新功能模塊

4.進行編譯,編譯完不進行安裝操作

5.備份老版本程序,使用復(fù)制的方法。在停掉老版本程序的進程,將新版本程序復(fù)制到老版本所在位置直接替換掉老版本程序并啟動新版本程序

//查看老版本編譯信息
[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

//準備好新版本安裝包和功能包
[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
默認啟動nginx時,使用的配置文件是:安裝路徑/conf/nginx.conf文件
可以在啟動nginx時通過-c選項來指定要讀取的配置文件
nginx常見的配置文件及其作用

配置文件作用nginx.confnginx的基本配置文件mime.typesMIME類型關(guān)聯(lián)的擴展文件fastcgi.conf與fastcgi相關(guān)的配置proxy.conf與proxy相關(guān)的配置sites.conf配置nginx提供的網(wǎng)站,包括虛擬主機

nginx.conf配置詳解

nginx.conf的內(nèi)容分為以下幾段:

  • main配置段:全局配置段。其中main配置段中可能包含event配置段
  • event {}:定義event模型工作特性
  • http {}:定義http協(xié)議相關(guān)的配置
    配置指令:要以分號結(jié)尾,語法格式如下:
derective value1 [value2 ...];
支持使用變量:
 
內(nèi)置變量:模塊會提供內(nèi)建變量定義
自定義變量:set var_name value

用于調(diào)試、定位問題的配置參數(shù)

daemon {on|off};    //是否以守護進程方式運行nginx,調(diào)試時應(yīng)設(shè)置為off
master_process {on|off};    //是否以master/worker模型來運行nginx,調(diào)試時可以設(shè)置為off
error_log 位置 級別;    //配置錯誤日志
error_log里的位置和級別能有以下可選項:
位置級別file stderr syslog:server=address[,parameter=value] memory:sizedebug:若要使用debug級別,需要在編譯nginx時使用–with-debug選項 info notice warn error crit alert emerg

正常運行必備的配置參數(shù)

user USERNAME [GROUPNAME];    //指定運行worker進程的用戶和組
pid /path/to/pid_file;    //指定nginx守護進程的pid文件
worker_rlimit_nofile number;    //設(shè)置所有worker進程最大可以打開的文件數(shù),默認為1024
worker_rlimit_core size;    //指明所有worker進程所能夠使用的總體的最大核心文件大小,保持默認即可

優(yōu)化性能的配置參數(shù)

worker_processes n;    //啟動n個worker進程,這里的n為了避免上下文切換,通常設(shè)置為cpu總核心數(shù)-1或等于總核心數(shù)
worker_cpu_affinity cpumask ...;    //將進程綁定到某cpu中,避免頻繁刷新緩存
//cpumask:使用8位二進制表示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;    //計時器解析度。降低此值,可減少gettimeofday()系統(tǒng)調(diào)用的次數(shù)
worker_priority number;    //指明worker進程的nice值
6.5 事件相關(guān)的配置:event{}段中的配置參數(shù)
accept_mutex {off|on};    //master調(diào)度用戶請求至各worker進程時使用的負載均衡鎖;on表示能讓多個worker輪流地、序列化地去響應(yīng)新請求
lock_file file;    //accept_mutex用到的互斥鎖鎖文件路徑
use [epoll | rtsig | select | poll];    //指明使用的事件模型,建議讓nginx自行選擇
worker_connections #;    //每個進程能夠接受的最大連接數(shù)

網(wǎng)絡(luò)連接相關(guān)的配置參數(shù)

keepalive_timeout number;    //長連接的超時時長,默認為65s
keepalive_requests number;    //在一個長連接上所能夠允許請求的最大資源數(shù)
keepalive_disable [msie6|safari|none];    //為指定類型的UserAgent禁用長連接
tcp_nodelay on|off;    //是否對長連接使用TCP_NODELAY選項,為了提升用戶體驗,通常設(shè)為on
client_header_timeout number;    //讀取http請求報文首部的超時時長
client_body_timeout number;    //讀取http請求報文body部分的超時時長
send_timeout number;    //發(fā)送響應(yī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ù)器時使用的配置:http{}段的配置參數(shù)

http{…}:配置http相關(guān),由ngx_http_core_module模塊引入。nginx的HTTP配置主要包括四個區(qū)塊,結(jié)構(gòu)如下:

http {//協(xié)議級別
  include mime.types;
  default_type application/octet-stream;
  keepalive_timeout 65;
  gzip on;
  upstream {//負載均衡配置
    ...
  }
  server {//服務(wù)器級別,每個server類似于httpd中的一個<VirtualHost>
    listen 80;
    server_name localhost;
    location / {//請求級別,類似于httpd中的<Location>,用于定義URL與本地文件系統(tǒng)的映射關(guān)系
      root html;
      index index.html index.htm;
    }
  }
}

http{}段配置指令:

server {}:定義一個虛擬主機,示例如下:
server {
  listen 80;
  server_name www.idfsoft.com;
  root "/vhosts/web";
}
listen:指定監(jiān)聽的地址和端口
listen address[:port];
listen port;
server_name NAME [...]; 后面可跟多個主機,名稱可使用正則表達式或通配符

當有多個server時,匹配順序如下:

  • 先做精確匹配檢查
  • 左側(cè)通配符匹配檢查,如*.idfsoft.com
  • 右側(cè)通配符匹配檢查,如mail.*
  • 正則表達式匹配檢查,如~ ^.*.idfsoft.com$
  • default_server
  • root path; 設(shè)置資源路徑映射,用于指明請求的URL所對應(yīng)的資源所在的文件系統(tǒng)上的起始路徑

  • alias path; 用于location配置段,定義路徑別名

  • index file; 默認主頁面
    index index.php index.html;

  • error_page code […] [=code] URI | @name 根據(jù)http響應(yīng)狀態(tài)碼來指明特用的錯誤頁面,例如 error_page 404 /404_customed.html

    [=code]:以指定的響應(yīng)碼進行響應(yīng),而不是默認的原來的響應(yīng),默認表示以新資源的響應(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可自己定義,但要一一對應(yīng),另外此處可用變量為nginx各模塊內(nèi)建變量
location區(qū)段,通過指定模式來與客戶端請求的URI相匹配
//功能:允許根據(jù)用戶請求的URI來匹配定義的各location,匹配到時,此請求將被相應(yīng)的location配置塊中的配置所處理,例如做訪問控制等功能
 
//語法:location [ 修飾符 ] pattern {......}
常用修飾符說明:
修飾符功能=精確匹配~正則表達式模式匹配,區(qū)分大小寫~*正則表達式模式匹配,不區(qū)分大小寫^~前綴匹配,類似于無修飾符的行為,也是以指定模塊開始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正則表達式@定義命名location區(qū)段,這些區(qū)段客戶端不能訪問,只可以由內(nèi)部產(chǎn)生的請求來訪問,如try_files或error_page等

查找順序和優(yōu)先級:由高到底依次為

  • 帶有=的精確匹配優(yōu)先
  • 帶有^~修飾符的,開頭匹配
  • 正則表達式按照他們在配置文件中定義的順序
    帶有或*修飾符的,如果正則表達式與URI匹配
  • 沒有修飾符的精確匹配

優(yōu)先級如下:

( location = 路徑 ) --> ( location ^~ 路徑 ) --> ( location ~ 正則 ) --> ( location ~* 正則 ) --> ( location 路徑 )
//如沒添加任何修飾符則按先后順序
[root@localhost conf]# vim nginx.conf
//添加三個訪問頁面測試
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 
//可以看到兩個目錄一樣,但是=優(yōu)先級大于沒有加=的,所以訪問的是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 
//因為區(qū)分大小寫找不到資源所以輸出的是默認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     
//因為不區(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平滑升級及nginx配置文件的文章就介紹到這了,更多相關(guān)nginx平滑升級內(nèi)容請搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!

標簽: Nginx
相關(guān)文章:
主站蜘蛛池模板: 亚洲成人在线免费视频 | 女在床上被男的插爽叫视频 | 另类欧美日韩 | 在线播放精品 | 在线观看va | 欧美一级免费大片 | 日韩欧美亚州 | 成人性视频在线三级 | 国产一国产一有一级毛片 | 亚洲一级毛片在线播放 | 免费在线观看a级毛片 | 成人欧美网站免费 | 国产在线91精品天天更新 | 人成精品视频三区二区一区 | 女人张开腿让男人操 | 一区二区三区免费视频 www | 亚洲人成网址在线播放a | 亚洲国产欧美在线人成aaa | 狼人激情网 | 中文精品久久久久国产网址 | 日本www高清免费视频观看 | 午夜性爽视频男人的天堂在线 | 九九精彩视频在线观看视频 | 最新中文字幕乱码在线 | 另类专区另类专区亚洲 | 欧美一级在线全免费 | 国产精品久久久久久久午夜片 | 亚洲欧美精品一区天堂久久 | 精品国产一区二区三区www | 国产精品欧美一区二区三区 | 国产精品久久久久久久久久一区 | 欧美中文在线 | 欧美日韩在线视频免费完整 | 性做久久久久久 | 视频二区精品中文字幕 | 亚洲影视一区二区 | 国产毛片一区二区三区精品 | 欧美一级片在线 | 一级毛片在线视频 | 国产精品国产三级国产专播 | 香蕉视频911|