OpenResty,也被稱爲「ngx_openresty」,是一個基於 Nginx 與 Lua 的高性能 Web 平臺,其內部集成了大量精良的 Lua 庫、第三方的Nginx模塊和大部分系統依賴包,用於方便地搭建可以處理超高併發、擴展性極高的動態 Web 應用、Web 服務和動態網關。OpenResty不是Nginx的分支,它只是一個軟件包。html
OpenResty容許開發人員使用lua編程語言構建現有的Nginx的C模塊,支持高流量的應用程序。linux
OpenResty官網:https://openresty.org/cn/download.html nginx
一、安裝OpenResty所需依賴的包c++
yum install -y readline-devel pcre-devel openssl-develweb
[root@localhost src]# yum install -y readline-devel pcre-devel openssl-devel perl gcc gcc-c++ libreadline-dev libncurses5-dev libpcre3-dev libssl-dev
二、編譯安裝OpenRestyredis
[root@localhost src]# tar zxvf openresty-1.11.2.5.tar.gz [root@localhost src]# cd openresty-1.11.2.5 [root@localhost openresty-1.11.2.5]# groupadd www [root@localhost openresty-1.11.2.5]# useradd -s /sbin/nologin -M -g www www [root@localhost openresty-1.11.2.5]# ./configure --prefix=/app/OpenResty \ >--user=www \ >--group=www \ >--with-luajit \ >--without-http_redis2_module \ >--with-http_iconv_module \ >--with-http_realip_module \ #獲取用戶真實ip模塊 >--with-pcre \ #Perl兼容的達式模塊 >--with-luajit \ #集成luajit模塊 >--add-module=./bundle/ngx_cache_purge-2.3/ \ #添加自定義的模塊 >--add-module=./bundle/nginx_upstream_check_module-0.3.0/ \ >-j2 #支持多核 make 工做的特性, [root@localhost openresty-1.11.2.5]# gmake && gmake install [root@localhost openresty-1.11.2.5]# cd /app/OpenResty/nginx/sbin/ [root@localhost sbin]# ls nginx [root@localhost sbin]# ./nginx -V nginx version: openresty/1.11.2.5 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS SNI support enabled configure arguments: --prefix=/app/OpenResty/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.0 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.05 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.31 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.06 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.10 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.32 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.18 --add-module=../redis2-nginx-module-0.14 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.14 --add-module=../rds-csv-nginx-module-0.07 --with-ld-opt=-Wl,-rpath,/app/OpenResty/luajit/lib --user=www --group=www --with-http_realip_module --with-http_ssl_module [root@localhost sbin]# ./nginx -v nginx version: openresty/1.11.2.5 [root@localhost sbin]# ./nginx -t nginx: the configuration file /app/OpenResty/nginx/conf/nginx.conf syntax is ok nginx: configuration file /app/OpenResty/nginx/conf/nginx.conf test is successful [root@localhost sbin]# ./nginx -t -c /app/OpenResty/nginx/conf/nginx.conf nginx: the configuration file /app/OpenResty/nginx/conf/nginx.conf syntax is ok nginx: configuration file /app/OpenResty/nginx/conf/nginx.conf test is successful [root@localhost sbin]# ./nginx [root@localhost sbin]# ps -ef|grep -i nginx root 18014 1 0 10:57 ? 00:00:00 nginx: master process ./nginx www 18015 18014 0 10:57 ? 00:00:00 nginx: worker process root 18017 2603 0 10:57 pts/0 00:00:00 grep -i nginx [root@localhost sbin]#
-v:顯示 nginx 版本號。編程
-V:顯示 nginx 的版本號以及編譯環境信息以及編譯時的參數。json
-c:指定了配置文件的路徑,若是不加'-c"參數, nginx,會默認加載其安裝目錄中conf子目錄中的nginx.conf文件。vim
-t:測試配置文件是否正確,在運行時須要從新加載配置的時候,此命令很是重要,用來檢測所修改的配置文件是否有語法錯誤後端
bundle目錄裏存放着nginx核心和不少第三方模塊,好比有咱們須要的Lua和LuaJIT。
三、配置服務
[root@localhost nginx]# cat /etc/init.d/nginx #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /app/nginx/conf/nginx.conf # pidfile: /app/nginx/logs/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 NGINX_PATH="/app/OpenResty/nginx" nginx="$NGINX_PATH/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="$NGINX_PATH/conf/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` if [ -z "`grep $user /etc/passwd`" ]; then useradd -M -s /bin/nologin $user fi options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { #configtest || return $? stop sleep 1 start } reload() { #configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac [root@localhost nginx]# chkconfig --add nginx [root@localhost nginx]# chkconfig nginx on [root@localhost nginx]# service nginx restart Stopping nginx: [ OK ] Starting nginx: [ OK ] [root@localhost nginx]#
新增模塊
查看原來的參數
[root@localhost sbin]# pwd /app/OpenResty2/nginx/sbin [root@localhost sbin]# ./nginx -V nginx version: web/999.999.9.1 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/app/OpenResty2/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.0 --add-module=../iconv-nginx-module-0.14 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.05 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.31 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.07 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.11 --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.18 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.15 --add-module=../rds-csv-nginx-module-0.08 --add-module=../ngx_stream_lua-0.0.3 --with-ld-opt=-Wl,-rpath,/app/OpenResty2/luajit/lib --user=www --group=www --with-http_realip_module --with-pcre --add-module=/app/soft/naxsi-master/naxsi_src --with-http_stub_status_module --with-http_ssl_module --with-stream --with-stream_ssl_module [root@localhost sbin]#
--prefix=/app/OpenResty2/和-user=www --group=www及後面的參數都是以前編譯的參數,再編譯除了須要加上這些和新的模塊,還須要添加--with-luajit參數,因爲再次編譯時沒有生成動態連接庫,須要手動連接。否則編譯完後是不能使用,提示libluajit-5.1.so.2找不到:
[root@localhost sbin]# ./nginx -V ./nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory [root@localhost sbin]# ldd /app/OpenResty2/nginx/sbin/nginx linux-vdso.so.1 => (0x00007ffef35e3000) libdl.so.2 => /lib64/libdl.so.2 (0x00007f0230ca1000) libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f0230a84000) libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007f023084d000) libluajit-5.1.so.2 => not fund libm.so.6 => /lib64/libm.so.6 (0x00007f02302d0000) libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f023006e000) libssl.so.10 => /lib64/libssl.so.10 (0x00007f022fdfc000) libcrypto.so.10 => /lib64/libcrypto.so.10 (0x00007f022f99a000) libz.so.1 => /lib64/libz.so.1 (0x00007f022f784000) libc.so.6 => /lib64/libc.so.6 (0x00007f022f3c3000) /lib64/ld-linux-x86-64.so.2 (0x00007f0230ead000) libfreebl3.so => /lib64/libfreebl3.so (0x00007f022f1bf000) libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f022efa9000) libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x00007f022ed5c000) libkrb5.so.3 => /lib64/libkrb5.so.3 (0x00007f022ea73000) libcom_err.so.2 => /lib64/libcom_err.so.2 (0x00007f022e86f000) libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x00007f022e63c000) libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x00007f022e42d000) libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x00007f022e229000) libresolv.so.2 => /lib64/libresolv.so.2 (0x00007f022e00f000) libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f022dde7000) [root@localhost sbin]#
添加新模塊nginx-sticky-module,從新編譯,編譯時候須要注意,上面查看編譯參數時顯示的是/app/OpenResty2/ngin,再次編譯是使用的路徑應該是/app/OpenResty2/,否則後面啓動會報/app/OpenResty2/nginx/nginx/logs/error.log、/app/OpenResty2/nginx/nginx/client_body_tem找不到。
[root@localhost soft]# wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/08a395c66e42.zip [root@localhost soft]# unzip 08a395c66e42.zip [root@localhost soft]# mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42 nginx-sticky-module-ng [root@localhost soft]# cd cd openresty-1.13.6.1 [root@localhost openresty-1.13.6.1]# ./configure --prefix=/app/OpenResty2/ --user=www --group=www --with-http_realip_module --with-pcre --add-module=/app/soft/naxsi-master/naxsi_src --with-http_stub_status_module --with-http_ssl_module --with-stream --with-stream_ssl_module --add-module=/app/soft/nginx-sticky-module-ng/ --with-luajit --with-http_iconv_module -j2 [root@localhost openresty-1.13.6.1]# make [root@localhost openresty-1.13.6.1]# cp /app/OpenResty2/nginx/sbin/nginx{,.old} [root@localhost openresty-1.13.6.1]# cp build/nginx-1.13.6/objs/nginx /app/OpenResty2/nginx/sbin/ [root@localhost ~]# vim /etc/ld.so.conf include ld.so.conf.d/*.conf /app/OpenResty2/luajit/lib/ #openresty自動的lunjit庫,安裝包自帶 [root@localhost ~]# ldconfig [root@localhost ~]# ldd /app/OpenResty2/nginx/sbin/nginx linux-vdso.so.1 => (0x00007fff5a1ee000) libdl.so.2 => /lib64/libdl.so.2 (0x00007fd42170a000) libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fd4214ed000) libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007fd4212b6000) libluajit-5.1.so.2 => /app/OpenResty2/luajit/lib/libluajit-5.1.so.2 (0x00007fd42103c000) libm.so.6 => /lib64/libm.so.6 (0x00007fd420d39000) libpcre.so.1 => /lib64/libpcre.so.1 (0x00007fd420ad7000) libssl.so.10 => /lib64/libssl.so.10 (0x00007fd420865000) libcrypto.so.10 => /lib64/libcrypto.so.10 (0x00007fd420403000) libz.so.1 => /lib64/libz.so.1 (0x00007fd4201ed000) libc.so.6 => /lib64/libc.so.6 (0x00007fd41fe2c000) /lib64/ld-linux-x86-64.so.2 (0x00007fd421916000) libfreebl3.so => /lib64/libfreebl3.so (0x00007fd41fc28000) libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fd41fa12000) libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x00007fd41f7c5000) libkrb5.so.3 => /lib64/libkrb5.so.3 (0x00007fd41f4dc000) libcom_err.so.2 => /lib64/libcom_err.so.2 (0x00007fd41f2d8000) libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x00007fd41f0a5000) libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x00007fd41ee96000) libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x00007fd41ec92000) libresolv.so.2 => /lib64/libresolv.so.2 (0x00007fd41ea78000) libselinux.so.1 => /lib64/libselinux.so.1 (0x00007fd41e850000) [root@localhost openresty-1.13.6.1]# vim /app/OpenResty2/nginx/conf/nginx.conf upstream 10.15.97.136 { sticky; #sticky expires=1h domain=web.com path=/; server 10.15.97.133:443 weight=5 max_fails=3 fail_timeout=30s; server 10.15.97.132:443 weight=5 max_fails=3 fail_timeout=30s; } [root@localhost openresty-1.13.6.1]# systemctl start nginx
nginx-sticky-module這個第三方模塊能夠基於cookie實現會話保持,經過分發和識別cookie,來使同一個客戶端的請求落在同一臺服務器上,防止session丟失,默認標識名爲route。sticky模塊依賴openssl openssl-devel。
cookie負載均衡相比iphash來比其中一個特色比較明顯:內網nat用戶的均衡。而iphash沒法作到。sticky模塊不能與ip_hash同時使用
cookie須要瀏覽器支持(如Android客戶端發送請求時,通常不會帶上全部的cookie),若是客戶端禁用cookie,則cookie不會生效,且有時候會泄露數據
Sticky工做原理:
a)客戶端首次發起訪問請求,nginx接收後,發現請求頭沒有cookie,則以輪詢方式將請求分發給後端服務器;同一客戶端的請求,有可能落在不一樣的後端服務器上,
若是客戶端啓動時同時發起多個請求。因爲這些請求都沒帶cookie,因此服務器會隨機選擇後端服務器,返回不一樣的cookie。當這些請求中的最後一個請求返回時,客戶端的cookie纔會穩定下來,值以最後返回的cookie爲準。
b)後端服務器處理完請求,將響應數據返回給nginx。
c)此時nginx生成帶route的cookie,返回給客戶端。route的值與後端服務器對應,多是明文,也多是md五、sha1等Hash值
d)客戶端接收請求,並保存帶route的cookie。
e)當客戶端下一次發送請求時,會帶上route,nginx根據接收到的cookie中的route值,轉發給對應的後端服務器。
sticky參數:
sticky [name=route] [domain=.foo.bar] [path=/] [expires=1h] [hash=index|md5|sha1] [no_fallback] [secure] [httponly];
[name=route] 設置用來記錄會話的cookie名稱
[domain=.foo.bar] 設置cookie做用的域名
[path=/] 設置cookie做用的URL路徑,默認根目錄
[expires=1h] 設置cookie的生存期,默認不設置,瀏覽器關閉即失效,須要是大於1秒的值
[hash=index|md5|sha1] 設置cookie中服務器的標識是用明文仍是使用md5值,默認使用md5
[no_fallback] 設置該項,當sticky的後端機器掛了之後,nginx返回502 (Bad Gateway or Proxy Error) ,而不轉發到其餘服務器,不建議設置
[secure] 設置啓用安全的cookie,須要HTTPS支持
[httponly] 容許cookie不經過JS泄漏,沒用過
實驗發現,當宕掉當前訪問的後臺主機後,再次訪問不會跳轉到其餘主機,生產中這種是不可接受的。
nginx升級
一、查看當前的版本號
[root@Super sbin]# ./nginx -V nginx version: openresty/1.11.2.5 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/opt/openresty//nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.0 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.05 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.31 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.06 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.10 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.32 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.18 --add-module=../redis2-nginx-module-0.14 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.14 --add-module=../rds-csv-nginx-module-0.07 --with-ld-opt=-Wl,-rpath,/opt/openresty/luajit/lib --add-module=/opt/soft/naxsi-master/naxsi_src --with-http_ssl_module --with-http_stub_status_module [root@Super sbin]#
二、下載要升級的版本、從新編譯
[root@Super soft]# wget [root@Super soft]# tar -zxvf openresty-1.13.6.2.tar.gz [root@Super soft]# cd openresty-1.13.6.2 [root@Super openresty-1.13.6.2]# ./configure --prefix=/opt/openresty/ --add-module=/opt/soft/naxsi-master/naxsi_src --with-http_ssl_module --with-http_stub_status_module --with-luajit [root@Super openresty-1.13.6.2]# gmake
此步切記不要make install
三、備份老的nginx文件、覆蓋最新的文件
[root@Super openresty-1.13.6.2]# mv /opt/openresty/nginx/sbin/nginx{,.20180907bak} [root@Super openresty-1.13.6.2]# cp build/nginx-1.13.6/objs/nginx /opt/openresty/nginx/sbin/ [root@Super openresty-1.13.6.2]# /opt/openresty/nginx/sbin/nginx -t -c /opt/openresty/nginx/conf/nginx.conf nginx: the configuration file /opt/openresty/nginx/conf/nginx.conf syntax is ok nginx: configuration file /opt/openresty/nginx/conf/nginx.conf test is successful [root@Super openresty-1.13.6.2]#
四、使用make upgrade替換老的nginx進程
[root@Super openresty-1.13.6.2]# make upgrade
此步在要升級的新版本中執行
[root@Super openresty-1.13.6.2]# /opt/openresty/nginx/sbin/nginx -V nginx version: openresty/1.13.6.2 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/opt/openresty//nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.0 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.06 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.32 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.08 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.13 --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.7 --add-module=../rds-json-nginx-module-0.15 --add-module=../rds-csv-nginx-module-0.09 --add-module=../ngx_stream_lua-0.0.5 --with-ld-opt=-Wl,-rpath,/opt/openresty/luajit/lib --add-module=/opt/soft/naxsi-master/naxsi_src --with-http_ssl_module --with-http_stub_status_module --with-stream