logging request reponse header to nginx - xiuyanduan/xiuyanduan.github.io GitHub Wiki

title: logging request and response header to nginx access log
date: 2016-02-22
tags:
- nginx
---

log_format conf

resonse header

$upstream_http_resonseHeaderName

request header

$http_requestHeaderName

demo of log_format

log_format addHeaderlog '$remote_addr - $remote_user [$time_local] '
                    '"$request" $status $body_bytes_sent '
                    '"$http_referer" "$http_user_agent" "$http_x_forwarded_for" "$request_body" "$http_Authorization" "$http_x_duid" "$http_x_ver" "$upstream_http_x_rqid"';

reference

nginx forward nginx

conf of server nginx

/etc/nginx/sites-enabled/back.conf

server {
    listen 999;
    root /usr/share/nginx/html;
    index index.html index.htm;

    access_log /var/log/nginx/back.log addHeaderlog ;
    
    add_header x-rqid testXRqidResponse;
	# Make site accessible from http://localhost/
    server_name 10.10.10.47;

    location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;
		# Uncomment to enable naxsi on this location
		# include /etc/nginx/naxsi.rules
	}

}

conf of balance nginx

/etc/nginx/sites-enabled/front.conf

upstream localhost_django{
#    ip_hash;
#    server 127.0.0.1:80;
    server 10.10.10.37:999;

}
server {
    listen 99;
    server_name 10.10.10.37;
    access_log /var/log/nginx/front-default.log client;
    access_log /var/log/nginx/front-addHeaders.log addHeaderlog ;



location / { 
            root  html; 
            index  index.html index.htm; 
            proxy_pass http://localhost_django; 
}}

logging POST request

add $request_body in log_format