环境:centos
yum install -y readline-devel pcre-devel openssl-devel gcc
yum install -y g++ make automake autoconf
./configure --prefix=/usr/local/openresty \
--with-luajit
--with-http_iconv_module \
--with-http_stub_status_module
gmake ; gmake install
groupadd -f www
useradd -r -s /sbin/nologin -g www www
openresty配置
反向代理配置实例
user www www;
worker_processes 1;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 3000;
}
http {
include mime.types;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
location / {
proxy_pass https://github.com;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /README.md {
proxy_pass https://github.com/moonbingbing/openresty-best-practices/blob/master/README.md
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwared_for;
}
}
}
/lb_health_check配置
location = /lb_health_check {
add_header Content-Type "text/plain; charset=utf-8";
return 200;
access_log off;
}
favicon.ico配置
location = /favicon.ico {
log_not_found on;
access_log off;
}
重启
nginx -s reload
nginx -t -c /path/to/nginx.conf
nginx -s stop
压力测试 yum -y install httpd-tools
lua扩展
获取POST请求配置
# vim conf/nginx.conf
log_format main '$remote_addr | $remote_user | [$time_local] | "$request" | '
'$status | $body_bytes_sent | "$http_referer" | '
'"$http_user_agent" | "$http_x_forwarded_for" | "$request_body" | "$resp_body"';
# vim vhost/http.conf
server {
listen 80;
set $resp_body "";
include vhost/loc/yun_location.conf;
}
# vim vhost/loc/yun_location.conf
location = /lb_health_check {
add_header Content-Type "text/plain; charset=utf-8";
return 200;
access_log off;
}
location = /favicon.ico {
log_not_found on;
access_log off;
}
location /api {
lua_need_request_body on;
body_filter_by_lua '
local resp_body = string.sub(ngx.arg[1], 1, 1000)
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
if ngx.arg[2] then
ngx.var.resp_body = ngx.ctx.buffered
end
';
proxy_pass http: //yun_backend;
}
接收请求配置
# nginx.conf
location ~ /lua_request/(\d+)/(\d+) {
set $a $1;
set $b $host;
default_type "application/json";
content_by_lua_file conf/lua/lua_request.lua
echo_after_body "ngx.var.b $b";
}
# 获取变量
local variable = ngx.var;
ngx.say("ngx.var.a: ", variable.a); -- set $a $1;
ngx.say("ngx.var.b: ", variable.b); -- set $b $host;
ngx.say("ngx.var[1]: ", variable[1]);
ngx.say("ngx.var[2]: ", variable[2]);
# 获取头
local headers = ngx.req.get_headers();
ngx.say("Host: ", headers["Host"]);
ngx.say("user-agent: ", headers["user-agent"]);
ngx.say("user-agent: ", headers.user_agent);
ngx.say("ngx.req.http_version: ", ngx.req.http_version());
ngx.say("ngx.req.get_method: ", ngx.req.get_method());
ngx.say("ngx.req.raw_header: ", ngx.req.raw_header()); -- 未解析的请求头字符串
for k, v in pairs(headers) do
ngx.say(k, ": ", v);
end
# 获取get请求
local get_argument = ngx.req.get_uri_args();
for k, v in pairs(get_argument) do
ngx.say(k, ": ", v);
end
# 获取post请求
ngx.req.read_body();
local post_argument = ngx.req.get_post_args();
for k, v in pairs(post_argument) do
ngx.say(k, ": ", v);
end
ngx.say("ngx.req.get_body_data(): ", ngx.req.get_body_data()); -- 解析的请求body体内容字符串
输出响应
# nginx.conf
location ~ /lua_response {
default_type "application/json";
content_by_lua_file conf/lua/lua_response.lua;
}
# 响应设置
ngx.header.a = "1" -- 写响应头
ngx.header.b = {"2", "3"} -- 写多个响应头
ngx.say("") -- 响应内容体
ngx.print("") -- 同上, 多输出换行符
ngx.exit(200) -- 指定状态码退出
ngx.status = 200
ngx.resp.get_headers()
ngx.send_headers()
# 编码解码
local request_uri = ngx.var.request_uri; -- 未经解码的请求uri
ngx.say("unescape_uri: ", ngx.unescape_uri(request_uri));
ngx.say("md5: ", ngx.md5("hello, world"));
ngx.say("time: ", ngx.http_time(ngx.time()));
ngx.escape_uri/ngx.unescape_uri -- uri编码解码
ngx.encode_args/ngx.decode_args -- 参数编码解码
ngx.encode_base64/ngx.decode_base64 -- base64编码解码
ngx.re.match -- 正则表达式匹配
## 全局内存
# nginx.conf
lua_shared_dict shared_data 1m;
location /lua_shared_dict {
default_type "application/json";
content_by_lua_file conf/lua/lua_shared_dict.lua;
}
# 获取全局共享的内存变量
local shared_data = ngx.shared.shared_data;
local i = shared_data:get("i")
if not i then
i = 100;
shared_data:set("i", i);
end
i = shared_data:incr("i", 1);
ngx.say("i = ", i);
## 处理流程
init_by_lua, init_by_lua_file
init_worker_by_lua, init_worker_by_lua_file
set_by_lua, set_by_lua_file
rewrite_by_lua, rewrite_by_lua_file
access_by_lua, access_by_lua_file
content_by_lua, content_by_lua_file
header_filter_by_lua, header_filter_by_lua_file
body_filter_by_lua, body_filter_by_lua_file
log_by_lua, log_by_lua_file
## 引入模块
# nginx.conf
lua_package_path "/usr/local/openresty/lualib/?.lua;;"; -- lua模块, ”;;”表示默认搜索路径
lua_package_cpath "/usr/local/openresty/lualib/?.so;;"; -- c模块
local cjson = require(“cjson”)
local redis = require(“resty.redis”)