<p>I'm using the nginx ingress controller on gke, by default these are what my access logs look like:</p>
<blockquote>
<p>&quot;10.123.0.20 - [10.123.0.20] - - [22/Apr/2019:18:47:59 +0000] &quot;GET /sdflksdf/sdfsdf HTTP/2.0&quot; 404 0 &quot;-&quot; &quot;Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/538.12 (KHTML, like Gecko) Chrome/73.0.3683.100 Safari/537.36&quot; 26 0.002 [default-blah-80] 10.44.0.26:80 0 0.001 404 skjf0s93jf0ws93jfsijf3s3fjs3i</p>
</blockquote>
<p>I want to add the x-forwarded-for header in my access logs. I'd like that field to be added at the end of the current log lines if possible. Or at the start of the log line would be OK too I guess.</p>
<p>Im looking at their docs and its not clear to me how to add x-forwarded-for to the access log:
<a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/log-format/" rel="nofollow noreferrer">https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/log-format/</a></p>

<p>You should use a ConfigMap to <a href="https://kubernetes.github.io/ingress-nginx/examples/customization/custom-configuration/" rel="noreferrer">customize the NGINX configuration</a>:</p>

<blockquote>
  <p><a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/" rel="noreferrer">ConfigMaps</a> allow you to decouple configuration artifacts from image
  content to keep containerized applications portable.</p>
  
  <p>The ConfigMap API resource stores configuration data as key-value
  pairs. The data provides the configurations for system components for
  the nginx-controller.</p>
</blockquote>

<p><strong>To configure custom logs, you need to use the <a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/#log-format-upstream" rel="noreferrer"><code>log-format-upstream</code></a> key.</strong></p>

<p><strong>e.g.:</strong></p>

<p>Create the following configmap:</p>

<pre><code>apiVersion: v1
data:
  log-format-upstream: '$remote_addr - $request_id - [$proxy_add_x_forwarded_for] - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_length $request_time [$proxy_upstream_name] $upstream_addr $upstream_response_length $upstream_response_time $upstream_status'
kind: ConfigMap
metadata:
  name: nginx-ingress-config
</code></pre>

<p>and make sure that you are using <code>--configmap=$(POD_NAMESPACE)/nginx-ingress-config</code> as command <code>args</code> for your nginx-ingress-controller (<a href="https://github.com/kubernetes/ingress-nginx/blob/e68b68d20cfc0e083f6b33413bc27887a05985f5/deploy/with-rbac.yaml#L30" rel="noreferrer">example from offical repo here</a>).</p>
