Pre-process request via upstream then forward to proxy

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.

According to Evan M.'s tutorial, the only valid location for
spawning sub-requests is in an output filter ie. to post-process a
response. So it appears that this approach is useful for massaging a
reponse but not valid for actually performing pre-processing on a
request.

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

  • Andrew

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,195702,195702#msg-195702

On Thu, May 5, 2011 at 12:11 PM, AndrewH [email protected] 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 M.'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 M.'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 :wink:

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 D. 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

Hello!

On Thu, May 05, 2011 at 12:11:30AM -0400, AndrewH 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.

According to Evan M.'s tutorial, the only valid location for
spawning sub-requests is in an output filter ie. to post-process a
response. So it appears that this approach is useful for massaging a
reponse but not valid for actually performing pre-processing on a
request.

Evan M.'s claim was correct before 0.7.25. In recent versions
you may issue subrequests almost everywhere. There are some
caveats though - it’s up to you to take appropriate measures to
ensure subrequest response won’t appear on client’s connection.

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

Take a look at:

http://grid.net.ru/nginx/eval.en.html
http://mdounin.ru/hg/ngx_http_auth_request_module/

Maxim D.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs