是什么
OpenResty是一个基于Nginx的高性能Web应用服务器,它通过将Nginx与Lua编程语言集成在一起,提供了一种灵活且高效的方式来开发和扩展Web应用程序。
通过配置文件和Lua脚本的灵活组合,您可以使用OpenResty构建高性能、可扩展和定制化的Web应用程序。
解决什么问题
-
高性能Web应用程序:OpenResty基于Nginx,通过将Nginx与Lua语言集成,提供了一个高性能的Web应用服务器。它利用Nginx的事件驱动、非阻塞I/O等特性,加上Lua脚本的灵活性,可以处理大量并发请求,提供高吞吐量和低延迟的响应。
-
轻量级和高效的开发方式:OpenResty使用Lua作为扩展和开发的脚本语言,Lua是一种轻量级、快速和易于学习的脚本语言。借助Lua脚本,可以以简洁、灵活和高效的方式开发和扩展Web应用程序,实现复杂的业务逻辑和定制化需求。
-
简化和加速Web应用开发:OpenResty提供了一组强大的API和库,使开发人员能够轻松处理HTTP请求和响应,操作请求头和响应体,访问数据库和缓存,以及实现高级功能如反向代理、负载均衡、缓存控制、安全认证等。这简化了Web应用程序的开发过程,并提高了开发效率。
-
扩展性和定制化:OpenResty的灵活性和可扩展性使得开发人员能够轻松扩展现有的Nginx功能,添加自定义模块和插件。可以使用Lua编写自定义模块来实现特定的业务需求,与其他系统进行集成,并根据实际需求对请求和响应进行个性化处理。
-
高度可定制的配置:OpenResty使用Nginx的配置文件语法,具有丰富的配置选项和灵活的配置方式。您可以使用配置文件定义URL路由、反向代理规则、缓存策略、访问控制规则等。这使得您能够根据具体的业务需求对OpenResty进行高度定制化的配置。
-
高可用性和可靠性:OpenResty通过利用Nginx的负载均衡和故障转移功能,以及Lua脚本的灵活性,可以实现高可用性的架构和可靠的应用部署。您可以配置多个实例并使用负载均衡算法来分发请求,同时使用健康检查和故障转移机制来确保应用程序的可用性和稳定性。
常用指令
通过content_by_lua
指令直接输出文本
server {
# 通过content_by_lua输出文本
location /hello_lua {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua!")';
}
}
读取请求参数
server {
# 读取查询参数
# ngx.var.arg_a:读取参数"a"的值
location /nginx_var {
# MIME type determined by default_type:
default_type 'text/plain';
# try access /nginx_var?a=hello,world
content_by_lua_block {
ngx.say(ngx.var.arg_a)
}
}
}
读取请求 body
server {
# 读取请求body
# curl -XPOST localhost/request_body -d '{"name":"alice"}'
location = /request_body {
client_max_body_size 50k;
client_body_buffer_size 50k;
content_by_lua_block {
ngx.req.read_body() -- explicitly read the req body
local data = ngx.req.get_body_data()
if data then
ngx.say("body data:")
ngx.print(data)
return
end
-- body may get buffered in a temp file:
local file = ngx.req.get_body_file()
if file then
ngx.say("body is in file ", file)
else
ngx.say("no body found")
end
}
}
}
子请求
server {
# 子请求
# transparent non-blocking I/O in Lua via subrequests
# (well, a better way is to use cosockets)
location = /lua {
# MIME type determined by default_type:
default_type 'text/plain';
content_by_lua_block {
local res = ngx.location.capture("/some_other_location")
if res then
ngx.say("status: ", res.status)
ngx.say("body:")
ngx.print(res.body)
end
}
}
# 通过content_by_lua输出文本
location /some_other_location {
internal; # 内部调用
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua!")';
}
}
带参数的子请求
server {
# 带有参数的子请求
location /foo {
default_type 'text/plain';
rewrite_by_lua_block {
res = ngx.location.capture("/memc", {
args = {
cmd = "incr", value = ngx.var.arg_value
}
}
)
if res then
ngx.say(res.body)
end
}
}
# 通过content_by_lua输出文本
location /memc {
internal; # 内部调用
default_type 'text/plain';
content_by_lua_block {
ngx.say(string.format("cmd:%s value:%s",ngx.var.arg_cmd,ngx.var.arg_value))
}
}
}
uri 重写
server {
# 重定向
location /rewrite {
rewrite ^ /rewrite_internal ;
}
location /rewrite_internal {
internal; # 内部调用'
default_type 'text/plain';
content_by_lua_block {
ngx.say("rewrite internal")
}
}
}
搭建一个测试Openresty的测试环境
docker run \
--name openresty \
-p 80:80 \
-d \
-v ${PWD}/data/conf.d:/etc/nginx/conf.d \
openresty/openresty:alpine
Api列表
- ngx.arg
- ngx.var.VARIABLE
- Core constants
- HTTP method constants
- HTTP status constants
- Nginx log level constants
- ngx.ctx
- ngx.location.capture
- ngx.location.capture_multi
- ngx.status
- ngx.header.HEADER
- ngx.resp.get_headers
- ngx.req.is_internal
- ngx.req.start_time
- ngx.req.http_version
- ngx.req.raw_header
- ngx.req.get_method
- ngx.req.set_method
- ngx.req.set_uri
- ngx.req.set_uri_args
- ngx.req.get_uri_args
- ngx.req.get_post_args
- ngx.req.get_headers
- ngx.req.set_header
- ngx.req.clear_header
- ngx.req.read_body
- ngx.req.discard_body
- ngx.req.get_body_data
- ngx.req.get_body_file
- ngx.req.set_body_data
- ngx.req.set_body_file
- ngx.req.init_body
- ngx.req.append_body
- ngx.req.finish_body
- ngx.req.socket
- ngx.exec
- ngx.redirect
- ngx.send_headers
- ngx.headers_sent
- ngx.print
- ngx.say
- ngx.log
- ngx.flush
- ngx.exit
- ngx.eof
- ngx.sleep
- ngx.escape_uri
- ngx.unescape_uri
- ngx.encode_args
- ngx.decode_args
- ngx.encode_base64
- ngx.decode_base64
- ngx.crc32_short
- ngx.crc32_long
- ngx.hmac_sha1
- ngx.md5
- ngx.md5_bin
- ngx.sha1_bin
- ngx.quote_sql_str
- ngx.today
- ngx.time
- ngx.now
- ngx.update_time
- ngx.localtime
- ngx.utctime
- ngx.cookie_time
- ngx.http_time
- ngx.parse_http_time
- ngx.is_subrequest
- ngx.re.match
- ngx.re.find
- ngx.re.gmatch
- ngx.re.sub
- ngx.re.gsub
- ngx.shared.DICT
- ngx.shared.DICT.get
- ngx.shared.DICT.get_stale
- ngx.shared.DICT.set
- ngx.shared.DICT.safe_set
- ngx.shared.DICT.add
- ngx.shared.DICT.safe_add
- ngx.shared.DICT.replace
- ngx.shared.DICT.delete
- ngx.shared.DICT.incr
- ngx.shared.DICT.lpush
- ngx.shared.DICT.rpush
- ngx.shared.DICT.lpop
- ngx.shared.DICT.rpop
- ngx.shared.DICT.llen
- ngx.shared.DICT.ttl
- ngx.shared.DICT.expire
- ngx.shared.DICT.flush_all
- ngx.shared.DICT.flush_expired
- ngx.shared.DICT.get_keys
- ngx.shared.DICT.capacity
- ngx.shared.DICT.free_space
- ngx.socket.udp
- udpsock:setpeername
- udpsock:send
- udpsock:receive
- udpsock:close
- udpsock:settimeout
- ngx.socket.stream
- ngx.socket.tcp
- tcpsock:connect
- tcpsock:sslhandshake
- tcpsock:send
- tcpsock:receive
- tcpsock:receiveany
- tcpsock:receiveuntil
- tcpsock:close
- tcpsock:settimeout
- tcpsock:settimeouts
- tcpsock:setoption
- tcpsock:setkeepalive
- tcpsock:getreusedtimes
- ngx.socket.connect
- ngx.get_phase
- ngx.thread.spawn
- ngx.thread.wait
- ngx.thread.kill
- ngx.on_abort
- ngx.timer.at
- ngx.timer.every
- ngx.timer.running_count
- ngx.timer.pending_count
- ngx.config.subsystem
- ngx.config.debug
- ngx.config.prefix
- ngx.config.nginx_version
- ngx.config.nginx_configure
- ngx.config.ngx_lua_version
- ngx.worker.exiting
- ngx.worker.pid
- ngx.worker.count
- ngx.worker.id
- ngx.semaphore
- ngx.balancer
- ngx.ssl
- ngx.ocsp
- ndk.set_var.DIRECTIVE
- coroutine.create
- coroutine.resume
- coroutine.yield
- coroutine.wrap
- coroutine.running
- coroutine.status
- ngx.run_worker_thread
指令列表
- lua_load_resty_core
- lua_capture_error_log
- lua_use_default_type
- lua_malloc_trim
- lua_code_cache
- lua_thread_cache_max_entries
- lua_regex_cache_max_entries
- lua_regex_match_limit
- lua_package_path
- lua_package_cpath
- init_by_lua
- init_by_lua_block
- init_by_lua_file
- init_worker_by_lua
- init_worker_by_lua_block
- init_worker_by_lua_file
- exit_worker_by_lua_block
- exit_worker_by_lua_file
- set_by_lua
- set_by_lua_block
- set_by_lua_file
- content_by_lua
- content_by_lua_block
- content_by_lua_file
- rewrite_by_lua
- rewrite_by_lua_block
- rewrite_by_lua_file
- access_by_lua
- access_by_lua_block
- access_by_lua_file
- header_filter_by_lua
- header_filter_by_lua_block
- header_filter_by_lua_file
- body_filter_by_lua
- body_filter_by_lua_block
- body_filter_by_lua_file
- log_by_lua
- log_by_lua_block
- log_by_lua_file
- balancer_by_lua_block
- balancer_by_lua_file
- lua_need_request_body
- ssl_client_hello_by_lua_block
- ssl_client_hello_by_lua_file
- ssl_certificate_by_lua_block
- ssl_certificate_by_lua_file
- ssl_session_fetch_by_lua_block
- ssl_session_fetch_by_lua_file
- ssl_session_store_by_lua_block
- ssl_session_store_by_lua_file
- lua_shared_dict
- lua_socket_connect_timeout
- lua_socket_send_timeout
- lua_socket_send_lowat
- lua_socket_read_timeout
- lua_socket_buffer_size
- lua_socket_pool_size
- lua_socket_keepalive_timeout
- lua_socket_log_errors
- lua_ssl_ciphers
- lua_ssl_crl
- lua_ssl_protocols
- lua_ssl_trusted_certificate
- lua_ssl_verify_depth
- lua_ssl_conf_command
- lua_http10_buffering
- rewrite_by_lua_no_postpone
- access_by_lua_no_postpone
- lua_transform_underscores_in_response_headers
- lua_check_client_abort
- lua_max_pending_timers
- lua_max_running_timers
- lua_sa_restart
- lua_worker_thread_vm_pool_size
指令执行流程如下: