Insert followed by Nginx return/redirect does not seem to be working. #13

jimishjoban opened this Issue Aug 21, 2012 · 3 comments


None yet
3 participants


Thanks for the module.

I am trying to do a data Insert followed by nginx return but it seems postgres_query is skipped if I do a return. If I comment out the return, then insert works fine:

        postgres_pass  database2;
        rds_json on;
        postgres_query "INSERT INTO numbers VALUES(80)";
        return 301 $remote_url;

Any way to execute postgres_query before doing 301 redirection.




agentzh commented Aug 21, 2012

Hello! The standard "return" directive provided by the ngx_rewrite module runs in the "rewrite" phase while the ngx_postgres module works in the "content" phase. And the "rewrite" phase always runs before the "content" phase no matter how you put those directives in the nginx configuration file. So "return" always runs before "postgres_pass"/"postgres_query" no matter how you write them.


PiotrSikora commented Aug 21, 2012

Hey Jimish,
you cannot combine postgres_query and return directives (or any other content handlers, for that matter).

However, you can achieve more or less what you want by using postgres_rewrite directive:

location / {
    postgres_pass     database2;
    postgres_query    "INSERT INTO numbers VALUES(80)";

    postgres_rewrite  no_changes 430;
    postgres_rewrite  changes    430;
    error_page        430 @redirect;

location @redirect {
    return            301 $remote_url;

430 (unused) response code is used here, because for the reasons that I don't recall right now, I filtered out 3xx response codes from the posgres_rewrite directive.

Hope this helps :)

I'm keeping this open as a reminder to re-investigate 3xx response codes and, if feasible, to add support for postgres_rewrite changes 301 $remote_url, which would make this use case less tricky.

Thanks a lot! That tip really helped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment