title: How to cache NextJS 10.0 images using NGINX
body: <p>We would like to launch a NextJS 10 app using NGINX so we use a configuration similar to:</p>
  <pre><code>location /_next/static/ {
      alias /home/ec2-user/my-app/.next/static/;
      expires 1y;
      access_log on;
  }
  </code></pre>
  <p>It works great, it caches for a year our statics but as we use <a href="https://nextjs.org/docs/api-reference/next/image" rel="nofollow noreferrer">NextJS images</a> I'm failing to add an <code>expires</code> tag on on-the-fly resized images.</p>
  <p>If I do:</p>
  <pre><code>location /_next/image/ {
      alias /home/ec2-user/my-app/.next/image;
      expires 1y;
      access_log on;
  }
  </code></pre>
  <p>It just returns a 404 on images.</p>
  <p>Here is my server part NGINX config :</p>
  <pre><code>server {
      listen 80;
      server_name *.my-website.com;
      # root        /usr/share/nginx/html;
      # root /home/ec2-user/my-app;
      charset     utf-8;
  
      client_max_body_size    20M;
      client_body_buffer_size 20M;
  
      proxy_connect_timeout    600;
      proxy_send_timeout       600;
      proxy_read_timeout       600;
      send_timeout             600;
  
      underscores_in_headers on;
  
      add_header X-Frame-Options SAMEORIGIN always;
      add_header X-Content-Type-Options nosniff always;
      add_header X-XSS-Protection &quot;1; mode=block&quot; always;
      add_header Referrer-Policy &quot;same-origin&quot; always;
  
      location = /robots.txt {
          proxy_pass https://api.my-website.com/robots.txt;
      }
  
      location /_next/static/ {
          alias /home/ec2-user/my-app/.next/static/;
          expires 1y;
          access_log on;
      }
  
      location / {
          # reverse proxy for merchant next server
          proxy_pass http://localhost:3000;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection 'upgrade';
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_pass_request_headers      on;
          proxy_cache_bypass $http_upgrade;
          proxy_buffering off;
      }
  }
  </code></pre>