标签: openresty

NGINX - OpenResty - 如何基于字符串反向代理对 2 个不同服务器的调用?

我正在尝试配置 NGINX/OpenResty 以根据 SOAP 请求上存在的字符串将 SOAP 调用代理到 ​​2 个不同的服务器。

我能做什么:我能够根据 SOAP 客户端调用的路径将请求代理到 2 个不同的服务器:

location /pathA {
    proxy_pass http://www.ServerA.com/PathA/;
}
location /pathB {
    proxy_pass http://www.ServerB.com/PathB/;
}
Run Code Online (Sandbox Code Playgroud)

我不能做什么:

我无法根据请求的内容分离流量。我认为主要原因是我无法正确组装LUA脚本来提取信息并随后使用它来代理请求。

location / {
    conten_by_lua '
        ngx.req.read_body()
        local match = ngx.re.match(ngx.var.request_body,"STRING TO FIND")
        if match then
            proxy_pass http://www.ServerA.com/PathA/;
        else
            proxy_pass http://www.ServerB.com/PathB/;
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

我安装了 OpenResty 并且 LUA 工作正常。

我想我在某处读到如果请求不是 HTTP POSTngx.req.read_body()将不起作用。那是对的吗?

感谢您的帮助。

nginx lua openresty

4
推荐指数
1
解决办法
1万
查看次数

nginx 中的日志轮换

我发现在做Nginx的日志旋转的例子在这里

但是一个简单的测试:

    set $date "2018-08-24";
    access_log /home/tim/log/access-http-$date.log default;
Run Code Online (Sandbox Code Playgroud)

生成一个名为access-http-.log. 我正在使用 nginx 1.13.6(带有 openresty)。

更新

经过大量的修改和调整后,我想出了以下 logrotate 脚本来旋转 nginx 生成的不同日志文件。我已经把它放到了 /etc/logrotate.conf 文件中。剩下的问题是日志不会每天轮换(我目前不确定为什么),但是 alogrotate -f <filename>产生的结果正是我想要的。

一个有趣的注意事项是nginx -s reload可以代替 USR1 使用。不幸的是,nginx 日志记录页面中没有引用它,但我找到了它(在手册页 IIRC 中)。我手动构建 openresty/nginx 以合并我需要的额外模块,因此我不会像 pid 保留那样获得打包版本中的各种额外功能。

/path/to/log/access-http.log /path/to/log/access-https.log /path/to/log/api.log /path/to/log/error.log {
    daily
    missingok
    notifempty
    create 664 nobody tim
    dateext
    dateformat -%Y-%m-%d
    dateyesterday
    rotate 10
    su tim tim
    postrotate
        /usr/local/bin/openresty -s reload
    endscript
}
Run Code Online (Sandbox Code Playgroud)

我认为这对于拥有大型 nginx 配置同时提供网页和 API 的任何人都非常有用。我将 http 分开,因为我不提供非 https 页面,并且它将脚本小子废话从我的页面日志中删除。

nginx logging openresty

1
推荐指数
1
解决办法
1万
查看次数

标签 统计

nginx ×2

openresty ×2

logging ×1

lua ×1