1. 安装
1.1 yum
# add the yum repo:wget https://openresty.org/package/centos/openresty.repomv openresty.repo /etc/yum.repos.d/# update the yum index:yum check-update# 安装软件包yum install -y openresty===================================================================================Package Arch Version Repository Size===================================================================================Installing:openresty x86_64 1.21.4.1-1.el7 openresty 1.1 MInstalling for dependencies:openresty-openssl111 x86_64 1.1.1n-1.el7 openresty 1.6 Mopenresty-pcre x86_64 8.45-1.el7 openresty 168 kopenresty-zlib x86_64 1.2.12-1.el7 openresty 55 kTransaction Summary===================================================================================Install 1 Package (+3 Dependent packages)(1/4): openresty-openssl111-1.1.1n-1.el7.x86_64.rpm | 1.6 MB 00:00:28(2/4): openresty-pcre-8.45-1.el7.x86_64.rpm | 168 kB 00:00:01(3/4): openresty-zlib-1.2.12-1.el7.x86_64.rpm | 55 kB 00:00:00(4/4): openresty-1.21.4.1-1.el7.x86_64.rpm | 1.1 MB 00:00:43# 命令行工具 restyyum install -y openresty-resty
1.2 源码
# 依赖库yum install -y pcre-devel openssl-devel gcc curl# 解压tar -xzvf openresty-VERSION.tar.gzcd openresty-VERSION/./configure --help# 编译、安装./configure --prefix=/usr/local/openresty --sbin-path=/usr/sbin/openresty \--with-cc-opt='-DNGX_LUA_ABORT_AT_PANIC -I/usr/local/openresty/zlib/include -I/usr/local/openresty/pcre/include -I/usr/local/openresty/openssl111/include' \--with-ld-opt='-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-http_ssl_module# 输出Configuration summary+ using threads+ using system PCRE library+ using system OpenSSL library+ using system zlib librarynginx path prefix: "/usr/local/openresty/nginx"nginx binary file: "/usr/local/openresty/nginx/sbin/nginx"nginx modules path: "/usr/local/openresty/nginx/modules"nginx configuration prefix: "/usr/local/openresty/nginx/conf"nginx configuration file: "/usr/local/openresty/nginx/conf/nginx.conf"nginx pid file: "/usr/local/openresty/nginx/logs/nginx.pid"nginx error log file: "/usr/local/openresty/nginx/logs/error.log"nginx http access log file: "/usr/local/openresty/nginx/logs/access.log"nginx http client request body temporary files: "client_body_temp"nginx http proxy temporary files: "proxy_temp"nginx http fastcgi temporary files: "fastcgi_temp"nginx http uwsgi temporary files: "uwsgi_temp"nginx http scgi temporary files: "scgi_temp"make -j4make install
1.3 configue参数
1.3.1 内置模块
# 编译器优化--with-cc-opt=-O2# 添加luajit的的lib目录,并使用ljemalloc来管理内存,性能可提高30%--with-ld-opt='-Wl,-rpath,/usr/local/openresty/luajit/lib -ljemalloc# 监控模块--with-http_stub_status_module# 使Nginx支持http2,由于http2是趋势,所以这里加上,方便以后扩展--with-http_v2_module# SSL--with-http_ssl_module# 支持ipv6,由于ipv6是趋势,所以这里加上,方便以后扩展--with-ipv6# gzip--with-http_gzip_static_module# realip,必备模块,取用户真实ip模块--with-http_realip_module# 支持flv流媒体的各项扩展参数--with-http_flv_module# 加载pcre--with-pcre=/root/oneinstack/src/openresty-1.9.15.1/../pcre-8.38# 加载pcre模块--with-pcre-jit
1.3.2 第三方模块
# 使 nginx 可以直接读 redis 中缓存的文件内容--add-module=../redis2-nginx-module# 是一个支持 Redis 2.0 协议的 Nginx upstream 模块,# 它可以让 Nginx 以非阻塞方式直接防问远方的 Redis 服务,# 同时支持 TCP 协议和 Unix Domain Socket 模式,# 并且可以启用强大的 Redis 连接池功能--add-module=../redis2-nginx-module# nginx开发包--add-module=../ngx_devel_kit-0.3.0# 可以使Nginx输出一些简单的文字--add-module=../echo-nginx-module-0.59# 缓存有callback的数据--add-module=../xss-nginx-module-0.05# nginx的插件集合,方便扩展--add-module=../ngx_coolkit-0.2rc3# set-misc-nginx-module模块是标准的HttpRewriteModule指令的扩展,提供更多的功能,# 如URI转义与非转义、JSON引述、Hexadecimal/MD5/SHA1/Base32/Base64编码与解码、随机数等等。--add-module=../set-misc-nginx-module-0.30# NGINX模块读取HTTP POST和PUT--add-module=../form-input-nginx-module-0.12# 加密cookie--add-module=../encrypted-session-nginx-module-0.05# srcache是为location增加了透明的基于subrequest的缓存层,与其它模块配合,可实现高效缓存--add-module=../srcache-nginx-module-0.31# nginx lua模块--add-module=../ngx_lua-0.10.5# Nginx LUA API--add-module=../ngx_lua_upstream-0.05# 定义header的返回信息--add-module=../headers-more-nginx-module-0.30# Nginx添加支持数组类型的变量--add-module=../array-var-nginx-module-0.05# 使缓存支持高并发,针对一些访问量很大的页面,特别是瞬时访问量很大的系统,jetty无法支撑住请求,# 这时候可以使用ngx-memc模块与srcache-nginx-module模块将请求页面数据存放在 memcached中,可大大提升系统的并发能力--add-module=../memc-nginx-module-0.17# 它可以让 Nginx 以非阻塞方式直接防问远方的 Redis 服务,同时支持 TCP 协议和 Unix Domain Socket 模式,并且可以启用强大的 Redis 连接池功能。--add-module=../redis2-nginx-module-0.13# 让Nginx可直接查询redis中的数据,不用经过程序,大大的提高效率。--add-module=../redis-nginx-module-0.3.7# 用于处理json字符串,提升nginx处理json字符串性能--add-module=../rds-json-nginx-module-0.14# 控制输出的模块转换Resty-DBD流为csv--add-module=../rds-csv-nginx-module-0.07
2. 配置
user root;worker_processes 1;error_log logs/error.log notice;events {worker_connections 1024;}http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;upstream my_balance {#consistent_hash $request_uri;server 10.0.32.159:80;server 10.0.32.169:80;session_sticky option=indirect;#balancer_by_lua_file lua/balancer.lua;}server {listen 80;server_name localhost;location / {#root html;#index index.html index.htm;session_sticky_hide_cookie upstream=my_balance;proxy_pass http://my_balance;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}}
3. 启动
# OpenResty 符号链接ll /usr/bin/openresty/usr/bin/openresty -> /usr/local/openresty/nginx/sbin/nginx# 启动默认的 OpenResty 服务systemctl start openrestysystemctl stop openrestysystemctl restart openrestysystemctl reload openresty# 安装目录ll /usr/local/openresty/# 自己的 OpenResty 应用openresty -p /opt/my-fancy-app/
4. 负载均衡
- 无reload动态负载均衡:Consul+OpenResty 实现无reload动态负载均衡
- 动态负载均衡:业务集群 和 nginx集群 有中间Discovery的接口,通过接口获取负载信息
- 实现负载均衡、限流功能:通过lua脚本实现负载均衡 ```c local balancer = require “ngx.balancer” local upstream = require “ngx.upstream” local upstream_name = ‘my_balance’ local srvs = upstream.get_servers(upstream_name)
function is_down(server) local down = false local peers = upstream.get_primary_peers(upstream_name) for i = 1, #peers do local peer = peers[i] if server == peer.name and peer.down == true then down = true end end return down end
local remote_ip = ngx.var.remote_addr local hash = ngx.crc32_long(remote_ip) hash = (hash % 2) + 1 local backend = srvs[hash].addr local index = string.find(backend, ‘:’) local host = string.sub(backend, 1, index-1) local port = string.sub(backend, index+1) ngx.log(ngx.NOTICE, “current peer “, host, “:”, port) local ok, err = balancer.set_current_peer(host, tonumber(port)) if not ok then ngx.log(ngx.ERR, “failed to set the current peer: “, err) return ngx.exit(500) end ```
5. LuaNginxModule

