獲取全套nginx教程岩臣,請(qǐng)?jiān)L問(wèn)瓦力博客
什么是nginx平滑升級(jí)袱蚓?
當(dāng)官方發(fā)布一個(gè)新的穩(wěn)定版技潘,我們很有可能需要進(jìn)行版本升級(jí)技扼。新版本不僅會(huì)修復(fù)之前版本的bug,還會(huì)添加一些新的功能(當(dāng)然還得視個(gè)人情況決定是否升級(jí))粮呢。所謂的平滑升級(jí)就是不要讓用戶感受到婿失,我們?cè)?br> 升級(jí)nginx,舊版本的nginx還在正常運(yùn)行,當(dāng)我們更新nginx版后啄寡,切換到新版本豪硅,這個(gè)過(guò)程不會(huì)中斷服務(wù),讓用戶感受不到服務(wù)卡頓这难。
平滑升級(jí)和添加模塊是一樣舟误,升級(jí)需要下載最新的nginx包,而添加模塊下載的當(dāng)前版本的nginx包姻乓。小菜這篇博客是基于之前nginx的安裝 卸載(3){:target="_blank"}
來(lái)進(jìn)行嵌溢,所以小伙伴們最好還是保持一致。升級(jí)是不需要卸載nginx的蹋岩。
小菜是阿里云服務(wù)器赖草,操作系統(tǒng)centos 7
1.安裝編譯源碼環(huán)境
由于我們下載的是源碼包,所以要對(duì)nginx進(jìn)行編譯剪个,那么編譯環(huán)境就必不可少秧骑。
安裝gcc
yum install gcc
安裝PCRE
yum install pcre pcre-devel
安裝zlib(zlib庫(kù)提供了開發(fā)人員的壓縮算法,在Nginx的各種模塊中需要使用gzip壓縮扣囊。如同安裝PCRE一樣乎折,同樣需要安裝庫(kù)和它的源代碼:zlib和zlib-devel)
yum install zlib zlib-devel
安裝OpenSSL(在Nginx中,如果服務(wù)器提供安全網(wǎng)頁(yè)時(shí)則會(huì)用到OpenSSL庫(kù)侵歇,我們需要安裝庫(kù)文件和它的開發(fā)安裝包 openssl和 openssl-devel)
yum install openssl openssl-devel
2.下載nginx包
下載nginx包需要去nginx 官方網(wǎng)站{:target="_blank"}骂澄,如果升級(jí)就找最新穩(wěn)定版本的包,如果添加模塊就找當(dāng)自己當(dāng)前版本
一樣的包惕虑。找到之后坟冲,復(fù)制下載鏈接地址,小菜找的是當(dāng)前最穩(wěn)定版本https://nginx.org/download/nginx-1.14.2.tar.gz
3.添加模塊
nginx調(diào)試是很麻煩的溃蔫,所以小菜就添加一個(gè)echo-nginx-module{:target="_blank"}模塊健提,它的版本
最新版本{:target="_blank"},找到自己要下載包伟叛,復(fù)制下載鏈接地址私痹,小菜找到最新包https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz
4.下載
我們將剛才找的包地址下載下來(lái)。下載到/opt/download
/opt
|-app
|-download
|-backup
進(jìn)入download
文件夾
cd /opt/download
下載nginx包
wget https://nginx.org/download/nginx-1.14.2.tar.gz
解壓nginx包
tar -xvf nginx-1.14.2.tar.gz
下載echo-nginx-module模塊
wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz
解壓echo-nginx-module包
tar -xvf v0.61.tar.gz
5.編譯nginx源碼
為了和之前nginx版本的參數(shù)保持一致,我們先查看之前的nginx編譯參數(shù)
nginx -V
輸出:
--prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
進(jìn)入解壓好nginx-1.14.2
文件夾
cd nginx-1.14.2
將echo-nginx-module添加到編譯參數(shù)中去紊遵,添加到最后
--prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/opt/download/echo-nginx-module-0.61
編譯
./configure 編譯參數(shù)
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/opt/download/echo-nginx-module-0.61
安裝
make && make install
檢測(cè)是否安裝成功
nginx -v
輸出nginx version: nginx/1.14.2
就算安裝成功了
6.檢測(cè)echo-nginx-module
前面幾步我們將echo-nginx-module模塊編譯到nginx雹锣,下面檢測(cè)echo-nginx-module
/etc/nginx/conf.d
|-echo.conf
echo.conf
server {
listen 9001;
server_name localhost;
location / {
default_type text/html;
echo "<h1>歡迎來(lái)到瓦力博客</h1>";
}
}
重啟nginx
nginx -s reload -c /etc/nginx/nginx.conf
檢測(cè)9001端口,看9001端口是否監(jiān)聽
ss -luntp | grep nginx
在頁(yè)面上輸入http://walidream.com:9001
癞蚕,輸出中文可能會(huì)有亂碼,出現(xiàn)亂碼需要將編碼改為utf-8
7.echo調(diào)試
我們有了echo模塊辉哥,在調(diào)試nginx時(shí)就變得容易多了桦山。
輸出字符串
echo "hello";
輸入變量
echo $remote_addr;