openresty配置部署

环境: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”)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了小程序应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值