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