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