Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx: Is it possible to capture response headers in access log when using nginx as a reverse proxy?

We are using nginx as a reverse proxy to control and log access to a Clojure (Java) web service application.

We are able to generate an access_log and capture incoming headers using nginx just fine. Our Clojure app logs activity via log4j. Trouble is, is that we can't match an entry in the access_log to an entry generated by the app.

The app responds to access by sending response headers as well as a body. We can freely change these response headers. My initial thought was to generate a UUID that corresponds to each and every web service request and send that back to the user within the reply header X-Uuid. My thought was that I could capture this response by creating a custom log_format:

log_format lt-custom '$remote_addr - $remote_user [$time_local]  '
                     '"$request" $status $body_bytes_sent '
                     '"$http_referer" "$http_user_agent" $request_time $http_x_uuid';

It's looking like nginx can capture headers in incoming requests but not outgoing replies (I verified this by replacing $http_x_uuid with $http_content_type).

So! Is there a way I can tie my access_log enties and my log4j entries by capturing outgoing reply headers using nginx? Is there a better way? I'd rather not have to rely on users generating their own UUIDs.

Thanks so much!

like image 591
jkndrkn Avatar asked Nov 25 '10 00:11

jkndrkn


People also ask

How do I configure logging in Nginx?

Configuring Logging 1 Setting Up the Error Log. NGINX writes information about encountered issues of different severity levels to the error log. ... 2 Setting Up the Access Log. ... 3 Enabling Conditional Logging. ... 4 Usecase: Sampling TLS Parameters. ... 5 Logging to Syslog. ... 6 Live Activity Monitoring. ...

How do Nginx proxies work?

When NGINX proxies a request, it sends the request to a specified proxied server, fetches the response, and sends it back to the client.

How are Nginx proxied requests distributed among the servers?

In this case, requests are distributed among the servers in the group according to the specified method. By default, NGINX redefines two header fields in proxied requests, “Host” and “Connection”, and eliminates the header fields whose values are empty strings. “Host” is set to the $proxy_host variable, and “Connection” is set to close.

What is the default header for pass request in Nginx?

Passing Request Headers. By default, NGINX redefines two header fields in proxied requests, “Host” and “Connection”, and eliminates the header fields whose values are empty strings. “Host” is set to the $proxy_host variable, and “Connection” is set to close.


1 Answers

$http_x_uuid is header sent by client. Response header send by upstream is $upstream_http_x_uuid

http://wiki.nginx.org/HttpUpstreamModule#.24upstream_http_.24HEADER

like image 71
citrin Avatar answered Oct 06 '22 23:10

citrin