Nginx平滑升级,以及配置文件

Nginx平滑升级,以及配置文件

nginx平滑升级

所谓平滑升级,就是一种在热升级手段,在不中断服务的情况下升级软件

平滑升级流程

  1. 获取老版本的编译信息
  2. 获取新版本安装包或者功能包
  3. 配置新版本或功能,配置时加上老版本的编译信息和新版本或功能
  4. 编译,编译后不执行安装操作
  5. 备份老版本程序
  6. 停止老版本程序或进程
  7. 将新版本复制到老版本所在位置替换老版本
  8. 启动新版本
//获取新版本安装包和功能包
[root@nginx ~]# dnf -y install git
[root@nginx ~]# cd /usr/local/src/
[root@nginx src]# wget https://nginx.org/download/nginx-1.22.0.tar.gz
--2022-10-12 20:26:13--  https://nginx.org/download/nginx-1.22.0.tar.gz
Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5704::6, ...
Connecting to nginx.org (nginx.org)|3.125.197.172|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1073322 (1.0M) [application/octet-stream]
Saving to: ‘nginx-1.22.0.tar.gz’

nginx-1.22.0.tar.gz                 100%[=================================================================>]   1.02M   788KB/s    in 1.3s

2022-10-12 20:26:16 (788 KB/s) - ‘nginx-1.22.0.tar.gz’ saved [1073322/1073322]

