From b9ac9fb18c2136ba3da93e08d1abe58c1e05865a Mon Sep 17 00:00:00 2001 From: Anna Sirota Date: Thu, 5 Oct 2023 11:41:22 +0200 Subject: [PATCH] [Ansible] add upstreaminfo log format to nginx Adding response times to access logs in a format already supported by Vector. See https://master.vector.dev/docs/reference/vrl/functions/#parse_nginx_log and https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/log-format/ for more information. --- .../tasks/nginx/add_upstreaminfo_log_format.yaml | 14 ++++++++++++++ playbooks/setup_certificate.yaml | 2 ++ .../nginx/conf.d/log-format-upstreaminfo.conf | 5 +++++ playbooks/templates/nginx/https.conf | 2 +- 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 playbooks/common/tasks/nginx/add_upstreaminfo_log_format.yaml create mode 100644 playbooks/templates/nginx/conf.d/log-format-upstreaminfo.conf diff --git a/playbooks/common/tasks/nginx/add_upstreaminfo_log_format.yaml b/playbooks/common/tasks/nginx/add_upstreaminfo_log_format.yaml new file mode 100644 index 00000000..2c279b9d --- /dev/null +++ b/playbooks/common/tasks/nginx/add_upstreaminfo_log_format.yaml @@ -0,0 +1,14 @@ +--- +- name: "Writing {{ conf_d }}{{ conf_f }}" + vars: + conf_d: /etc/nginx/conf.d/ + conf_f: log-format-upstreaminfo.conf + ansible.builtin.template: + src: "templates/nginx/conf.d/{{ conf_f }}" + dest: "{{ conf_d }}{{ conf_f }}" + backup: true + mode: 0664 + tags: + - nginx + - config + - log_format diff --git a/playbooks/setup_certificate.yaml b/playbooks/setup_certificate.yaml index 5f085f28..919f6170 100644 --- a/playbooks/setup_certificate.yaml +++ b/playbooks/setup_certificate.yaml @@ -25,6 +25,8 @@ - import_tasks: tasks/copy_error_pages.yaml + - import_tasks: common/tasks/nginx/add_upstreaminfo_log_format.yaml + - name: Copying nginx config ansible.builtin.template: src: templates/nginx/https.conf diff --git a/playbooks/templates/nginx/conf.d/log-format-upstreaminfo.conf b/playbooks/templates/nginx/conf.d/log-format-upstreaminfo.conf new file mode 100644 index 00000000..d3821c11 --- /dev/null +++ b/playbooks/templates/nginx/conf.d/log-format-upstreaminfo.conf @@ -0,0 +1,5 @@ +log_format upstreaminfo + '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" "$http_user_agent" ' + '$request_length $request_time [upstream-{{ env }}-service-1234] [upstream-{{ env }}-service-1234] $upstream_addr ' + '$upstream_response_length $upstream_response_time $upstream_status $request_id'; diff --git a/playbooks/templates/nginx/https.conf b/playbooks/templates/nginx/https.conf index 7684ee75..e1bb689f 100644 --- a/playbooks/templates/nginx/https.conf +++ b/playbooks/templates/nginx/https.conf @@ -21,7 +21,7 @@ server { {% endfor %} deny all; {% endif %} - access_log /var/log/nginx/{{ domain }}-access.log; + access_log /var/log/nginx/{{ domain }}-access.log upstreaminfo; error_log /var/log/nginx/{{ domain }}-error.log; #location /robots.txt { -- 2.30.2