next/image shows nothing for an image from another domain
400 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.
Fix it fast
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'2Match what you see, then jump to its fix
- 1
400 Bad Request for any image whose src starts with http(s)://The host is missing from remotePatternsFix - 2
Other 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
400 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
- 1
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 startConfirmed by nextjs.org
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
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.example2Fix
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 startConfirmed by nextjs.org
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
Web apps
next/image fails with The requested resource isn't a valid image
The requested resource isn't a valid image for /public/logo.png received text/html; charset=utf-8
The image optimizer asked for the file and got an HTML page back, usually a 404 page or a redirect to a login.
2 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
Clerk cannot read its publishable key in a Next.js app
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY is undefined
Clerk reads NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY.
3 causes, each with a fixWeb apps
The shell cannot find create-next-app
command not found: create-next-app
create-next-app is not meant to be installed as a shell command.
2 causes, each with a fixWeb apps
ESLint fails with: Cannot find module 'next/babel'
ESLint stops on the first file with a Babel or parser error instead of listing lint results
next/babel is a Babel preset that ships inside the next package.
3 causes, each with a fixWeb apps
Firebase Auth says the API key is not valid
Firebase: Error (auth/api-key-not-valid.-please-pass-a-valid-api-key.)
The apiKey your app sends is either empty, wrong, or blocked.
4 causes, each with a fix