1. 安装

在centos7.5中进行。

1.1 yum

  1. # add the yum repo:
  2. wget https://openresty.org/package/centos/openresty.repo
  3. mv openresty.repo /etc/yum.repos.d/
  4. # update the yum index:
  5. yum check-update
  6. # 安装软件包
  7. yum install -y openresty
  8. ===================================================================================
  9. Package Arch Version Repository Size
  10. ===================================================================================
  11. Installing:
  12. openresty x86_64 1.21.4.1-1.el7 openresty 1.1 M
  13. Installing for dependencies:
  14. openresty-openssl111 x86_64 1.1.1n-1.el7 openresty 1.6 M
  15. openresty-pcre x86_64 8.45-1.el7 openresty 168 k
  16. openresty-zlib x86_64 1.2.12-1.el7 openresty 55 k
  17. Transaction Summary
  18. ===================================================================================
  19. Install 1 Package (+3 Dependent packages)
  20. (1/4): openresty-openssl111-1.1.1n-1.el7.x86_64.rpm | 1.6 MB 00:00:28
  21. (2/4): openresty-pcre-8.45-1.el7.x86_64.rpm | 168 kB 00:00:01
  22. (3/4): openresty-zlib-1.2.12-1.el7.x86_64.rpm | 55 kB 00:00:00
  23. (4/4): openresty-1.21.4.1-1.el7.x86_64.rpm | 1.1 MB 00:00:43
  24. # 命令行工具 resty
  25. yum install -y openresty-resty

1.2 源码

  1. # 依赖库
  2. yum install -y pcre-devel openssl-devel gcc curl
  3. # 解压
  4. tar -xzvf openresty-VERSION.tar.gz
  5. cd openresty-VERSION/
  6. ./configure --help
  7. # 编译、安装
  8. ./configure --prefix=/usr/local/openresty --sbin-path=/usr/sbin/openresty \
  9. --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' \
  10. --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' \
  11. --with-pcre-jit --with-stream --with-stream_ssl_module \
  12. --with-stream_ssl_preread_module --with-http_v2_module --without-mail_pop3_module \
  13. --without-mail_imap_module --without-mail_smtp_module --with-http_stub_status_module \
  14. --with-http_realip_module --with-http_addition_module --with-http_auth_request_module \
  15. --with-http_secure_link_module --with-http_random_index_module \
  16. --with-http_gzip_static_module --with-http_sub_module --with-http_dav_module \
  17. --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module \
  18. --with-threads --with-compat --with-http_ssl_module
  19. # 输出
  20. Configuration summary
  21. + using threads
  22. + using system PCRE library
  23. + using system OpenSSL library
  24. + using system zlib library
  25. nginx path prefix: "/usr/local/openresty/nginx"
  26. nginx binary file: "/usr/local/openresty/nginx/sbin/nginx"
  27. nginx modules path: "/usr/local/openresty/nginx/modules"
  28. nginx configuration prefix: "/usr/local/openresty/nginx/conf"
  29. nginx configuration file: "/usr/local/openresty/nginx/conf/nginx.conf"
  30. nginx pid file: "/usr/local/openresty/nginx/logs/nginx.pid"
  31. nginx error log file: "/usr/local/openresty/nginx/logs/error.log"
  32. nginx http access log file: "/usr/local/openresty/nginx/logs/access.log"
  33. nginx http client request body temporary files: "client_body_temp"
  34. nginx http proxy temporary files: "proxy_temp"
  35. nginx http fastcgi temporary files: "fastcgi_temp"
  36. nginx http uwsgi temporary files: "uwsgi_temp"
  37. nginx http scgi temporary files: "scgi_temp"
  38. make -j4
  39. make install

1.3 configue参数

1.3.1 内置模块

  1. # 编译器优化
  2. --with-cc-opt=-O2
  3. # 添加luajit的的lib目录,并使用ljemalloc来管理内存,性能可提高30%
  4. --with-ld-opt='-Wl,-rpath,/usr/local/openresty/luajit/lib -ljemalloc
  5. # 监控模块
  6. --with-http_stub_status_module
  7. # 使Nginx支持http2,由于http2是趋势,所以这里加上,方便以后扩展
  8. --with-http_v2_module
  9. # SSL
  10. --with-http_ssl_module
  11. # 支持ipv6,由于ipv6是趋势,所以这里加上,方便以后扩展
  12. --with-ipv6
  13. # gzip
  14. --with-http_gzip_static_module
  15. # realip,必备模块,取用户真实ip模块
  16. --with-http_realip_module
  17. # 支持flv流媒体的各项扩展参数
  18. --with-http_flv_module
  19. # 加载pcre
  20. --with-pcre=/root/oneinstack/src/openresty-1.9.15.1/../pcre-8.38
  21. # 加载pcre模块
  22. --with-pcre-jit

