OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。
OpenResty® 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。
OpenResty® 的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。
下载部署 wget https://openresty.org/download/openresty-1.13.6.2.tar.gz cd openresty-1.13.6.2 ./configure make && make install 目录结构 [root@server openresty]# ls bin COPYRIGHT luajit lualib nginx pod resty.index site # 分析目录结构 # 相比Nginx源代码目录相比少了很多东西,少了的东西在bundle目录下。build是编译后生成的目标中间文件 # 在bundle目录中有很多模块,最核心的是Nginx源代码,nginx-相应的版本中,当前的openresty基于nginx-1.13.6.2这个版本进行二次开发。 /root/openresty-1.13.6.2/configure --help # 查看configure脚本帮助文件 # 和nginx帮助文件基本没有太大的不同,只不过openresty集成了很多第三方模块 # 前缀带有without是默认是内置在编译版本中的 # 前缀带有with是默认是没有在编译版本中的 添加Lua代码启动 添加lua注意 /* 在nginx.conf 中实际是可以直接添加Lua代码,但是不能把Lua的语法Lua的源代码直接放在conf中,因为nginx的解析器它的配置语法是跟Lua代码是不相同的。 在openresty的nginx lua模块中,它提供了几条指令,其中有一条指令是content_by_lua content_by_lua是在http请求处理的内容生成阶段,我们用Lua代码来处理。 */ /* openresty的Lua模块中提供了一些API 如ngx.say,会去生成http响应,浏览器在发起http请求中,它会在User-Agent这样的head中, 去添加当前浏览器的类型,我是xxx,我用了什么样的内核,用ngx.req.ge_headers把用户请求中的头部取出来,然后找出User-Agent,把User-Agent值通过这样一种文本方式返回给浏览器中. 通过openresty的nginx lua模块,我们可以用它提供给我们的API完成很多功能,我们可以利用Lua本身的一些工具库把Lua语言添加进来参加我们生成响应的这样一个过程中。 直接使用openresty提供的API或者Lua代码生成响应,为浏览器客户端提供服务。 我们可以使用Lua语言以及提供的相应的API库直接去访问Redis,Mysql,Tomcat等这样的服务,然后把不同的响应通过程序逻辑组成相应的http响应返回给用户 */ 修改配置文件 [root@server nginx]# cat /usr/local/openresty/nginx/conf/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location /lua { default_type text/html; content_by_lua ' ngx.say("User-Agent: ", ngx.req.get_headers()["User-Agent"]) '; } location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } # 启动 [root@server nginx]# cat /usr/local/openresty/nginx/sbin/nginx 访问 Linux下ab性能测试 下载配置ab [root@server openresty]# yum -y install httpd-tools [root@server openresty]# ab -V This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, Licensed to The Apache Software Foundation, 1W并发压力测试 [root@server openresty]# ab -c 10000 -n 100000 127.0.0.1/lua This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, Licensed to The Apache Software Foundation, Benchmarking 127.0.0.1 (be patient) Completed 10000 requests Completed 20000 requests Completed 30000 requests Completed 40000 requests Completed 50000 requests Completed 60000 requests Completed 70000 requests Completed 80000 requests Completed 90000 requests Completed 100000 requests Finished 100000 requests Server Software: openresty/1.13.6.2 # 服务器信息和版本 Server Hostname: 127.0.0.1 # 服务器的域名 Server Port: 80 # 端口 Document Path: /lua # 访问的路径 Document Length: 28 bytes # 文档大小 Concurrency Level: 10000 # 并发请求数 Time taken for tests: 5.978 seconds # 整个测试持续的时间 Complete requests: 100000 # 完成的请求数 Failed requests: 196008 # 失败的请求数 (Connect: 0, Receive: 0, Length: 98912, Exceptions: 97096) Write errors: 0 # 网络连接写入错误数 Total transferred: 511104 bytes # 传输的总数据量 HTML transferred: 81312 bytes # 传输的HTML内容量 Requests per second: 16729.18 [#/sec] (mean) # 平均每秒请求数 Time per request: 597.758 [ms] (mean) # 所有用户请求一次的平均时间 Time per request: 0.060 [ms] (mean, across all concurrent requests) # 单个用户请求一次的时间 Transfer rate: 83.50 [Kbytes/sec] received # 传输速率 Connection Times (ms) min mean[+/-sd] median max Connect: 0 323 302.4 256 3359 Processing: 64 246 62.9 247 525 Waiting: 0 8 48.8 0 342 Total: 283 570 304.2 512 3566 # 所有服务请求的百分比占用时间,这里50%的请求用时512ms, 一般看90%的部分 Percentage of the requests served within a certain time (ms) 50% 512 66% 541 75% 545 80% 547 90% 556 95% 1463 98% 1524 99% 1555 100% 3566 (longest request) 1.5W并发压力测试 [root@server openresty]# ab -c 15000 -n 100000 127.0.0.1/lua This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, Licensed to The Apache Software Foundation, Benchmarking 127.0.0.1 (be patient) Completed 10000 requests Completed 20000 requests Completed 30000 requests Completed 40000 requests Completed 50000 requests Completed 60000 requests Completed 70000 requests Completed 80000 requests Completed 90000 requests Completed 100000 requests Finished 100000 requests Server Software: openresty/1.13.6.2 Server Hostname: 127.0.0.1 Server Port: 80 Document Path: /lua Document Length: 28 bytes Concurrency Level: 15000 Time taken for tests: 6.128 seconds Complete requests: 100000 Failed requests: 201164 (Connect: 0, Receive: 0, Length: 102820, Exceptions: 98344) Write errors: 0 Total transferred: 291456 bytes HTML transferred: 46368 bytes Requests per second: 16317.62 [#/sec] (mean) Time per request: 919.252 [ms] (mean) Time per request: 0.061 [ms] (mean, across all concurrent requests) Transfer rate: 46.44 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 471 296.5 409 3498 Processing: 74 380 100.9 383 878 Waiting: 0 7 57.5 0 573 Total: 494 851 303.4 808 4077 Percentage of the requests served within a certain time (ms) 50% 808 66% 818 75% 832 80% 856 90% 888 95% 1750 98% 1812 99% 1832 100% 4077 (longest request) 2W并发压力测试 [root@server openresty]# ab -c 20000 -n 100000 127.0.0.1/lua This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, Licensed to The Apache Software Foundation, Benchmarking 127.0.0.1 (be patient) Completed 10000 requests Completed 20000 requests Completed 30000 requests Completed 40000 requests Completed 50000 requests Completed 60000 requests Completed 70000 requests Completed 80000 requests Completed 90000 requests Completed 100000 requests Finished 100000 requests Server Software: openresty/1.13.6.2 Server Hostname: 127.0.0.1 Server Port: 80 Document Path: /lua Document Length: 28 bytes Concurrency Level: 20000 Time taken for tests: 6.101 seconds Complete requests: 100000 Failed requests: 194710 (Connect: 0, Receive: 0, Length: 97430, Exceptions: 97280) Write errors: 0 Total transferred: 478720 bytes HTML transferred: 76160 bytes Requests per second: 16389.97 [#/sec] (mean) Time per request: 1220.258 [ms] (mean) Time per request: 0.061 [ms] (mean, across all concurrent requests) Transfer rate: 76.62 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 625 432.2 507 3665 Processing: 107 474 149.2 441 1024 Waiting: 0 15 94.3 0 714 Total: 521 1099 443.9 984 4362 Percentage of the requests served within a certain time (ms) 50% 984 66% 1005 75% 1010 80% 1019 90% 1949 95% 1997 98% 2131 99% 2421 100% 4362 (longest request) 通过lua实现灰度发布