I have a reverse proxy setup with nginx.
Client ------> Nginx ------------------------------------> Backend Server
<------ <-----------------------------------
(I want to see the requests here)
How can I log the http requests including headers sent from the backend server to nginx into a file?
Maybe one of the directives in nginx http proxy module can help me do this.
But I cannot find any helpful directives.
The servers that Nginx proxies requests to are known as upstream servers. Nginx can proxy requests to servers that communicate using the http(s), FastCGI, SCGI, and uwsgi, or memcached protocols through separate sets of directives for each type of proxy.
$request_time. request processing time in seconds with a milliseconds resolution; time elapsed between the first bytes were read from the client and the log write after the last bytes were sent to the client.
Note: I gave this answer before the OP added tag openresty
. I am not an expert in Openresty, but I am sure I gave correct details in case of vanilla Nginx.
A "backend server" is called "upstream" in Nginx terminology. And there is a separate page in the documentation that lists variables associated w/ upstreams. Among them, you will probably be interested in
$upstream_addr
– IP address of an backend server that processed the request$upstream_connect_time
, $upstream_header_time
, $upstream_response_time
– how long Nginx waited for connection accept, for header from the upstream and how long did it take for the backend to process the request$upstream_http_*NAME*
will contain the response header *NAME*
. For example, $upstream_http_etag
or $upstream_http_last_modified
.It's quite common practice to log these into Nginx log file. You will need to declare your own log format, for example:
log_format my_upstream '$remote_addr [$time_local] "$request" $status'
'"$upstream_addr" $upstream_response_time $upstream_http_etag';
and then use it in location
or server
:
access_log /var/log/nginx/upstream.log my_upstream;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With