OpenResty,也被称为“ngx_openresty”,是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方的Nginx模块和大部分系统依赖包,用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。OpenResty不是Nginx的分支,它只是一个软件包。
OpenResty允许开发人员使用lua编程语言构建现有的Nginx的C模块,支持高流量的应用程序。
OpenResty官网:https://openresty.org/cn/download.html
1、安装OpenResty所需依赖的包
yum install -y readline-devel pcre-devel openssl-devel
[root@localhost src]# yum install -y readline-devel pcre-devel openssl-devel perl gcc gcc-c++ libreadline-dev libncurses5-dev libpcre3-dev libssl-dev
2、编译安装OpenResty
[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 的版本号以及编译环境信息以及编译时的参数。
-c:指定了配置文件的路径,如果不加'-c"参数, nginx,会默认加载其安装目录中conf子目录中的nginx.conf文件。
-t:测试配置文件是否正确,在运行时需要重新加载配置的时候,此命令非常重要,用来检测所修改的配置文件是否有语法错误
bundle目录里存放着nginx核心和很多第三方模块,比如有我们需要的Lua和LuaJIT。
3、配置服务
[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的值与后端服务器对应,可能是明文,也可能是md5、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升级
1、查看当前的版本号
[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]#
2、下载要升级的版本、重新编译
[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
3、备份老的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]#
4、使用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