# Pre-process request via upstream then forward to proxy

**URL:** <https://www.ruby-forum.com/t/pre-process-request-via-upstream-then-forward-to-proxy/206578>\
**Category:** NGINX\
**Created:** [May 5, 2011, 6:12am UTC](https://www.ruby-forum.com/t/pre-process-request-via-upstream-then-forward-to-proxy/206578 "2011-05-05T06:12:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [May 5, 2011, 6:12am UTC](https://www.ruby-forum.com/t/pre-process-request-via-upstream-then-forward-to-proxy/206578/1 "2011-05-05T06:12:12Z")

</div>

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:

> **[Pre-process request via upstream then forward to proxy.](https://forum.nginx.org/read.php?2,195702,195702#msg-195702)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![AndrewH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@AndrewH](https://www.ruby-forum.com/u/AndrewH)\
**Post date:** [May 5, 2011, 8:11am UTC](https://www.ruby-forum.com/t/pre-process-request-via-upstream-then-forward-to-proxy/206578/2 "2011-05-05T08:11:53Z")

</div>

On Thu, May 5, 2011 at 12:11 PM, AndrewH [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 [GitHub - openresty/lua-nginx-module: Embed the Power of Lua into NGINX HTTP servers · GitHub](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 😉

> 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

---

<div class="post-metadata">

**Author:** ![AndrewH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@AndrewH](https://www.ruby-forum.com/u/AndrewH)\
**Post date:** [May 5, 2011, 8:31am UTC](https://www.ruby-forum.com/t/pre-process-request-via-upstream-then-forward-to-proxy/206578/3 "2011-05-05T08:31:38Z")

</div>

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://grid.net.ru/nginx/eval.en.html)

> **[ngx\_http\_auth\_request\_module: log](http://mdounin.ru/hg/ngx_http_auth_request_module/)**

Maxim D.
