Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

next/image returns 400 Bad Request from /_next/image

Error400 Bad Request

Short answer

The /_next/image endpoint answers 400 when the request breaks an allow-list in next.config: a remote URL not matched by remotePatterns, a local path not matched by localPatterns, a quality not listed in qualities, or a source on a local IP. Match the failing URL against the config, then widen only that rule.

  • Applies to Next.js 15, Next.js 16
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the image request in the network tab and copy the url and q parameters. These are the values the optimizer rejected.

    curl -I "http://localhost:3000/_next/image?url=%2Fphoto.jpg&w=640&q=75"
  2. 2Match what you see, then jump to its fix

    1. 1The src is an https URL and protocol, host, port, path or query differs from the configured pattern.The remote URL is not matched by remotePatternsFix
    2. 2The src starts with / and the config defines localPatterns.A local image path is blocked by localPatternsFix
    3. 3The failing request has a q value other than 75, often when you call the endpoint directly.The quality is not in the qualities listFix
    4. 4Self-hosted behind a VPC or split-horizon DNS, and the source host resolves to a local address.The image host resolves to a private IPFix

What does the error look like?

  • 400 Bad Request
  • GET /_next/image?url=... 400
  • 400 bad request from next/image

Cause 1 of 4

The remote URL is not matched by remotePatterns

1Check

Any other protocol, hostname, port or unmatched path answers 400. A pattern with a search value also needs the exact query string.

grep -n -A8 "remotePatterns" next.config.*

2Fix

Add a pattern that matches the real URL.

// next.config.ts
const nextConfig = {
  images: {
    remotePatterns: [
      {
        protocol: 'https',
        hostname: 'assets.example.com',
        pathname: '/account123/**',
      },
    ],
  },
}
export default nextConfig

3Verify

Restart the server and reload the page; the request returns 200.

npm run dev

Confirmed by nextjs.org

Did this fix it?

Cause 2 of 4

A local image path is blocked by localPatterns

1Check

When localPatterns is set, any other local path, or one with a query string where search is empty, answers 400.

grep -n -A8 "localPatterns" next.config.*

2Fix

Add the folder the image lives in, or fix the src so it sits under an allowed path.

images: {
  localPatterns: [
    { pathname: '/assets/images/**', search: '' },
  ],
},

3Verify

Reload; the image loads from the optimizer.

curl -I "http://localhost:3000/_next/image?url=%2Fassets%2Fimages%2Fa.png&w=640&q=75"

Confirmed by nextjs.org

Did this fix it?

Cause 3 of 4

The quality is not in the qualities list

1Check

Next.js 16 requires qualities and defaults it to [75]. A direct request with a quality outside the list gets 400. The quality prop on the component is coerced to the closest allowed value instead.

grep -n -A3 "qualities" next.config.*

2Fix

List every quality you use.

images: {
  qualities: [50, 75, 90],
},

3Verify

Request the image with q=90 again.

curl -I "http://localhost:3000/_next/image?url=%2Fphoto.jpg&w=640&q=90"

Confirmed by nextjs.org

Did this fix it?

Cause 4 of 4

The image host resolves to a private IP

1Check

By default optimizing images from local IP addresses is off. The docs name this as a possible reason for a 400 inside a VPC.

getent hosts assets.example.com

2Fix

Serve the image publicly, or turn on dangerouslyAllowLocalIP only once you accept the SSRF risk. If the image needs authentication, use unoptimized on that image.

images: {
  dangerouslyAllowLocalIP: true,
},

3Verify

Restart and request the image again.

npm run start

Confirmed by nextjs.org

Did this fix it?

How do I stop it coming back?

  • Keep remotePatterns specific: protocol, host and a path prefix.
  • Use an exact search value in patterns instead of leaving it out.
  • Skip optimization for small, SVG or animated images with unoptimized.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.