Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx monitoring response from upstream server

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.

like image 415
Brian Avatar asked Jul 15 '17 04:07

Brian


People also ask

What does upstream do in NGINX?

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.

What is NGINX Request_time?

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


1 Answers

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.
  • there are other variables to report caching status, retries, etc.

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;
like image 79
Alexander Azarov Avatar answered Sep 21 '22 00:09

Alexander Azarov