Welcome! Log In Create A New Profile

Advanced

Re: Nginx postgres problem

Yichun Zhang (agentzh)
March 05, 2014 05:34PM
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

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Subject Author Posted

Nginx postgres problem

arunh March 04, 2014 10:15AM

Re: Nginx postgres problem

Yichun Zhang (agentzh) March 04, 2014 06:20PM

Re: Nginx postgres problem

arunh March 05, 2014 11:39AM

Re: Nginx postgres problem

Yichun Zhang (agentzh) March 05, 2014 03:00PM

Re: Nginx postgres problem

Yichun Zhang (agentzh) March 05, 2014 03:04PM

Re: Nginx postgres problem

arunh March 05, 2014 05:21PM

Re: Nginx postgres problem

Yichun Zhang (agentzh) March 05, 2014 05:34PM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 297
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready