# Further "Hacking" the event model of Nginx

**URL:** <https://www.ruby-forum.com/t/further-hacking-the-event-model-of-nginx/191684>\
**Category:** NGINX\
**Created:** [July 16, 2010, 6:02am UTC](https://www.ruby-forum.com/t/further-hacking-the-event-model-of-nginx/191684 "2010-07-16T06:02:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinjenace](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/d9b06d/32.png) [@vinjenace](https://www.ruby-forum.com/u/vinjenace)\
**Post date:** [July 16, 2010, 6:02am UTC](https://www.ruby-forum.com/t/further-hacking-the-event-model-of-nginx/191684/1 "2010-07-16T06:02:18Z")

</div>

Hi all,

I have read the following post.  
[http://www.ruby-forum.com/topic/149417](http://www.ruby-forum.com/topic/149417)  
It is helpful, I am new to nginx and need some instructions on my work.

We have 3 machines, server MASTER is the application server visible to  
client. server WORK1 and server WORK2 is the task processing server. the  
request sequence is like this:

1. client  
-send request

2. nginx server MASTER:  
-receive request  
-extract useful information from request  
-post the re-structured extract data to nginx server WORK1

3. nginx server WORK1:  
-receive the request  
-process…  
-send back response

4. nginx server MASTER:  
-receive the response from WORK1  
-post the response data to nginx server WORK2

5. nginx server WORK2:  
-receive the request  
-process…  
-send back response

6. nginx server MASTER:  
-receive the response from WORK2  
-send back the response data to client

Can anyone help me on where to hack on nginx that can suit my scenario.

Regards,  
Vincent

---

<div class="post-metadata">

**Author:** ![vinjenace](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/d9b06d/32.png) [@vinjenace](https://www.ruby-forum.com/u/vinjenace)\
**Post date:** [July 16, 2010, 6:28am UTC](https://www.ruby-forum.com/t/further-hacking-the-event-model-of-nginx/191684/2 "2010-07-16T06:28:48Z")

</div>

you really need to specify more details about what you are doing. is  
that HTTP? what are you processing, maybe you don’t need to hack  
anything, depending on what you do with the request…

---

<div class="post-metadata">

**Author:** ![vinjenace](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/d9b06d/32.png) [@vinjenace](https://www.ruby-forum.com/u/vinjenace)\
**Post date:** [July 16, 2010, 6:50am UTC](https://www.ruby-forum.com/t/further-hacking-the-event-model-of-nginx/191684/3 "2010-07-16T06:50:50Z")

</div>

Mauro Stettler wrote:

> you really need to specify more details about what you are doing. is  
> that HTTP? what are you processing, maybe you don’t need to hack  
> anything, depending on what you do with the request…  
> Thanks for your help;-)  
> yes, it is HTTP. Our business logic is split into multiple tasks, they  
> are deployed on multiple backend servers. We have a master server  
> visible to the client. The master server need read client post data  
> ,modify the data and send to backend server1, the backend server1 also  
> have nginx running, it should do a time consuming task. The master  
> server received the response from backend server1 and send back to  
> backend server2. backend server2 also do a time consuming task. the  
> master server received the response and send back the client.  
> By now, I have the following design:

the master server handle the client request, it will modify the post  
body and bypass request to backend server1.(I may need to learn code in  
proxy\_module and fastcgi\_module)  
problem:  
How can I hook the response send back from backend server1? Can I use  
body filter module so that I can get the response body and send to  
backend server2 again?

the backend server logic is simple, it will invoke functions in dll.  
problem:  
the functions in dll is time consuming, Will it block the nginx?

Thanks again;-)

---

<div class="post-metadata">

**Author:** ![vinjenace](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/d9b06d/32.png) [@vinjenace](https://www.ruby-forum.com/u/vinjenace)\
**Post date:** [July 16, 2010, 7:10am UTC](https://www.ruby-forum.com/t/further-hacking-the-event-model-of-nginx/191684/4 "2010-07-16T07:10:22Z")

</div>

to be able to receive the request of one backend, process it, and then  
go to the next one, i think this will help you:

> **[GitHub - vkholodkov/nginx-eval-module: A module for evaluating memcached or...](https://github.com/vkholodkov/nginx-eval-module)**
>
> A module for evaluating memcached or proxy response into variable - GitHub - vkholodkov/nginx-eval-module: A module for evaluating memcached or proxy response into variable

for getting the post value this might help:

> **[GitHub - calio/form-input-nginx-module: This is a nginx module that reads...](https://github.com/calio/form-input-nginx-module)**
>
> This is a nginx module that reads HTTP POST and PUT request body encoded in "application/x-www-form-urlencoded", and parse the arguments in request body into nginx variables. - Gi...

---

<div class="post-metadata">

**Author:** ![vinjenace](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/d9b06d/32.png) [@vinjenace](https://www.ruby-forum.com/u/vinjenace)\
**Post date:** [July 16, 2010, 8:46am UTC](https://www.ruby-forum.com/t/further-hacking-the-event-model-of-nginx/191684/5 "2010-07-16T08:46:33Z")

</div>

Mauro Stettler wrote:

> to be able to receive the request of one backend, process it, and then  
> go to the next one, i think this will help you:  
> [GitHub - vkholodkov/nginx-eval-module: A module for evaluating memcached or proxy response into variable · GitHub](http://github.com/vkholodkov/nginx-eval-module)
> 
> for getting the post value this might help:
> 
> [GitHub - calio/form-input-nginx-module: This is a nginx module that reads HTTP POST and PUT request body encoded in "application/x-www-form-urlencoded", and parse the arguments in request body into nginx variables. · GitHub](http://github.com/calio/form-input-nginx-module)

Thanks for your kind help;-)

I have download and looked into nginx-eval-module. It seems that the  
module can send sub request to backend only once. when the response is  
sent back from the backend, the module only can modify the response  
body…(maybe I should try remove the response from the body chain and  
send to another backend, I will try this;-) )

The task to extract post value/head is finished. The remaining problem  
is that my handler is time consuming and it may block nginx work  
process, I need to try the way in ["Hacking" the event model of Nginx - NGINX - Ruby-Forum](http://www.ruby-forum.com/topic/149417)

Thanks again;-)

---

<div class="post-metadata">

**Author:** ![vinjenace](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/d9b06d/32.png) [@vinjenace](https://www.ruby-forum.com/u/vinjenace)\
**Post date:** [July 16, 2010, 9:25am UTC](https://www.ruby-forum.com/t/further-hacking-the-event-model-of-nginx/191684/6 "2010-07-16T09:25:33Z")

</div>

Hey,

Wouldn’t [http://wiki.nginx.org/NginxXSendfile](http://wiki.nginx.org/NginxXSendfile) work for bouncing your  
request between servers without modifying source code (thats how I do  
it) ?

Cheers,  
Johan

---

<div class="post-metadata">

**Author:** ![vinjenace](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/d9b06d/32.png) [@vinjenace](https://www.ruby-forum.com/u/vinjenace)\
**Post date:** [July 16, 2010, 9:52am UTC](https://www.ruby-forum.com/t/further-hacking-the-event-model-of-nginx/191684/7 "2010-07-16T09:52:29Z")

</div>

Hello,

On 16 jul 2010, at 09.33, Vincent Huang wrote:

> Thanks for your help.  
> I need hold the response from one backend and send it to another(not  
> directory use the backed response as the result to client), so I have to  
> write some code inside nginx…  
> Thanks again;-)

The example might be a bit misleading in your case. What  
X-Accel-Redirect does is take precedence over the location uri. You  
don’t specifically have to pass the location match to a directory for  
downloads, you might just as well pass it along to another backend.

This is how my setup looks like:

- incoming get /foo
- location match on /foo that passes to fcgi backend
- fcgi backend analyzes request and returns response with  
x-accel-redirect /backend1/foo
- location match on /backend1 passes the request along to http backend
- backend responds with “final” request, master sends back to client

---

<div class="post-metadata">

**Author:** ![vinjenace](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/d9b06d/32.png) [@vinjenace](https://www.ruby-forum.com/u/vinjenace)\
**Post date:** [July 16, 2010, 10:41am UTC](https://www.ruby-forum.com/t/further-hacking-the-event-model-of-nginx/191684/8 "2010-07-16T10:41:11Z")

</div>

Johan BergstrÃ¶m wrote:

> Hello,
> 
> On 16 jul 2010, at 09.33, Vincent Huang wrote:
> 
> > Thanks for your help.  
> > I need hold the response from one backend and send it to another(not  
> > directory use the backed response as the result to client), so I have to  
> > write some code inside nginx…  
> > Thanks again;-)
> 
> The example might be a bit misleading in your case. What  
> X-Accel-Redirect does is take precedence over the location uri. You  
> don’t specifically have to pass the location match to a directory for  
> downloads, you might just as well pass it along to another backend.
> 
> This is how my setup looks like:
> 
> - incoming get /foo
> - location match on /foo that passes to fcgi backend
> - fcgi backend analyzes request and returns response with  
> x-accel-redirect /backend1/foo
> - location match on /backend1 passes the request along to http backend
> - backend responds with “final” request, master sends back to client

X-Accel-Redirect only works for intern redirect(just like what  
ngx\_http\_subrequest(…)). My backend is another remote server(it should  
like what ngx\_http\_upstream\_t done in proxy/fcgi module).  
Thanks for your help;-)

Regard,  
Vincent

---

<div class="post-metadata">

**Author:** ![vinjenace](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/d9b06d/32.png) [@vinjenace](https://www.ruby-forum.com/u/vinjenace)\
**Post date:** [July 16, 2010, 9:33am UTC](https://www.ruby-forum.com/t/further-hacking-the-event-model-of-nginx/191684/9 "2010-07-16T09:33:46Z")

</div>

Johan BergstrÃ¶m wrote:

> Hey,
> 
> Wouldn’t [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/NginxXSendfile) work for bouncing your  
> request between servers without modifying source code (thats how I do  
> it) ?
> 
> Cheers,  
> Johan

Thanks for your help.  
I need hold the response from one backend and send it to another(not  
directory use the backed response as the result to client), so I have to  
write some code inside nginx…  
Thanks again;-)

Regards,  
Vincent

---

<div class="post-metadata">

**Author:** ![vinjenace](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/d9b06d/32.png) [@vinjenace](https://www.ruby-forum.com/u/vinjenace)\
**Post date:** [July 17, 2010, 8:58am UTC](https://www.ruby-forum.com/t/further-hacking-the-event-model-of-nginx/191684/10 "2010-07-17T08:58:10Z")

</div>

Hello!

On Fri, Jul 16, 2010 at 10:41:11AM +0200, Vincent Huang wrote:

> > - location match on /backend1 passes the request along to http backend
> > - backend responds with “final” request, master sends back to client
> 
> X-Accel-Redirect only works for intern redirect(just like what  
> ngx\_http\_subrequest(…)). My backend is another remote server(it should  
> like what ngx\_http\_upstream\_t done in proxy/fcgi module).  
> Thanks for your help;-)

Yes, X-Accel-Redirect does internal redirect. No, internal  
redirects isn’t limited to local files, they may be easily handled  
by proxy/fastcgi modules as well. E.g. in configuration like  
this:

```
location /back1 {
    proxy_pass http://back1;
}

location /back2
    proxy_pass http://back2;
}

```

backends may easily bounce request from one to another via  
X-Accel-Redirect.

Maxim D.

---

<div class="post-metadata">

**Author:** ![vinjenace](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/d9b06d/32.png) [@vinjenace](https://www.ruby-forum.com/u/vinjenace)\
**Post date:** [March 9, 2012, 3:15am UTC](https://www.ruby-forum.com/t/further-hacking-the-event-model-of-nginx/191684/11 "2012-03-09T03:15:07Z")

</div>

Hi vincent,

Were you able to get to do what you wanted in comment #1?

I was wondering if you would happen to have some sample code or  
pointers, that you wouldn’t mind sharing?

I have a similar requirement, and have asked about the same here:  
[http://www.ruby-forum.com/topic/3809975](http://www.ruby-forum.com/topic/3809975)

Thanks,  
Ashish
