<p>I'm hoping someone can help me here because I'm stuck. </p>

<p>I'm moving over our nginx config from a traditional nginx/node server config whereby both nginx and node server are on the same machine.</p>

<p>In Kubernetes, the ingress controller (nginx) obviously lives in another container. </p>

<p>Where I'm getting stuck is reimplementing our rules that disable access logging for images and assets using location blocks.</p>

<p>Our configuration looks like </p>

<pre><code>location ~* \.(?:jpg|jpeg|gif|png|ico|cur|mp4|ogg|ogv|webm|htc)$ {
  access_log off;
  expires 2M;
  add_header Cache-Control "public, max-age=5184000"; # 5184000 is 60 days
}
</code></pre>

<p>When I implement this same block in a <code>server-snippet</code> it matches, but all the assets throw a 404. </p>

<p>I did some Googling and found an answer that might explain why here <a href="https://stackoverflow.com/a/52711388/573616">https://stackoverflow.com/a/52711388/573616</a></p>

<p>but the suggested answer hints to use an  <code>if</code> block instead of a <code>location</code> block because the location interferes with the proxy upstream, however, disabling access logs is not possible from inside the <code>if</code> block,  only from a <code>location</code> context.</p>

<p>The rest of my ingress looks like (everything else is default)</p>

<pre><code>real_ip_header X-Forwarded-For;
real_ip_recursive on;        
underscores_in_headers on;
gzip_types text/css application/x-javascript application/javascript application/json image/svg+xml;
client_max_body_size 5M;

proxy_buffers 8 16k;      
proxy_set_header X-Request-Start "t=${msec}";   
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_redirect off;
</code></pre>

<p>The images live at /images/ on the upstream server path.</p>

<p>So I'm back to trying to figure out how to get these location blocks working so I can actually disable the access logs for these images from a <code>server-snippet</code></p>

<p>So can anyone tell me how to get the above location block to not throw 404's for assets in an ingress controller?</p>

Cheyne
<p>I'm assuming that your backend is serving your assets, so I think the problem is that your <code>location {}</code> block doesn't have an upstream like the regular paths defined in the nginx ingress. </p>

<p>There's a lot of lua code in the <code>nginx.conf</code> of your nginx-ingress-controller so it might take time to understand, but you can copy your <code>nginx.conf</code> locally:</p>

<pre><code>$ kubectl cp nginx-ingress-controller-xxxxxxxxx-xxxxx:nginx.conf .
</code></pre>

<p>Check the <code>location {}</code> blocks that are defined for your current services and copy them in the bottom of your <code>server-snippet</code> <code>location {}</code> block like this:</p>

<p>I believe a <code>server-snippet</code> like this would do:</p>

<pre><code>location ~* \.(?:jpg|jpeg|gif|png|ico|cur|mp4|ogg|ogv|webm|htc)$ {
  access_log off;
  expires 2M;
  add_header Cache-Control "public, max-age=5184000"; # 5184000 is 60 days
  &lt;== add what you copied here
  set $namespace      "k8s-namespace";
  set $ingress_name   "ingress-name";
  set $service_name   "service-name";
  set $service_port   "80";
  set $location_path  "/images";
  ...
  ...
  ...
  proxy_http_version                      1.1;

  proxy_cookie_domain                     off;
  proxy_cookie_path                       off;

  # In case of errors try the next upstream server before returning an error
  proxy_next_upstream                     error timeout;
  proxy_next_upstream_tries               3;

  proxy_pass http://upstream_balancer;

  proxy_redirect                          off;
}
</code></pre>
