廣州網(wǎng)絡(luò)推廣有限公司滎陽seo推廣
文章目錄
- Nginx平滑升級&重定向rewrite
- nginx平滑升級
- 流程
- 環(huán)境
- 查看舊版的配置信息
- 下載新版nginx源碼包和功能模塊包
- 編譯配置新版本
- 平滑升級
- 驗(yàn)證
- 重定向rewrite
- 配置重定向準(zhǔn)發(fā)
- 訪問測試
Nginx平滑升級&重定向rewrite
nginx平滑升級
流程
平滑升級:
(升級版本、增加新功能)
1.獲取老版本的編譯信息
2.老版本備份
3.編譯新版本或者新功能(不能執(zhí)行make install)
4.手動替換新版本并重啟
5.驗(yàn)證新版本
環(huán)境
操作系統(tǒng) | 舊版本 | 新版本 | 新加功能 |
---|---|---|---|
centos-8 | nginx-1.22.1 | nginx-1.24.0 | echo-nginx-module |
部署nginx請閱讀nginx服務(wù)和LNMP架構(gòu)&部署Discuz論壇系統(tǒng)
查看舊版的配置信息
升級的同時(shí)要保留舊版的功能,所以要查看詳細(xì)的編譯信息
//查看當(dāng)前版本nginx的詳細(xì)信息,記住舊版的編譯信息
[root@wanf ~]# 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 --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@wanf ~]#
下載新版nginx源碼包和功能模塊包
Nginx官網(wǎng)
GitHub官網(wǎng)
//下載新版nginx源碼包
[root@wanf ~]# wget http://nginx.org/download/nginx-1.24.0.tar.gz -P /usr/src/
[root@wanf ~]# ls /usr/src/
debug nginx-1.22.1 nginx-1.24.0.tar.gz php-8.2.10.tar.gz
kernels nginx-1.22.1.tar.gz php-8.2.10//在GitHub下載模塊代碼
[root@wanf ~]# yum -y install git
[root@wanf ~]# git clone https://github.com/openresty/echo-nginx-module.git
[root@wanf ~]# mv echo-nginx-module/ /usr/src/
[root@wanf ~]# ls /usr/src/
debug kernels nginx-1.22.1.tar.gz php-8.2.10
echo-nginx-module nginx-1.22.1 nginx-1.24.0.tar.gz php-8.2.10.tar.gz
[root@wanf ~]#
編譯配置新版本
//備份舊版nginx主程序
[root@wanf ~]# cp /usr/local/nginx/sbin/nginx /opt/nginx-buckup-20231019
[root@wanf ~]# ls /opt/
nginx-buckup-20231019//復(fù)制舊版的編譯參數(shù),并加上新的功能模塊,進(jìn)行編譯
[root@wanf ~]# cd /usr/src/
[root@wanf src]# ls
debug kernels nginx-1.22.1.tar.gz php-8.2.10
echo-nginx-module nginx-1.22.1 nginx-1.24.0.tar.gz php-8.2.10.tar.gz
[root@wanf src]# tar -xf nginx-1.24.0.tar.gz
[root@wanf src]# cd nginx-1.24.0/
[root@wanf 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 \
--error-log-path=/var/log/nginx/error.log \
--add-module=../echo-nginx-module/
(配置過程省略)//編譯新版本(只執(zhí)行make,不執(zhí)行make install)
[root@wanf nginx-1.24.0]# make
(編譯過程省略)
平滑升級
先關(guān)閉舊版nginx服務(wù),然后把新編譯的nginx主程序替換掉舊版,再啟動服務(wù)即可
//新版nginx主程序文件,/opjs/nginx就是主程序
[root@wanf nginx-1.24.0]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[root@wanf 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@wanf nginx-1.24.0]# //平滑升級。停止服務(wù),替換文件,啟動服務(wù)要一步執(zhí)行完。否則可能導(dǎo)致升級失敗。
[root@wanf nginx-1.24.0]# cd objs/
[root@wanf objs]# systemctl stop nginx;\cp nginx /usr/local/nginx/sbin/nginx;systemctl start nginx
驗(yàn)證
//端口號80已經(jīng)啟動
[root@wanf objs]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
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 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:* //已經(jīng)升級為新版nginx-1.24.0
[root@wanf objs]# nginx -v
nginx version: nginx/1.24.0
[root@wanf objs]# //測試新添加的echo功能
//修改配置文件
[root@wanf ~]# vim /usr/local/nginx/conf/nginx.conf
......
location /status {echo "hallo wanf";stub_status on;allow 192.168.179.0/24;deny all;}
......//測試配置文件語法;語法沒有報(bào)錯
[root@wanf ~]# 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@wanf ~]#
升級成功
重定向rewrite
以下是拿Discuz論壇系統(tǒng)做案例
假設(shè)原來的域名是www.wanf2.com,但是公司換了新的域名為www.wanf1.com,那么用戶使用舊域名就無法訪問到論壇系統(tǒng)
LNMP架構(gòu)部署論壇系統(tǒng)請閱讀LNMP架構(gòu)&部署Discuz論壇系統(tǒng)
舊域名無法訪問
使用新域名可以訪問
配置重定向準(zhǔn)發(fā)
為了用戶體驗(yàn),需要做一個轉(zhuǎn)發(fā),讓用戶訪問舊域名的時(shí)候,會跳轉(zhuǎn)到新域名
//修改配置文件,在舊域名的虛擬主機(jī)里面添加一個轉(zhuǎn)發(fā)
[root@wanf ~]# vim /usr/local/nginx/conf/nginx.conf
......server {listen 80;server_name www.wanf2.com;location / {root html;index index.php index.html index.htm;rewrite ^/(.*)$ http://www.wanf1.com/$1 break; //添加此行,改為新域名}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}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.conf;}}
......//重啟服務(wù)
[root@wanf ~]# systemctl restart nginx.service
[root@wanf ~]# systemctl restart php-fpm.service
訪問測試
訪問舊域名,自動跳轉(zhuǎn)訪問新域名
訪問新域名,直接可以訪問