OpenResty 1.17.8.2 使用

一、介绍

简介

OpenResty又被称为ngx_openresty,基于nginx核心和许多第三方模块组成,默认继承了Lua开发环境,使得nginx可以做为一个web服务使用。而且OpenResty提供了大量组件如Mysql、Redis、Memcached等等,使在Nginx上开发Web应用更方便更简单。目前在京东如实时价格、秒杀、动态服务、单品页、列表页等都在使用Nginx+Lua架构,其他公司如淘宝、去哪儿网等。

原理

Nginx采用master-worker模型,也就是一个master进程管理多个worker进程,基本的时间处理都放在worker进程中,master进程负责全局初始化以及对worker进程的管理,OpenResty利用 nginx的非阻塞IO模型,实现高性能web应用程序

二、安装openresty

安装命令:

    yum install yum-utils -y
    yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
    
    yum install openresty -y
    yum install openresty-resty -y
    
    resty -V

/usr/local/openresty/nginx/sbin/nginx -V

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.32 
--add-module=../form-input-nginx-module-0.12 
--add-module=../encrypted-session-nginx-module-0.08 
--add-module=../srcache-nginx-module-0.32 
--add-module=../ngx_lua-0.10.17 
--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=../ngx_stream_lua-0.0.8 

--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-stream_ssl_preread_module 
--with-http_ssl_module

说明:

内置模块 --with-

  --with-cc-opt=-O2
      #编译器优化

  --with-ld-opt='-Wl,-rpath,/usr/local/openresty/luajit/lib -ljemalloc
  #添加luajit的的lib目录,并使用ljemalloc来管理内存,性能可提高30%

  --user=www --group=www
  #用户,组,不解释

  --with-http_stub_status_module
  #监控模块,不解释

  --with-http_v2_module
  #使Nginx支持http2,由于http2是趋势,所以这里加上,方便以后扩展。

  --with-http_ssl_module
  #SSL不解释

  --with-ipv6
  #支持ipv6,由于ipv6是趋势,所以这里加上,方便以后扩展

  --with-http_gzip_static_module
  #gzip不解释

  --with-http_realip_module
  #realip,必备模块,取用户真实ip模块

  --with-http_flv_module
  #支持flv流媒体的各项扩展参数

  --with-pcre=/root/oneinstack/src/openresty-1.9.15.1/../pcre-8.38
  #加载pcre,不解释

  --with-pcre-jit
  #加载pcre模块

 

第三方模块 --add-module

  --add-module=../redis2-nginx-module
  #使 nginx 可以直接读 redis 中缓存的文件内容

  --add-module=../redis2-nginx-module

  #是一个支持 Redis 2.0 协议的 Nginx upstream 模块,它可以让 Nginx 以非阻塞方式直接防问远方的 Redis 服务,同时支持 TCP 协议和 Unix Domain Socket 模式,并且可以启用强大的   Redis 连接池功能

  --add-module=../ngx_devel_kit-0.3.0
  #nginx开发包

  --add-module=../echo-nginx-module-0.59
  #可以使Nginx输出一些简单的文字

  --add-module=../xss-nginx-module-0.05
  #缓存有callback的数据

  --add-module=../ngx_coolkit-0.2rc3
  #nginx的插件集合,方便扩展

  --add-module=../set-misc-nginx-module-0.30
  #set-misc-nginx-module模块是标准的HttpRewriteModule指令的扩展,提供更多的功能,
  #如URI转义与非转义、JSON引述、Hexadecimal/MD5/SHA1/Base32/Base64编码与解码、随机数等等。

  --add-module=../form-input-nginx-module-0.12
  #NGINX模块读取HTTP POST和PUT

  --add-module=../encrypted-session-nginx-module-0.05
  #加密cookie

  --add-module=../srcache-nginx-module-0.31
  #srcache是为location增加了透明的基于subrequest的缓存层,与其它模块配合,可实现高效缓存

  --add-module=../ngx_lua-0.10.5
  #nginx lua模块,不解释

  --add-module=../ngx_lua_upstream-0.05
  #Nginx LUA API

  --add-module=../headers-more-nginx-module-0.30
  #定义header的返回信息

  --add-module=../array-var-nginx-module-0.05
  #Nginx添加支持数组类型的变量

  --add-module=../memc-nginx-module-0.17
  #使缓存支持高并发,针对一些访问量很大的页面,特别是瞬时访问量很大的系统,jetty无法支撑住请求,这时候可以使用ngx-memc模块与srcache-nginx-module模块将请求页面数据
  存放在 memcached中,可大大提升系统的并发能力

  --add-module=../redis2-nginx-module-0.13
  #它可以让 Nginx 以非阻塞方式直接防问远方的 Redis 服务,同时支持 TCP 协议和 Unix Domain Socket 模式,并且可以启用强大的 Redis 连接池功能。

  --add-module=../redis-nginx-module-0.3.7
  #让Nginx可直接查询redis中的数据,不用经过程序,大大的提高效率。

  --add-module=../rds-json-nginx-module-0.14
  #用于处理json字符串,提升nginx处理json字符串性能

  --add-module=../rds-csv-nginx-module-0.07
  #控制输出的模块转换Resty-DBD流为csv

openresty没有的模块

  --add-module=../nginx_upstream_check_module-0.3.0
  #该模块用于upstream健康检查

  #下载地址:https://codeload.github.com/yaoweibin/nginx_upstream_check_module/tar.gz/v0.3.0

  --add-module=../ngx_cache_purge-2.3
  #该模块用于清理nginx缓存

  #下载地址:https://codeload.github.com/FRiCKLE/ngx_cache_purge/tar.gz/2.3

lua 使用踩坑

一定要注意使用 lua 的版本,版本不同,可能有的函数就没有了,比如 bit 中的 math.mod 函数,5.1 之后就改为 fmod() 了,但是当时引入的还是之前的版本,就报错了.

http://www.lua.org/manual/5.1/manual.html#7.2

1.字符串拼接(不是 + 而是 “…”)

做过java 的都知道,java 中字符串的拼接使用 + ,但是在 lua 里千万要注意,不是"+", “+” 在 Lua 里只表示算术运算,真正的拼接字符串是 “…”

local str = "Hello," + 'bamboo'
ngx.say(str)

看运行结果:

 [error]  lua entry thread aborted: runtime error: /app/lua_pro/lua_exercise/mobile_login_exercise/ng_test.lua:182: attempt to perform arithmetic on a string value   (在 string 类型的数据上执行算术运算)
stack traceback:
coroutine 0:

2.方法的调用 “.” 和 “:”

这个也是很容易出错的地方,因为方法调用的时候用了".",而没有用 “:”,出了好多次问题,宝宝心里苦呀,有的时候用 “.”,有的时候用 “:”

带点号的函数声明: “.”,默认是没有 self 参数的

冒号声明的函数,默认有一个隐藏的参数 self,so 在你调用的时候要用 “:”,而不是 “.”

我的经验是,用冒号声明,就用冒号调用;用点号声明,就用点号调用.保持一致,一般情况下喜欢用冒号声明,因为有一个 self 可以很方便的使用,上面给的链接中也涉及到了self 的用处。

3. nil 这个值

一个变量,你没有赋默认值,他就是 nil,所以一般情况下最好根据类型赋个默认值

比如 table
local tmp_tab = {}

string
local str = ''

number 
local n = 0

boolean
local flag = true

and so on .....
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值