OpenResty体验
官网:https://openresty.org/cn/download.html
本文使用docker来部署环境
一、运行Docker容器,暴露80端口,然后通过宿主机IP:80即可访问容器里的OpenResty
docker run -itd --name openresty -p 8000:80 openresty/openresty:centos
二、进入容器
docker exec -it $(docker ps -f "name=openresty" -q) bash
#docker ps -f "name=openresty" -q是-f过滤条件,筛选出docker run指定的name,-q是只打印container id,将结果给docekr exec 执行
三、修改配置
openresty的文件在/usr/local/openresty/
nginx.conf配置文件在/usr/local/openresty/nginx/conf/nginx.conf,该配置文件include /etc/nginx/conf.d/*.conf,因此可在/etc/nginx/conf.d/创建你自己的配置文件。
执行命令:
vi /etc/nginx/conf.d/default.conf
将default.conf内容修改为:(注意upstream改为提供api接口的服务器,比如tomcat)
upstream tomcat {
server 127.0.0.1:8080;
server 127.0.0.1:8081;
server 127.0.0.1:8082 backup;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/local/openresty/nginx/html;
index index.html index.htm;
}
location /f/ {
proxy_pass http://tomcat/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/openresty/nginx/html;
}
}
reload配置,执行命令:
openresty -s reload
在宿主机执行curl "localhost:8000/f/xxx"(xxx替换为tomcat服务端提供的接口名)
四、健康检查
作为上层,需要感知底层upstream服务是否可用,如果server down了必须摘除,不再将请求转发到该upstream server上。因此需做健康检查,也即是探活。
健康检查的作用有两个:
1.保证后端服务高可用,请求不会落到down掉的机器;
2.优雅地上下线,将要上线的机器设置为OpenResty认为机器down的状态,保证请求不会再落到该机器,然后就可以进行一顿操作之后再让其恢复up的状态
简单的做法是让upstream后端服务提供一个/status接口,该接口的HTTP状态有两种:200和503,接口维护一个boolean变量,true则将接口状态改为200,否则改为503。OpenResty定时进行健康检查,调用该接口,返回HTTP状态是200则认为机器up,否则是down。
健康模块:https://github.com/openresty/lua-resty-upstream-healthcheck
步骤:
vi /etc/nginx/conf.d/default.conf
假设现在部署了3个api服务,都提供/status接口,将default.conf修改为
lua_package_path "/path/to/lua-resty-upstream-healthcheck/lib/?.lua;;";
# sample upstream block:
upstream foo.com {
server 127.0.0.1:12354;
server 127.0.0.1:12355;
server 127.0.0.1:12356 backup;
}
# the size depends on the number of servers in upstream {}:
lua_shared_dict healthcheck 1m;
lua_socket_log_errors off;
init_worker_by_lua_block {
local hc = require "resty.upstream.healthcheck"
local ok, err = hc.spawn_checker{
shm = "healthcheck", -- defined by "lua_shared_dict"
upstream = "foo.com", -- defined by "upstream"
type = "http",
http_req = "GET /status HTTP/1.0\r\nHost: foo.com\r\n\r\n",
-- raw HTTP request for checking
interval = 2000, -- run the check cycle every 2 sec
timeout = 1000, -- 1 sec is the timeout for network operations
fall = 3, -- # of successive failures before turning a peer down
rise = 2, -- # of successive successes before turning a peer up
valid_statuses = {200}, -- a list valid HTTP status code,可用逗号分隔多个有效状态
concurrency = 10, -- concurrency level for test requests
}
if not ok then
ngx.log(ngx.ERR, "failed to spawn health checker: ", err)
return
end
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/local/openresty/nginx/html;
index index.html index.htm;
}
location /f/ {
proxy_pass http://foo.com/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/openresty/nginx/html;
}
}
然后检查配置是否正确,执行命令:
openresty -t
接着reload
openresty -s reload

浙公网安备 33010602011771号