[root@nginx src]# git clone https://gitee.com/wujunze/nginx_module_echo.git
Cloning into 'nginx_module_echo'...
remote: Enumerating objects: 80, done.
remote: Total 80 (delta 0), reused 0 (delta 0), pack-reused 80
Unpacking objects: 100% (80/80), 14.32 KiB | 637.00 KiB/s, done.
[root@nginx src]# ls
nginx-1.22.0.tar.gz  nginx_module_echo
[root@nginx src]# tar -xf nginx-1.22.0.tar.gz
[root@nginx src]#
//配置时,老版本的配置加上新加的模块,用--add-module=模块目录参数
[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
//开始编译,别安装
[root@nginx nginx-1.22.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l)
//编译后的版本查看
[root@nginx nginx-1.22.0]# objs/nginx -v
nginx version: nginx/1.22.0
//替换版本
[root@nginx nginx-1.22.0]# cp /usr/local/nginx/sbin/nginx{,-bak};pkill nginx;\cp objs/nginx /usr/local/nginx/sbin/;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
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                               [::]:*
[root@nginx nginx-1.22.0]#

nginx配置文件

主配置文件:/usr/local/nginx/conf/nginx.conf
默认启动nginx时,使用的配置文件是:安装路径/conf/nginx.conf文件
可以在启动nginx时通过-c选项来指定要读取的配置文件
nginx常见的配置文件及其作用

配置文件作用
nginx.confnginx的基本配置文件
mime.typesMIME类型关联的扩展文件
fastcgi.conf与fastcgi相关的配置
proxy.conf与proxy相关的配置
sites.conf配置nginx提供的网站,包括虚拟主机

nginx.conf配置详解

nginx.conf的内容分为以下几段:

  • main配置段:全局配置段。其中main配置段中可能包含event配置段
  • event {}:定义event模型工作特性
  • http {}:定义http协议相关的配置
    配置指令:要以分号结尾,语法格式如下:
derective value1 [value2 ...];
支持使用变量:
 
内置变量:模块会提供内建变量定义
自定义变量:set var_name value

用于调试、定位问题的配置参数

daemon {on|off};    //是否以守护进程方式运行nginx,调试时应设置为off
master_process {on|off};    //是否以master/worker模型来运行nginx,调试时可以设置为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

正常运行必备的配置参数

user USERNAME [GROUPNAME];    //指定运行worker进程的用户和组
pid /path/to/pid_file;    //指定nginx守护进程的pid文件
worker_rlimit_nofile number;    //设置所有worker进程最大可以打开的文件数,默认为1024
worker_rlimit_core size;    //指明所有worker进程所能够使用的总体的最大核心文件大小,保持默认即可

优化性能的配置参数

worker_processes n;    //启动n个worker进程,这里的n为了避免上下文切换,通常设置为cpu总核心数-1或等于总核心数
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()系统调用的次数
worker_priority number;    //指明worker进程的nice值

事件相关的配置:event{}段中的配置参数

accept_mutex {off|on};    //master调度用户请求至各worker进程时使用的负载均衡锁;on表示能让多个worker轮流地、序列化地去响应新请求
lock_file file;    //accept_mutex用到的互斥锁锁文件路径
use [epoll | rtsig | select | poll];    //指明使用的事件模型,建议让nginx自行选择
worker_connections #;    //每个进程能够接受的最大连接数

网络连接相关的配置参数

keepalive_timeout number;    //长连接的超时时长,默认为65s
keepalive_requests number;    //在一个长连接上所能够允许请求的最大资源数
keepalive_disable [msie6|safari|none];    //为指定类型的UserAgent禁用长连接
tcp_nodelay on|off;    //是否对长连接使用TCP_NODELAY选项,为了提升用户体验,通常设为on
client_header_timeout number;    //读取http请求报文首部的超时时长
client_body_timeout number;    //读取http请求报文body部分的超时时长
send_timeout number;    //发送响应报文的超时时长

nginx作为web服务器时使用的配置:http{}段的配置参数

ttp{…}:配置http相关,由ngx_http_core_module模块引入。nginx的HTTP配置主要包括四个区块,结构如下:

http {//协议级别
  include mime.types;
  default_type application/octet-stream;
  keepalive_timeout 65;
  gzip on;
  upstream {//负载均衡配置
    ...
  }
  server {//服务器级别,每个server类似于httpd中的一个<VirtualHost>
    listen 80;
    server_name localhost;
    location / {//请求级别,类似于httpd中的<Location>,用于定义URL与本地文件系统的映射关系
      root html;
      index index.html index.htm;
    }
  }
}

http{}段配置指令:

server {}:定义一个虚拟主机,示例如下:

server {
  listen 80;
  server_name www.idfsoft.com;
  root "/vhosts/web";
}

listen:指定监听的地址和端口

listen address[:port];
listen port;

server_name NAME [...]; 后面可跟多个主机,名称可使用正则表达式或通配符

当有多个server时,匹配顺序如下:

  1. 先做精确匹配检查
  2. 左侧通配符匹配检查,如*.idfsoft.com
  3. 右侧通配符匹配检查,如mail.*
  4. 正则表达式匹配检查,如~ ^.*\.idfsoft\.com$
  5. default_server

root path; 设置资源路径映射,用于指明请求的URL所对应的资源所在的文件系统上的起始路径

alias path; 用于location配置段,定义路径别名

index file; 默认主页面

index index.php index.html;
error_page code [...] [=code] URI | @name` 根据http响应状态码来指明特用的错误页面,例如 `error_page 404 /404_customed.html

[=code]:以指定的响应码进行响应,而不是默认的原来的响应,默认表示以新资源的响应码为其响应码,例如 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;

//注意:此处可用变量为nginx各模块内建变量

location区段,通过指定模式来与客户端请求的URI相匹配

//功能:允许根据用户请求的URI来匹配定义的各location,匹配到时,此请求将被相应的location配置块中的配置所处理,例如做访问控制等功能

//语法:location [ 修饰符 ] pattern {......}

常用修饰符说明:

修饰符功能
=精确匹配
~正则表达式模式匹配,区分大小写
~*正则表达式模式匹配,不区分大小写
^~前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式
@定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或error_page等
 server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

#        location / {
#            root   html;
#            index  index.html index.htm index.php;
#        }

        location / {		//优先级最低,其他匹配不到就匹这个
            echo "/";
        }
        location = /abc {	//精准匹配uri为‘/hwf’
            echo "= abc";
        }
        location ^~ /abc {	//匹配uri开头为/hwf
            echo "^abc";
        }
        location ~ ^/a {	//使用正则表达式来匹配以hw开头的uri
            echo "^/a";
        }
[root@nginx ~]# systemctl reload nginx.service 
[root@nginx ~]# 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                               [::]:*
[root@nginx ~]#

//测试

##1.这次访问的的是"/abc",它的URI就是"/abc",被"location = /abc"精确匹配到,所以说修饰符"="优先级最高,打印了"=abc"
[root@nginx ~]# curl http://192.168.159.167/abc
= abc
##注意"?"后面部分不属于URI,所以说还能被"location = /abc",精确匹配到
[root@nginx ~]# curl http://192.168.159.167/abc&adf
= abc
##2.访问这个的URI是"/abc/",所以说不能被"location = /abc"精确匹配到,但是这个URI是以"/abc"开头的,所以说被"location ^~ /abc"优先匹配到
[root@nginx ~]# curl http://192.168.159.167/abc/
^abc
##3.只要不被前两条匹配到,且以"a"开头的URI,就都会被"location ~ ^/a"匹配到
[root@nginx ~]# curl http://192.168.159.167/asa
^/a
##5.只要上面的条件都不被匹配到"/",符合条件的只有"location /"这一条
[root@nginx ~]# curl http://192.168.159.167/zwl
/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值