# Duplicating or forking incoming requests

**URL:** <https://www.ruby-forum.com/t/duplicating-or-forking-incoming-requests/245809>\
**Category:** NGINX\
**Created:** [July 12, 2013, 9:47pm UTC](https://www.ruby-forum.com/t/duplicating-or-forking-incoming-requests/245809 "2013-07-12T21:47:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadaf\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Post date:** [July 12, 2013, 9:47pm UTC](https://www.ruby-forum.com/t/duplicating-or-forking-incoming-requests/245809/1 "2013-07-12T21:47:43Z")

</div>

I’m trying to figure out how to accomplish something with nginx and it  
seems to have me baffled.

We use nginx as an endpoint to log incoming events. The default  
response is a 200, empty body and an entry in the access log in a very  
specific format. For legacy reasons, this will not change any time  
soon.

Now, in addition to just simply logging this request, we want to forward  
that request to an internal server to also do something with it (and  
again, for legacy reasons I can’t just make that server do the logging  
instead).

I can get this working fine, except for failure modes I specifically do  
not want the original nginx logging affected at all if it can’t proxy  
the request upstream, and in point of fact specifically want to return a  
200 in that case also. So basically the desired behavior looks like  
this:

nginx running  
proxy running  
log a successful incoming GET request to nginx access log  
forward request to proxy  
return 200

nginx running  
proxy not running (or returns an error)  
log a successful incoming GET request to nginx access log  
forward request to proxy  
proxy returns an error  
nginx ignores the error and returns a 200

I haven’t been able to get to that point, but instead have been only  
able to get various combinations of “nginx doesn’t log anything”, “nginx  
logs everything only when the entire chain is up” and “nginx logs  
incoming requests fine when chain is up but logs to the error log when  
the proxy is down”.

Here’s the configuration snippets I’m currently using:

location = /events {  
access\_log events\_access.log events\_format;  
expires 1s;  
try\_files @proxy @proxy;  
# try\_files @proxy /empty.html;  
# try\_files @proxy =200;  
}

location @proxy {  
proxy\_pass [http://127.0.0.1:8887](http://127.0.0.1:8887);  
proxy\_intercept\_errors on;  
access\_log events\_access.log events\_format;  
error\_page 502 =200 /empty.html;  
proxy\_set\_header X-Real-IP $remote\_addr;  
}

Basically, what I want is that if it logs every incoming request  
normally. If it can forward the request to the upstream proxy, it does  
so after logging it, and if it can’t, it simply logs it and returns a  
200.

Is this possible and if so how?

Thanks in advance!

– Gary F.

---

<div class="post-metadata">

**Author:** ![Gary\_Foster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a4c791/32.png) [@Gary\_Foster](https://www.ruby-forum.com/u/Gary_Foster)\
**Post date:** [July 12, 2013, 10:05pm UTC](https://www.ruby-forum.com/t/duplicating-or-forking-incoming-requests/245809/2 "2013-07-12T22:05:15Z")

</div>

Hello!

On Fri, Jul 12, 2013 at 12:47:02PM -0700, Gary Foster wrote:

[…]

> Here’s the configuration snippets I’m currently using:
> 
> location = /events {  
> access\_log events\_access.log events\_format;  
> expires 1s;  
> try\_files @proxy @proxy;  
> # try\_files @proxy /empty.html;  
> # try\_files @proxy =200;

You are trying to use try\_files incorrectly. Please read docs at  
[Module ngx\_http\_core\_module](http://nginx.org/r/try_files).

> Basically, what I want is that if it logs every incoming request  
> normally. If it can forward the request to the upstream proxy,  
> it does so after logging it, and if it can’t, it simply logs it  
> and returns a 200.
> 
> Is this possible and if so how?

I would recommend something like this:

```
location = /events {
    access_log events_access.log events_format;
    error_page 502 504 = /empty;
    proxy_pass ...;
}

location = /empty {
    access_log events_access.log events_format;
    return 200 "";
}

```

Note the above configuration snippet doesn’t try to intercept  
errors returned by upstream servers, but only handles cases when  
nginx can’t reach them and/or an invalid response is returned. If  
upstream servers are expected to return various errors,  
proxy\_intercept\_errors should be used, as well as additional codes  
in error\_page.

–  
Maxim D.

> **[nginx: donation](http://nginx.org/en/donation.html)**

---

<div class="post-metadata">

**Author:** ![Gary\_Foster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a4c791/32.png) [@Gary\_Foster](https://www.ruby-forum.com/u/Gary_Foster)\
**Post date:** [July 12, 2013, 11:01pm UTC](https://www.ruby-forum.com/t/duplicating-or-forking-incoming-requests/245809/3 "2013-07-12T23:01:55Z")

</div>

Thanks, that did the trick exactly!

Now that I have something that works, I’m off to the docs to figure out  
where my fundamental misunderstandings were and correct them. Very much  
appreciated!

– Gary F.
