An nginx server provides a simple REST interface, using a PostgreSQL instance as the backend. The nginx should insert POST data (already in JSON format) into a database table. Unfortunately, the $request_body
containing the POST data is filled by nginx only on fastcgi_pass
, proxy_pass
, …
The ngx_form_input doesn’t help either, as it expects the POST data in key-value format. I tried ngx_echo, but this leads to an internal server error:
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;
}
It seems that ngx_echo doesn’t work together with ngx_postgres. Are there any other ways to get the request body data?