Nginx postgres problem

Yichun Zhang (agentzh) agentzh at gmail.com
Wed Mar 5 22:32:50 UTC 2014


Hello!

On Wed, Mar 5, 2014 at 2:21 PM, arunh wrote:
> Depending on the parameters a,b,c and d I will get the IP and port of the
> destination server (by communicating with postgres) where the request must
> be redirected to ie the new url is of the form:
> http://IP:port/a/b/c/d.
>

You need either a 301/302 redirect or a proxy_pass to go to external target.

Below is an example:

    rewrite_by_lua '
        local new_target = "http:/IP:port/a/b/c/d"
        return ngx.redirect(new_target)
    ';

> Using both ngx.redirect and nginx.exec() are giving errors.
> I tried to redirect the url to "www.google.com" using ngx.redirect inside
> rewrite_by_lua. Even I get the same error.
>

Please provide a minimal but still complete example that can reproduce
the issue.

> You had mentioned that " redirects in a subrequest won't affect its parent
> requests." Does that mean that I cannot change the url inside the
> rewrite_by_lua module?
>

You created a subrequest with ngx.location.capture and you used the
"rewrite" directive in the location targeted by the subrequest in the
hope that it will change the parent request calling
ngx.location.capture.

What you need is to initiate redirects in your *main* request. So you
should not use ngx.location.capture for a redirect. I never say you
cannot perform redirect in rewrite_by_lua. Do not get me wrong.

> I cannot use http rewrite module as this will be executed before the
> rewrite_by_lua code is executed.
>

No, as I've said, you should use the Lua API in rewrite_by_lua to
perform redirects.

> 2014/03/05 22:59:42 [error] 8129#0: *374 lua entry thread aborted: runtime
> error: [string "rewrite_by_lua"]:32: attempt to call ngx.exec after sending
> out response headers

The error clearly indicates the problem. You should not send the
response header before doing redirects (note that, ngx.say, ngx.print,
ngx.flush all trigger sending out the response header automatically,
so avoid them).

Regards,
-agentzh



More information about the nginx mailing list