Maaz Zindani

Loading the index…

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

next/image shows nothing for an image from another domain

Error400 Bad Request on /_next/image?url=https%3A%2F%2F...

Short answer

next/image only optimizes remote images that match an entry in images.remotePatterns in next.config. Any other protocol, hostname, port, path or query string gets a 400 Bad Request from the image route. Add a specific pattern for the host, redeploy, and check that the production build contains the same config.

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

Fix it fast

  1. 1Find out which cause you have

    Open the browser network tab, copy the failing /_next/image request and request it directly. The status code and the url= value show which host and path Next.js refused.

    curl -I 'https://your-site.example/_next/image?url=https%3A%2F%2Fcdn.example.com%2Fa%2Fphoto.jpg&w=640&q=75'
  2. 2Match what you see, then jump to its fix

    1. 1400 Bad Request for any image whose src starts with http(s)://The host is missing from remotePatternsFix
    2. 2Other images from the same host work; one fails, often a URL ending in ?v=123 or ?token=...The pattern exists but the path or query string does not matchFix
    3. 3400 Bad Request even though remotePatterns matches, and the host resolves to a local or VPC addressThe image host sits on a private network (self-hosted, split DNS)Fix

What does the error look like?

  • 400 Bad Request on /_next/image?url=https%3A%2F%2F...
  • next/image shows a broken image only for external src values

Cause 1 of 3

The host is missing from remotePatterns

1Check

Compare the full src URL with the images.remotePatterns list in next.config. Without a matching entry nothing remote is allowed.

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

2Fix

Add a pattern for the exact host. Be as narrow as you can on pathname.

// next.config.js
module.exports = {
  images: {
    remotePatterns: [
      {
        protocol: 'https',
        hostname: 'cdn.example.com',
        port: '',
        pathname: '/photos/**',
        search: '',
      },
    ],
  },
}

3Verify

Rebuild and reload the page. The /_next/image request should return 200.

npm run build && npm run start

Confirmed by nextjs.org

Did this fix it?

Cause 2 of 3

The pattern exists but the path or query string does not match

1Check

Look at the failing URL. A pattern with search: '' rejects any query string, and pathname must match the start of the path. Omitted fields mean the ** wildcard, which is less strict.

grep -n "pathname\|search" next.config.*

2Fix

Widen only what you need: a broader pathname glob, or the exact query string in search.

remotePatterns: [
  {
    protocol: 'https',
    hostname: 'assets.example.com',
    pathname: '/uploads/**',
    search: '?v=1727111025337',
  },
]

3Verify

Open the failing /_next/image URL again after the rebuild.

curl -I 'https://your-site.example/_next/image?url=https%3A%2F%2Fassets.example.com%2Fuploads%2Fa.jpg%3Fv%3D1727111025337&w=640&q=75'

Confirmed by nextjs.org

Did this fix it?

Cause 3 of 3

The image host sits on a private network (self-hosted, split DNS)

1Check

Next.js refuses to optimize images from local IP addresses by default. Check whether the hostname resolves to a private address from the server.

getent hosts images.internal.example

2Fix

Only if you understand the SSRF risk, allow local IPs. Prefer a public hostname for the image source.

module.exports = {
  images: {
    dangerouslyAllowLocalIP: true,
  },
}

3Verify

Restart the Next.js server and request the image again.

npm run build && npm run start

Confirmed by nextjs.org

Did this fix it?

How do I stop it coming back?

  • Add the remotePatterns entry in the same change that adds a new image host to the code.
  • Keep protocol, hostname and pathname specific instead of leaving them out.
  • Prefer remotePatterns over the deprecated domains option.

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.