Nginx log request and response API Proxy

Solution 1:

OP, Welcome to Server Fault! As an FYI, listen to @MichaelHampton’s advice and triple check your instincts. In this case the access log really does provide you with the information you likely want. You just have to tell it what it is that you want logged.

To expand on M Hampton’s suggestion, what you should do is define your own log format for upstream/proxy logging and then add the access_log directive to the location from which you are doing proxy_pass using your custom log format.

location ~ /api/(?<path>.*) {
    log_format upstream_logging . . .;
    . . .
    access_log /var/log/nginx/api_logging.log upstream_logging;
}

And you can include whatever variables from nginx you want to in your log format. You will have to specify which headers you want individually from the upstream using the variables here, for example $upstream_http_server. Here is one proposed logging format for logging upstream information that you can use as a starting point.

log_format upstream_logging '[$time_local] $remote_addr - $remote_user - $server_name to: $upstream_addr: $request upstream_response_time $upstream_response_time msec $msec request_time $request_time';

A little bit of googling and reading the docs (and listening to M Hampton!) will take you a long way. Good luck!