Nginx平滑升级&重定向rewrite
环境说明:
操作系统 | 旧版本 | 新版本 | 新加功能 |
---|---|---|---|
centos-8 | nginx-1.22.1 | nginx-1.24.0 | echo-nginx-module |
部署nginx请阅读nginx服务和LNMP架构&部署Discuz论坛系统
查看旧版的配置信息
[root@localhost ~]# nginx -v
nginx version: nginx/1.22.1
[root@localhost ~]#
# 详细信息
[root@localhost ~]# nginx -V
nginx version: nginx/1.22.1
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 --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
[root@localhost ~]#
下载新版nginx源码包和功能模块包
Nginx官网:http://nginx.org/
Github官网:https://github.com/
# 下载新版nginx源码包
[root@localhost src]# wget http://nginx.org/download/nginx-1.24.0.tar.gz
[root@localhost src]# ls
debug nginx-1.22.1 nginx-1.24.0.tar.gz php-8.2.9.tar.gz
kernels nginx-1.22.1.tar.gz php-8.2.9
[root@localhost src]#
# 在Github下载模块代码
[root@localhost ~]# yum -y install git
[root@localhost ~]# cd /usr/src/
[root@localhost src]# git clone https://github.com/openresty/echo-nginx-module.git
Cloning into 'echo-nginx-module'...
fatal: unable to access 'https://github.com/openresty/echo-nginx-module.git/': OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 104
[root@localhost src]# git clone https://github.com/openresty/echo-nginx-module.git
Cloning into 'echo-nginx-module'...
remote: Enumerating objects: 3061, done.
remote: Counting objects: 100% (43/43), done.
remote: Compressing objects: 100% (31/31), done.
remote: Total 3061 (delta 21), reused 30 (delta 12), pack-reused 3018
Receiving objects: 100% (3061/3061), 1.18 MiB | 1.23 MiB/s, done.
Resolving deltas: 100% (1645/1645), done.
[root@localhost src]# ls
debug kernels nginx-1.22.1.tar.gz php-8.2.9
echo-nginx-module nginx-1.22.1 nginx-1.24.0.tar.gz php-8.2.9.tar.gz
[root@localhost src]#
编译配置新版本
# 备份旧版nginx主程序
[root@localhost ~]# cp /usr/local/nginx/sbin/nginx /opt/nginx-buckup-20231020
[root@localhost ~]# ls /opt/
nginx-buckup-20231020
[root@localhost ~]
# 先解压新版本
[root@localhost src]# tar -xf nginx-1.24.0.tar.gz
[root@localhost src]# ls
debug nginx-1.22.1 nginx-1.24.0.tar.gz
echo-nginx-module nginx-1.22.1.tar.gz php-8.2.9
kernels nginx-1.24.0 php-8.2.9.tar.gz
[root@localhost src]# cd nginx-1.2
# 复制旧版的编译参数,并加上新的功能模块,进行编译
[root@localhost src]# cd nginx-1.24.0
[root@localhost nginx-1.24.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 --add-module=../echo-nginx-module/ (加的echo模块)
# 编译新版版,注意只能执行make,不能make install
[root@localhost nginx-1.24.0]# make
平滑升级
先关闭旧版nginx服务,然后把新编译的nginx主程序替换掉旧版,再启动服务即可
# 新版nginx主程序文件,/opjs/nginx就是主程序
[root@localhost nginx-1.24.0]# ls
auto CHANGES.ru configure '--group=nginx' LICENSE man README
CHANGES conf contrib html Makefile objs src
[root@localhost nginx-1.24.0]# ls objs/
addon Makefile nginx.8 ngx_auto_headers.h ngx_modules.o
autoconf.err nginx ngx_auto_config.h ngx_modules.c src
[root@localhost nginx-1.24.0]#
# 平滑升级,停止nginx服务,替换文件,启动服务要一步执行完。否则可能导致升级失败。
[root@localhost nginx-1.24.0]# cd objs/
[root@localhost objs]# systemctl stop nginx;\cp nginx /usr/local/nginx/sbin/nginx;systemctl start nginx
[root@localhost objs]#
测试
# 查看端口
[root@localhost objs]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 511 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 2048 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
# 查看版本
[root@localhost objs]# nginx -V
nginx version: nginx/1.24.0
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 --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 --add-module=../echo-nginx-module/
[root@localhost objs]# nginx -v
nginx version: nginx/1.24.0
[root@localhost objs]#
# 测试新添加的echo功能
6 }
47
48 location /nginx_status {
49 echo "hallo tq";
50 stub_status on;
51 access_log off;
52 allow 192.168.136.0/24;
53 deny all;
54 }
# 检查配置文件语法;语法没有报错
[root@localhost ~]# 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
[root@localhost ~]#
升级成功
配置重定向准发
为了用户体验,需要做一个转发,让用户访问旧域名的时候,会跳转到新域名
# [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.tqloh.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html/;
index index.php index.html index.htm;
rewrite ^/(.*)$ http://www.tqlot.com/$1 break; //添加此行,改为新域名
}
location /nginx_status {
echo "hallo tq";
stub_status on;
access_log off;
allow 192.168.136.0/24;
deny all;
}
# 重启服务
[root@localhost ~]# systemctl restart nginx.service
[root@localhost ~]# systemctl restart php-fpm.service
[root@localhost ~]#
访问测试
访问旧域名,自动跳转访问新域名