---
slug: "nginx-log-request-headers"
title: "Outputting Request Headers in Nginx Logs"
description: "Output every request header and response info to the nginx access log via a custom `log_format`, with an Ansible playbook to deploy it."
url: "https://www.ytyng.com/en/blog/nginx-log-request-headers"
publish_date: "2020-05-06T14:24:27Z"
created: "2020-05-06T14:24:27Z"
updated: "2026-05-11T12:57:57.293Z"
categories: ["nginx"]
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20250602/9006f17a0d6e44ce9fc706bffe63f59a.png.webp?width=768"
has_video: true
has_music: true
video_urls: ["https://media.ytyng.net/ytyng-blog/171/featured-video-1.mp4", "https://media.ytyng.net/ytyng-blog/171/featured-video-2.mp4", "https://media.ytyng.net/ytyng-blog/171/featured-video-3.mp4"]
music_urls: ["https://media.ytyng.net/ytyng-blog/171/featured-music-171-5.mp3", "https://media.ytyng.net/ytyng-blog/171/featured-music-171-6.mp3"]
lang: "en"
---

# Outputting Request Headers in Nginx Logs

### log_format.conf
```plaintext
log_format header
  '[$time_local]\n'
  '$request\n'
  'request ------------\n'
  'remote_addr: $remote_addr\n'
  'remote_user: $remote_user\n'
  'request_time: $request_time\n'
  'host: $http_host\n'
  'referer: $http_referer\n'
  'accept_encoding: $http_accept_encoding\n'
  'accept_language: $http_accept_language\n'
  'user_agent: $http_user_agent\n'
  'x_forwarded_proto: $http_x_forwarded_proto\n'
  'x_forwarded_for: $http_x_forwarded_for\n'
  'x_forwarded_host: $http_x_forwarded_host\n'
  'x_real_ip: $http_x_real_ip\n'
  'response ------------\n'
  'status: $status\n'
  'size: $body_bytes_sent\n'
  'content_type: $sent_http_content_type\n';
```

### Ansible Playbook YAML
```yaml
- remote_user: ubuntu
  hosts: servers
  become: yes
  tasks:

    - copy:
      src: log_format.conf
      dest: /etc/nginx/conf.d/log_format.conf
      owner: root
      group: root
      mode: 0644

    - name: "Restart nginx"
      service:
      name: nginx
      state: restarted
```

### Nginx Server Configuration
```plaintext
server {
  listen 80 default;
  ...
  access_log /var/log/nginx/torico-id-header.log header;
}
```
