Nginx中平滑升级及httpd段配置
文章目录
- Nginx中平滑升级及httpd段配置
- 平滑升级
- **nginx 平滑升级**
- http段中server
- **nginx 错误页面配置**
- location 段
平滑升级
随着nginx越来越流行,并且nginx的优势也越来越明显,也使得nginx的版本迭代也越来越快,而随之带来对nginx升级的工作也是难所避免的
nginx 方便地帮助我们实现了平滑升级。其原理简单概括,就是:
- (1)在不停掉老进程的情况下,启动新进程。
- (2)老进程负责处理仍然没有处理完的请求,但不再接受处理请求。
- (3)新进程接受新请求。
- (4)老进程处理完所有请求,关闭所有连接后,停止。 这样就很方便地实现了平滑升级。一般有两种情况下需要升级 nginx,一种是确实要升级 nginx 的版本,另一种是要为 nginx 添加新的模块
nginx 平滑升级
#当前ngixn版本和即将更新的nginx源码包
[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.22.0.tar.gz to.sh
[root@localhost ~]# nginx -v
nginx version: nginx/1.20.2
#解压
[root@localhost ~]# tar -xf nginx-1.22.0.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.22.0 nginx-1.22.0.tar.gz to.sh
[root@localhost ~]# unzip nginx_module_echo-master.zip
#没有unzip就yum装unzip
[root@localhost ~]# 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
#进入解压后目录并查看之前编译参数
[root@localhost ~]# cd nginx-1.22.0/
[root@localhost 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
#开始编译,并且在原先的基础上加入需要新加入的模块
[root@localhost 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,只升级,不覆盖配置文件
[root@localhost nginx-1.22.0]# make
#备份老版本的nginx的二进制文件,期间nginx不会停止服务
[root@localhost nginx-1.22.0]# ls
CHANGES CHANGES.ru LICENSE Makefile README auto conf configure contrib html man objs src
[root@localhost nginx-1.22.0]# cd objs/
[root@localhost objs]# ls
Makefile autoconf.err nginx.8 ngx_auto_headers.h ngx_modules.o
addon nginx ngx_auto_config.h ngx_modules.c src
[root@localhost objs]# mv /usr/local/nginx/sbin/nginx{,-2}
[root@localhost objs]# ls /usr/local/nginx/sbin/
nginx-2
#复制新的nginx二进制文件,进入新的nginx源码包
[root@localhost objs]# ls
Makefile autoconf.err nginx.8 ngx_auto_headers.h ngx_modules.o
addon nginx ngx_auto_config.h ngx_modules.c src
[root@localhost objs]# pwd
/root/nginx-1.22.0/objs
[root@localhost objs]# cp nginx /usr/local/nginx/sbin/
[root@localhost objs]# ls /usr/local/nginx/sbin/
nginx nginx-2
#测试新版本的nginx是否正常
[root@localhost 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`
[root@localhost objs]# pkill nginx
[root@localhost 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
[root@localhost objs]# systemctl restart nginx.service
[root@localhost 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 [::]:*
[root@localhost objs]# nginx -v
nginx version: nginx/1.22.0
[root@localhost 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
#上面是演示,大可直接把所有命令都结合在一起用;间隔
例如在完成编译后升级版本:
[root@localhost 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 ";
}
}