postgresql - nginx:在 PostgreSQL 中存储 POST 数据

标签 postgresql rest nginx openresty

nginx 服务器提供简单的 REST 接口(interface),使用 PostgreSQL 实例作为后端。 nginx 应该将 POST 数据(已经是 JSON 格式)插入到数据库表中。不幸的是,包含 POST 数据的 $request_body 仅由 nginx 在 fastcgi_passproxy_pass、...

上填充

ngx_form_input也没有帮助,因为它需要键值格式的 POST 数据。我试过ngx_echo ,但这会导致内部服务器错误:

location ~ "^/api/v1/dummy/$" {
    auth_basic              "Restricted";
    auth_basic_user_file    /usr/local/etc/nginx/.htpasswd;

    if ($request_method != POST) {
        return 405;
    }

    client_max_body_size    100k;
    client_body_buffer_size 100k;

    echo_read_request_body;

    postgres_pass       postgresql;
    postgres_escape     $json =$request_body;

    postgres_query      POST "INSERT INTO mytable (data) VALUES ('$json')";
    postgres_rewrite    POST changes    201;
    postgres_rewrite    POST no_changes 204;
}

ngx_echo 似乎不能与 ngx_postgres 一起使用。还有其他方法获取请求正文数据吗?

最佳答案

echo_read_request_bodypostgres_pass 指令都在内容阶段工作。 在这种情况下只有一个模块可以工作。

这里的问题是 nginx 本质上是异步的。 Nginx 可能会在收到完整请求正文之前启动上游连接。

使用 OpenResty,您可以强制 nginx 通过 lua_need_request_body 读取整个请求正文。 请注意 client_body_buffer_sizeclient_max_body_size。 包含空的rewrite_by_lua*

另一个可能的解决方案是编写 Lua 代码,例如在 set_by_lua_block 中并读取完整的请求正文,请记住它可能会缓冲到文件中,使用 ngx.req.get_body_file检查一下。

关于postgresql - nginx:在 PostgreSQL 中存储 POST 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55380295/

相关文章:

nginx - nginx ldap auth 和 CoreOS Dex 之间的区别

PostgreSQL 恢复备份解决方案

sql - 日历的 Postgres 查询

java - 如何从 Java 在 AWS 中生成签名

java - Tomcat 7 Jersey Jackson 200 OK 但有效载荷被剥离

nginx - 为所有上游服务器位置启用 COR

nginx - 使用Ingress Kubernetes公开流量

postgresql - 如何在 PostgreSQL 中获取唯一的日期范围?

javascript - 在 Adonis Lucid 子查询上选择 JSON 字段键

authentication - WCF REST RequestInterceptor 身份验证