Nginx smooth upgrade & amp;redirect rewrite
Environment description:
Operating system | Old version | New version | New features |
---|---|---|---|
centos-8 | nginx-1.22.1 | nginx-1.24.0 | echo-nginx -module |
To deploy nginx, please read nginx service and LNMP architecture & deploy Discuz forum system
View the configuration information of the old version
[root@localhost ~]# nginx -v nginx version: nginx/1.22.1 [root@localhost ~]# # details [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 ~]#
Download the new version of nginx source code package and function module package
Nginx official website: http://nginx.org/
Github official website: https://github.com/
# Download the new version of nginx source code package [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]# # Download module code from 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]#
Compile and configure the new version
# Back up the old nginx main program [root@localhost ~]# cp /usr/local/nginx/sbin/nginx /opt/nginx-buckup-20231020 [root@localhost ~]# ls /opt/ nginx-buckup-20231020 [root@localhost ~] # Unzip the new version first [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 # Copy the compilation parameters of the old version, add the new function module, and compile [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/ (added echo module) # Compile the new version. Note that you can only execute make, not make install. [root@localhost nginx-1.24.0]# make
Smooth upgrade
First close the old version of nginx service, then replace the old version with the newly compiled nginx main program, and then start the service
# New version of nginx main program file, /opjs/nginx is the main program [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]# # For smooth upgrade, stop the nginx service, replace files, and start the service in one step. Otherwise, the upgrade may fail. [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]#
Test
# View port [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 *:* # View version [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]# # Test the newly added echo function 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 } # Check the syntax of the configuration file; no syntax error is reported [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 ~]#
Upgrade successful
Configure redirection for accurate delivery
For the sake of user experience, a forwarding is needed so that when users access the old domain name, they will jump to the new domain name
# [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/{ roothtml/; index index.php index.html index.htm; rewrite ^/(.*)$ http://www.tqlot.com/$1 break; //Add this line and change to the new domain name } location /nginx_status { echo "hallo tq"; stub_status on; access_log off; allow 192.168.136.0/24; deny all; } # Restart the service [root@localhost ~]# systemctl restart nginx.service [root@localhost ~]# systemctl restart php-fpm.service [root@localhost ~]#
Access test
Visit the old domain name and automatically redirect to the new domain name