背景
線上跑了多年的一個(gè)網(wǎng)關(guān)業(yè)務(wù),隨著部門(mén)的拆分,逐漸有了一個(gè)痛點(diǎn)。該網(wǎng)關(guān)業(yè)務(wù)主要處理app端請(qǐng)求,app端發(fā)起的請(qǐng)求,采用http協(xié)議,post方法,content-type采用application/x-www-form-urlencoded
,表單中有一個(gè)固定的字段,叫功能號(hào),即funcNo=1000100這樣,然后表單中其他業(yè)務(wù)字段就根據(jù)funcNo的不同而各有不同。
后端有一個(gè)接入層nginx,nginx將請(qǐng)求轉(zhuǎn)給一個(gè)網(wǎng)關(guān)服務(wù)進(jìn)行處理,網(wǎng)關(guān)會(huì)解析出funcNo字段。我們的funcNo字段也是有講究的,前4位(1000)是系統(tǒng)編碼,后面3位(100)是接口編號(hào)。網(wǎng)關(guān)就根據(jù)系統(tǒng)編碼轉(zhuǎn)發(fā)給不同的系統(tǒng)進(jìn)行處理。
網(wǎng)關(guān)采用的技術(shù)是早起的servlet容器,容器叫resin,我之前也沒(méi)有接觸過(guò),總之就是類(lèi)似于tomcat,不過(guò)輕量一些。
說(shuō)起來(lái),這個(gè)網(wǎng)關(guān)的職責(zé)和現(xiàn)在流行的接入層網(wǎng)關(guān),比如現(xiàn)在的spring cloud gateway等,職責(zé)也差不太多。只是早期都是配置文件那種,把后端的各個(gè)系統(tǒng)全都先配置好;而不像現(xiàn)在的網(wǎng)關(guān),會(huì)采用動(dòng)態(tài)配置。
現(xiàn)在有啥問(wèn)題呢?每次新上一個(gè)服務(wù),都需要在這個(gè)服務(wù)中配置新服務(wù)的系統(tǒng)編碼、新服務(wù)的后端ip+端口等,當(dāng)然了,具體細(xì)節(jié)更加復(fù)雜,它還需要配置新服務(wù)中的各個(gè)接口編號(hào),以及各個(gè)接口需要使用哪些filter等。
總之,這個(gè)服務(wù)的代碼架構(gòu)還是穩(wěn)定的,但是配置經(jīng)常需要變更。
以前,這個(gè)服務(wù)歸技術(shù)部管,后來(lái)部門(mén)拆分了,多出一個(gè)部門(mén)B,這個(gè)服務(wù)還歸技術(shù)部管;但是部門(mén)B新上服務(wù)的話,就要來(lái)找技術(shù)部對(duì)這個(gè)服務(wù)加配置,變更上線。
我們就想著,讓部門(mén)B能自己上線是最好,免得總來(lái)找我們,因此,想到的辦法就是,將這個(gè)網(wǎng)關(guān)服務(wù)再部署一套給部門(mén)B用,然后前端nginx替換為openresty,openresty根據(jù)請(qǐng)求中的funcNo,判斷系統(tǒng)編碼屬于哪個(gè)部門(mén),然后就把請(qǐng)求轉(zhuǎn)發(fā)到對(duì)應(yīng)的網(wǎng)關(guān)服務(wù)。
大體思路
1、首先,將網(wǎng)關(guān)服務(wù)做成無(wú)狀態(tài)的,目前的網(wǎng)關(guān),還使用了servlet session技術(shù),是有狀態(tài)的,這一步目前已經(jīng)做了修改,把使用servlet session的代碼,換成了jwt,有待測(cè)試環(huán)境驗(yàn)證,這個(gè)本篇先不講;
2、其次,開(kāi)發(fā)環(huán)境模擬安裝openresty,屆時(shí),使用openresty替換nginx。openresty相當(dāng)于增強(qiáng)版本的nginx,可以寫(xiě)lua代碼,解析請(qǐng)求中的funcNo字段,然后決定分發(fā)到哪個(gè)部門(mén)的網(wǎng)關(guān)服務(wù)。
本篇文章,主要是聚焦于第二個(gè)事情,即如何用openresty完美替換線上的nginx,說(shuō)起來(lái)簡(jiǎn)單,實(shí)則復(fù)雜,因?yàn)榫€上nginx運(yùn)行多年,很多配置項(xiàng),我們openresty屆時(shí)必須把線上nginx的配置也遷移過(guò)來(lái)才行,而且要保證功能都正常,不能說(shuō)之前在nginx處理得好好的,到了openresty就不行了。
一、安裝openresty
官網(wǎng)推薦的安裝方式,為什么不適用
我們首先要解決的是,線上服務(wù)器,怎么安裝openresty的問(wèn)題。
一般來(lái)說(shuō),官網(wǎng)都是讓你用yum這類(lèi)包管理器安裝,如下:
https://openresty.org/en/installation.html提到其提供了預(yù)編譯好的版本,yum安裝即可:
OpenResty? provides official pre-built packages for some of the common Linux distributions (Ubuntu, Debian, CentOS, RHEL, Fedora, OpenSUSE, Alpine, and Amazon Linux).
但是,由于線上服務(wù)器都是沒(méi)有外網(wǎng)的,你想yum安裝是不行的(要么就是yum中的版本太老了),所以,基本是采用rpm安裝。
rpm方式安裝,為什么不適用
rpm安裝的話,那么,如何獲取一個(gè)rpm包呢?
我們?cè)陂_(kāi)發(fā)環(huán)境機(jī)器上,可以利用yum來(lái)獲取rpm包。
先設(shè)置repo倉(cāng)庫(kù):
wget https://openresty.org/package/centos/openresty.repo
sudo mv openresty.repo /etc/yum.repos.d/
# update the yum index:
sudo yum check-update
然后,我們不執(zhí)行yum install openresty
,而是執(zhí)行如下命令,將rpm包下載到指定目錄:
yum install --downloadonly --downloaddir=/root/mypackage/ openresty
下載的結(jié)果如下:
[root@server172 openresty-1.21.4.1]# ls /root/mypackage/*.rpm
/root/mypackage/openresty-1.21.4.1-1.el7.x86_64.rpm
/root/mypackage/openresty-pcre-8.45-1.el7.x86_64.rpm
/root/mypackage/openresty-openssl111-1.1.1s-1.el7.x86_64.rpm
/root/mypackage/openresty-zlib-1.2.13-1.el7.x86_64.rpm
下載了rpm后,進(jìn)行rpm安裝:
cd /root/mypackage
rpm -ivh openresty-*
默認(rèn)安裝目錄即在/usr/local/openresty
下:
可以看到,其中在bin目錄下創(chuàng)建了一個(gè)鏈接文件openresty,鏈接到openresty下的nginx可執(zhí)行文件,這里也可以看出,openresty是基于nginx的增強(qiáng)。
我們執(zhí)行下openresty -V,查看這種安裝方式下的編譯選項(xiàng):
格式化之后,如下:
[root@server172 openresty]# openresty -V
nginx version: openresty/1.21.4.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.1.1n 15 Mar 2022 (running with OpenSSL 1.1.1s 1 Nov 2022)
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx
--with-cc-opt='-O2 -DNGX_LUA_ABORT_AT_PANIC -I/usr/local/openresty/zlib/include -I/usr/local/openresty/pcre/include
-I/usr/local/openresty/openssl111/include'
--add-module=../ngx_devel_kit-0.3.1
--add-module=../echo-nginx-module-0.62
--add-module=../xss-nginx-module-0.06
--add-module=../ngx_coolkit-0.2
--add-module=../set-misc-nginx-module-0.33
--add-module=../form-input-nginx-module-0.12
--add-module=../encrypted-session-nginx-module-0.09
--add-module=../srcache-nginx-module-0.32
--add-module=../ngx_lua-0.10.21
--add-module=../ngx_lua_upstream-0.07
--add-module=../headers-more-nginx-module-0.33
--add-module=../array-var-nginx-module-0.05
--add-module=../memc-nginx-module-0.19
--add-module=../redis2-nginx-module-0.15
--add-module=../redis-nginx-module-0.3.9
--add-module=../ngx_stream_lua-0.0.11
--with-ld-opt='-Wl,-rpath,/usr/local/openresty/luajit/lib
-L/usr/local/openresty/zlib/lib -L/usr/local/openresty/pcre/lib
-L/usr/local/openresty/openssl111/lib -Wl,-rpath,/usr/local/openresty/zlib/lib:/usr/local/openresty/pcre/lib:/usr/local/openresty/openssl111/lib'
--with-cc='ccache gcc -fdiagnostics-color=always'
--with-pcre-jit
--with-stream
--with-stream_ssl_module
--with-stream_ssl_preread_module
--with-http_v2_module
--without-mail_pop3_module
--without-mail_imap_module
--without-mail_smtp_module
--with-http_stub_status_module
--with-http_realip_module
--with-http_addition_module
--with-http_auth_request_module
--with-http_secure_link_module
--with-http_random_index_module
--with-http_gzip_static_module
--with-http_sub_module
--with-http_dav_module
--with-http_flv_module
--with-http_mp4_module
--with-http_gunzip_module
--with-threads
--with-compat
--with-stream
--with-http_ssl_module
但是,我們還是不能用rpm方式安裝,因?yàn)?,因?yàn)槟壳霸谂艿膎ginx,我支持是支持ipv6的,而上面rpm安裝的這個(gè)openresty,沒(méi)看到ipv6的選項(xiàng)啊。這種安裝方式,好像也沒(méi)辦法再去增加對(duì)其他模塊的支持,如ipv6.
那只能利用源碼方式來(lái)安裝了。
源碼方式安裝--獲取線上configure選項(xiàng)
要編譯,得拿到編譯選項(xiàng),等到運(yùn)維同事有空后,終于拿到了線上nginx的配置:
線上一共兩臺(tái)nginx機(jī)器,在第一臺(tái)執(zhí)行nginx -V查看配置:
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
built with OpenSSL 1.1.1d 10 Sep 2019
TLS SNI support enabled
configure arguments: --prefix=/opt/software/nginx --with-http_stub_status_module --with-stream --with-http_ssl_module --with-http_realip_module --with-ipv6 --add-module=./nginx-http-concat-master --with-openssl=/opt/software/openssl-1.1.1d --with-pcre=/opt/software/pcre-8.10
第二臺(tái)執(zhí)行后,查看配置:
nginx version: nginx/1.12.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
built with OpenSSL 1.0.2k 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/opt/software/nginx --with-http_stub_status_module --with-stream --with-http_ssl_module --with-http_realip_module --add-module=./nginx-http-concat-master --with-openssl=/opt/software/openssl-1.0.2k --with-pcre=/opt/install/pcre-8.10
兩臺(tái)的差別,主要是:
-
nginx版本不一致
-
--with-openssl中ssl路徑不同
-
第一臺(tái)多了--with-ipv6選項(xiàng)
ok,編譯選項(xiàng)拿到了,接下來(lái),就開(kāi)始準(zhǔn)備源碼編譯。
源碼方式安裝--源碼準(zhǔn)備
先下載openresty源碼包(自己在官網(wǎng)看文檔找):
wget https://openresty.org/download/openresty-1.21.4.1.tar.gz
tar -xvf openresty-1.21.4.1.tar.gz
cd openresty-1.21.4.1/
另外,由于線上nginx的如下編譯選項(xiàng)還依賴(lài)了openssl、pcre、nginx-http-concat-maste等,而當(dāng)時(shí)也沒(méi)注意要運(yùn)維同事幫我們把這些線上目錄拿下來(lái),所以,我們還要在網(wǎng)上找下這些模塊的源碼。
--add-module=./nginx-http-concat-master --with-openssl=/opt/software/openssl-1.0.2k --with-pcre=/opt/install/pcre-8.10
添加的第三方模塊源碼:
openssl:
wget --no-check-certificate https://www.openssl.org/source/openssl-1.1.1d.tar.gz
tar -zxvf openssl-1.1.1d.tar.gz
解壓后存放到:
/root/mypackage/openresty-source/openssl-1.1.1d
pcre:
wget https://sourceforge.net/projects/pcre/files/pcre/8.10/pcre-8.10.tar.gz/download --no-check-certificate
tar -zxvf pcre-8.10.tar.gz
解壓后存放到:
/root/mypackage/openresty-source/pcre-8.10
nginx-http-concat:
https://github.com/alibaba/nginx-http-concat
/root/mypackage/openresty-source/nginx-http-concat-master
解壓后存放到:
/root/mypackage/openresty-source/nginx-http-concat-master
準(zhǔn)備好上面的依賴(lài)模塊后,編輯好下面的命令:
./configure --with-http_stub_status_module --with-stream --with-http_ssl_module --with-http_realip_module --with-ipv6 --with-openssl=/root/mypackage/openresty-source/openssl-1.1.1d --with-pcre=/opt/software/pcre-8.10 --add-module=/root/mypackage/openresty-source/nginx-http-concat-master
configure完成后,發(fā)現(xiàn):
其中提示--with-ipv6
選項(xiàng)已經(jīng)過(guò)期,這個(gè)不影響,因?yàn)閚ginx的高版本已經(jīng)默認(rèn)開(kāi)啟了ipv6。
那是不是意味著直接用rpm的方式安裝也可以呢,這個(gè)呢,反正就是看rpm方式安裝出來(lái)的nginx -V的編譯選項(xiàng),到底是不是包含了線上nginx的全部選項(xiàng),如果是的話,也可以。我這邊就還是用源碼方式算了,更靈活一些。
執(zhí)行make,可能需要等待幾分鐘
make
make完成后,make install即可。
make install
完成后,即在/opt/software/openresty
目錄下安裝好了,切換到bin目錄下,里面有可執(zhí)行openresty,其實(shí)就是個(gè)指向/opt/software/openresty/nginx/sbin/nginx
的軟連接。
二、準(zhǔn)備openresty配置
大體思路
這一步的大體思路,就是找運(yùn)維同事拿到線上的nginx配置,然后根據(jù)這份配置,來(lái)修改openresty的配置。
這塊有個(gè)簡(jiǎn)單的方式,使用beyond compare進(jìn)行對(duì)比(nginx.conf),看看有哪些差異,然后配置成和線上一致,測(cè)試openresty即可。
線上陌生nginx配置解讀
with-http_stub_status_module
源文件就不看了,這里說(shuō)下看到的陌生的配置??吹絥ginx.conf里http塊下有:
#設(shè)置監(jiān)控nginx狀態(tài)URL
location /__nginxstatus
{
stub_status on;
access_log off;
}
請(qǐng)求這個(gè)接口,發(fā)現(xiàn)響應(yīng)如下:
看起來(lái),就是一些狀態(tài)信息,不知道是拿來(lái)干啥的,難道是健康監(jiān)測(cè)?
我網(wǎng)上查了下,原來(lái)這個(gè)不是nginx默認(rèn)模塊,是需要在configure的時(shí)候,使用如下模塊才有的:
./configure --with-http_stub_status_module
參考文檔如下:
http://nginx.org/en/docs/http/ngx_http_stub_status_module.html
https://blog.redis.com.cn/doc/index.html
https://blog.redis.com.cn/doc/optional/stubstatus.html
with-http_realip_module
這個(gè)模塊也在線上nginx的configure中出現(xiàn)了。
這個(gè)模塊的作用是,有時(shí)候nginx是部署在l5這類(lèi)硬件負(fù)載均衡軟件后的,nginx正常獲取客戶端ip的話,那可能就拿到的是l5的ip,要怎么獲取真實(shí)客戶端的ip呢?
一般l5這種,會(huì)在請(qǐng)求nginx時(shí),往header里加一個(gè)X-Real-IP
或者X-Forwarded-For
這樣的header,里面的value就是客戶端的真實(shí)ip。
所以,nginx啟用這個(gè)模塊后,就會(huì)從這些header里獲取客戶端ip。
http://nginx.org/en/docs/http/ngx_http_realip_module.html
https://blog.redis.com.cn/doc/optional/realip.html
nginx-http-concat
這個(gè)模塊在configure里有,是淘寶開(kāi)發(fā)的,https://github.com/alibaba/nginx-http-concat。
用途是,當(dāng)nginx作為一個(gè)靜態(tài)文件服務(wù)器時(shí),如客戶端請(qǐng)求1.js、2.js,就會(huì)是兩個(gè)請(qǐng)求和響應(yīng),當(dāng)文件多的時(shí)候,比較耗性能。
所以,nginx開(kāi)啟這個(gè)模塊后,支持如下方式訪問(wèn):http://host:80/??1.js,2.js,此時(shí),該模塊就可以一把拿到1.js、2.js,合并后返回給客戶端。
效果如下(來(lái)自于https://blog.csdn.net/qq_34556414/article/details/105892602):
# curl http://www.ttlsa.com/static/??css/ttlsa_concat.css,css/a.css
/** this is css ttlsa_concat.css **/
/** this is css a.css *
但我在配置文件中,沒(méi)找到相關(guān)的配置項(xiàng),不知道是不是線上從未使用過(guò)這個(gè)功能。
ssl支持
#開(kāi)啟SSL支持
ssl on;
ssl_certificate test.crt;
ssl_certificate_key test.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssi (Server Side Include)
ssi on;
ssi_silent_errors on;
ssi_types text/shtml;
應(yīng)該是沒(méi)在使用了,老掉牙的技術(shù)了,有興趣隨便看看:
https://blog.csdn.net/qq_33616529/article/details/79061608
https://cloud.tencent.com/developer/article/1915087
gzip
gzip on;
gzip_min_length 1K;
gzip_buffers 4 8k;
gzip_types text/* text/css application/javascript application/x-javascript application/xml;
gzip_comp_level 9;
gzip_proxied any;
gzip_vary on;
gzip_http_version 1.1;
參考文檔:
http://nginx.org/en/docs/http/ngx_http_gzip_module.html文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-469512.html
三、驗(yàn)證功能
這部分就主要包括,https驗(yàn)證、功能驗(yàn)證,具體就不展開(kāi)了,和業(yè)務(wù)強(qiáng)相關(guān)。如果一切驗(yàn)證ok,就可以準(zhǔn)備上線了。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-469512.html
到了這里,關(guān)于使用openresty替換線上nginx網(wǎng)關(guān)之openresty安裝細(xì)節(jié)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!