本文档记录openresty的学习流程,希望后续温习和为新人提供参考手册。
Nginx(“engine x”)是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP 代理服务器。
由于Nginx使用基于事件驱动的架构,能够并发处理百万级别的TCP连接,高度模块化的 设计和自由的许可证使得扩展Nginx功能的第三方模块层出不穷。因此其作为web服务 器被广泛应用到大流量的网站上,包括淘宝、腾讯、新浪、京东等访问量巨大的网站。
- 处理响应请求很快
- 高并发连接
- 低的内存消耗
- 具有很高的可靠性
- 高扩展性
- 热部署
- 自由的BSD许可协议
下载源码包,http://nginx.org/en/download.html $ tar zxvf nginx-1.10.1.tar.gz $ cd nginx-1.10.1 $ ./configure --prefix=/home/sqlfocus/Program $ make $ make install 超级权限用户的PATH中,添加nginx的执行文件路径 可能的依赖: $ sudo apt-get install libpcre3-dev
测试是否安装成功 启动nginx,$ sudo ~/Program/sbin/nginx 在浏览器打开网址,http://localhost 查看是否正常显示
location [=|~|~*|^~] /uri/ {…},其中
- =
- 精确匹配
- ^~
- 匹配URL路径,表示URL以某个常规字符串开头
- ~
- 区分大小写的正则匹配
- ~*
- 不区分大小写的正则匹配
- /
- 通用匹配,匹配任何请求
匹配顺序:
- =精确匹配
- ^~路径匹配
- 按配置文件中顺序的正则匹配
- /通用匹配
- last
- 基本标识
- break
- 中止rewrite,不再继续匹配
- redirect
- 返回临时重定向的HTTP 302
- permanent
- 返回永久重定向的HTTP 301
openresty整合了nginx + lua,也整合了它们的优势;有nginx的高性能,也带来了 lua的便捷式开发;并以lua语言开发了许多第三方模块儿,极大的丰富了lua插件开 发库。另外,openresty不仅仅是引入了lua,而是依靠两者的整合,提供了一套高 性能、异步非阻塞的服务器端开发框架。
注意,openresty源码包已经包含了nginx和luajit的安装包,因此它们不需要单独安装。
for Ubuntu/Centos
下载源码包,[[http://openresty.org/en/download.html][网址]]
$ tar zxvf openresty-1.11.2.1.tar.gz
$ cd openresty-1.11.2.1
$ ./configure --prefix=/home/sqlfocus/Program/openresty --with-luajit
--without-http_redis2_module --with-http_iconv_module
$ make
$ make install
for mac os X
#10.15版本需要执行此句,否则报错“make[1]: *** [lj_folddef.h] Segmentation fault: 11”
$ export MACOSX_DEPLOYMENT_TARGET=10.14
#下载pcre/openssl源码并解压,编译时指定目录
$ ./configure --prefix=/home/sqlfocus/Program/openresty
--with-openssl=/path/to/openssl/dir
--with-pcre=/path/to/pcre/dir
$ make
$ make install
- set_by_lua
- 流程分支处理判断变量初始化
- rewrite_by_lua
- 转发、重定向、缓存等功能
- access_by_lua
- IP准入、接口权限等情况集中处理
- content_by_lua
- 内容生成
- header_filter_by_lua
- 应答HTTP过滤处理
- body_filter_by_lua
- 应答BODY过滤处理
- log_by_lua
- 会话完成后本地异步完成日志记录
OpenResty安装之后就有配置文件及相关的目录的,为了工作目录与安装目录互不干扰, 我们另外创建一个OpenResty的工作目录。
创建工作目录
$ mkdir ~/openresty-test ~/openresty-test/conf ~/openresty-test/logs
创建nginx配置文件
$ emacs -nw ~/openresty-test/conf/nginx.conf
worker_processes 1
error_log logs/error.log
events {
worker_connections 1024
}
http {
server {
listen 6699
location / {
default_type text/html
content_by_lua_block {
ngx.say("hello world")
}
}
}
}
启动nginx
$ nginx -p ~/openresty-test
验证
$ curl http://localhost:6699
由于nginx的限速模块儿ngx_http_limit_conn_module、ngx_http_req_limit_module 都是基于nginx.conf配置文件,更新配置后必须重新加载进程;且使用不方便,不能 细粒度定制化限速,比如针对不同的IP制订不同的限速策略等。
本模块儿并不是通过修正nginx的模块儿,使其灵活易用;而是模仿nginx的模块儿, 利用Lua表重新实现了限速功能,因此暴露了灵活的接口,可灵活定制策略、细粒度 操控限速对象等。
本节将从实际需求出发,深入了解openresty。
不知道大家什么时候开始注意的,百度的首页已经不再是HTTP协议,它已经全面修改到 了HTTPS协议上。但是对于大家的输入习惯,估计还是在地址栏里面输入baidu.com,回 车后发现它会自动跳转到https://www.baidu.com ,这时候就需要的外部重定向了。
修改nginx的配置文件中的location如下:
location = /foo {
content_by_lua_block {
ngx.say([[I am foo]])
}
}
location = / {
rewrite_by_lua_block {
return ngx.redirect('/foo');
}
}
重新启动nginx
$ nginx -p ~/openresty-test
查看跳转效果
$ curl -i http://localhost:6699
$ curl -i http://localhost:6699/foo
location /sum {
#使用access阶段完成黑名单
access_by_lua_block {
local black_ips = {["127.0.0.1"]=true}
local ip = ngx.var.remote_addr
if true == black_ips[ip] then
ngx.exit(ngx.HTTP_FORBIDDEN)
end
}
#处理业务
content_by_lua_block {
local a = tonumber(ngx.var.arg_a) or 0
local b = tonumber(ngx.var.arg_b) or 0
ngx.say("sum:", a + b)
}
}
location /download {
access_by_lua_block {
ngx.var.limit_rate = 1000
}
}
参考360工程师的测试工具github