1.3.2 第三方模块

  1. # 使 nginx 可以直接读 redis 中缓存的文件内容
  2. --add-module=../redis2-nginx-module
  3. # 是一个支持 Redis 2.0 协议的 Nginx upstream 模块,
  4. # 它可以让 Nginx 以非阻塞方式直接防问远方的 Redis 服务,
  5. # 同时支持 TCP 协议和 Unix Domain Socket 模式,
  6. # 并且可以启用强大的 Redis 连接池功能
  7. --add-module=../redis2-nginx-module
  8. # nginx开发包
  9. --add-module=../ngx_devel_kit-0.3.0
  10. # 可以使Nginx输出一些简单的文字
  11. --add-module=../echo-nginx-module-0.59
  12. # 缓存有callback的数据
  13. --add-module=../xss-nginx-module-0.05
  14. # nginx的插件集合,方便扩展
  15. --add-module=../ngx_coolkit-0.2rc3
  16. # set-misc-nginx-module模块是标准的HttpRewriteModule指令的扩展,提供更多的功能,
  17. # 如URI转义与非转义、JSON引述、Hexadecimal/MD5/SHA1/Base32/Base64编码与解码、随机数等等。
  18. --add-module=../set-misc-nginx-module-0.30
  19. # NGINX模块读取HTTP POST和PUT
  20. --add-module=../form-input-nginx-module-0.12
  21. # 加密cookie
  22. --add-module=../encrypted-session-nginx-module-0.05
  23. # srcache是为location增加了透明的基于subrequest的缓存层,与其它模块配合,可实现高效缓存
  24. --add-module=../srcache-nginx-module-0.31
  25. # nginx lua模块
  26. --add-module=../ngx_lua-0.10.5
  27. # Nginx LUA API
  28. --add-module=../ngx_lua_upstream-0.05
  29. # 定义header的返回信息
  30. --add-module=../headers-more-nginx-module-0.30
  31. # Nginx添加支持数组类型的变量
  32. --add-module=../array-var-nginx-module-0.05
  33. # 使缓存支持高并发,针对一些访问量很大的页面,特别是瞬时访问量很大的系统,jetty无法支撑住请求,
  34. # 这时候可以使用ngx-memc模块与srcache-nginx-module模块将请求页面数据存放在 memcached中,可大大提升系统的并发能力
  35. --add-module=../memc-nginx-module-0.17
  36. # 它可以让 Nginx 以非阻塞方式直接防问远方的 Redis 服务,同时支持 TCP 协议和 Unix Domain Socket 模式,并且可以启用强大的 Redis 连接池功能。
  37. --add-module=../redis2-nginx-module-0.13
  38. # 让Nginx可直接查询redis中的数据,不用经过程序,大大的提高效率。
  39. --add-module=../redis-nginx-module-0.3.7
  40. # 用于处理json字符串,提升nginx处理json字符串性能
  41. --add-module=../rds-json-nginx-module-0.14
  42. # 控制输出的模块转换Resty-DBD流为csv
  43. --add-module=../rds-csv-nginx-module-0.07

2. 配置

  1. user root;
  2. worker_processes 1;
  3. error_log logs/error.log notice;
  4. events {
  5. worker_connections 1024;
  6. }
  7. http {
  8. include mime.types;
  9. default_type application/octet-stream;
  10. sendfile on;
  11. keepalive_timeout 65;
  12. upstream my_balance {
  13. #consistent_hash $request_uri;
  14. server 10.0.32.159:80;
  15. server 10.0.32.169:80;
  16. session_sticky option=indirect;
  17. #balancer_by_lua_file lua/balancer.lua;
  18. }
  19. server {
  20. listen 80;
  21. server_name localhost;
  22. location / {
  23. #root html;
  24. #index index.html index.htm;
  25. session_sticky_hide_cookie upstream=my_balance;
  26. proxy_pass http://my_balance;
  27. }
  28. #error_page 404 /404.html;
  29. # redirect server error pages to the static page /50x.html
  30. #
  31. error_page 500 502 503 504 /50x.html;
  32. location = /50x.html {
  33. root html;
  34. }
  35. }
  36. }

3. 启动

  1. # OpenResty 符号链接
  2. ll /usr/bin/openresty
  3. /usr/bin/openresty -> /usr/local/openresty/nginx/sbin/nginx
  4. # 启动默认的 OpenResty 服务
  5. systemctl start openresty
  6. systemctl stop openresty
  7. systemctl restart openresty
  8. systemctl reload openresty
  9. # 安装目录
  10. ll /usr/local/openresty/
  11. # 自己的 OpenResty 应用
  12. openresty -p /opt/my-fancy-app/

4. 负载均衡

  1. 无reload动态负载均衡:Consul+OpenResty 实现无reload动态负载均衡
  2. 动态负载均衡:业务集群 和 nginx集群 有中间Discovery的接口,通过接口获取负载信息
  3. 实现负载均衡、限流功能:通过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

openresty_phases.png

参考文档