Pre-process request via upstream then forward to proxy.

agentzh agentzh at gmail.com
Thu May 5 10:11:32 MSD 2011


On Thu, May 5, 2011 at 12:11 PM, AndrewH <nginx-forum at nginx.us> wrote:
> As part of processing a request, I need to first send and receive a
> response to/from an upstream to authenticate some credentials in the
> request.  Once this has been done, the original request needs to be
> proxied to another upstream.
>

Your task can be trivially done in pure Lua by using the ngx_lua
module. Specifically, by using the ngx.location.capture() interface
for Lua to issue your subrequests and wait for the responses.

See http://github.com/chaoslawful/lua-nginx-module for the full documentation.

You can surely code it up in pure C by looking at how ngx_lua does on
the C level. But it's not recommended because it's error prone and the
performance gain is small.

> According to Evan Miller's tutorial, the only valid location for
> spawning sub-requests is in an output filter ie. to post-process a
> response.

Nope, you can surely create subrequests in any of your rewrite,
access, and content phase handlers. That part of Evan Miller's
tutorial is somehow misleading, and keep in mind that Evan stated like
this: "as far as I know..." So he is not to blame ;)

> Does anyone know what the sanctioned way of going about this in NGINX
> is?  Or which existing modules/examples implement similar
> functionality?
>

See the echo_subrequest and echo_location directives provided by ngx_echo:

    http://github.com/agentzh/echo-nginx-module

But their (synchronous but non-blocking) subrequest implementation is
not as correct as ngx_lua's.

The ngx_auth_request module by Maxim Dounin is an example of issuing
subrequests in an access phase handler and my ngx_srcache module is
another example for this:

    http://github.com/agentzh/srcache-nginx-module

The ngx_eval module is an example of issuing subrequests in a rewrite
phase handler:

    http://www.grid.net.ru/nginx/eval.en.html

Happy subrequesting!

-agentzh



More information about the nginx mailing list