next/image returns 400 Bad Request from /_next/image
400 Bad RequestShort 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.
Fix it fast
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"2Match what you see, then jump to its fix
- 1
The 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
The src starts with / and the config defines localPatterns.A local image path is blocked by localPatternsFix - 3
The 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
Self-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
- 1
What does the error look like?
400 Bad RequestGET /_next/image?url=... 400400 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 nextConfig3Verify
Restart the server and reload the page; the request returns 200.
npm run devConfirmed by nextjs.org
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
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
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.com2Fix
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 startConfirmed by nextjs.org
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
Web apps
AuthUserPoolException: Auth UserPool not configured in a Next.js app
Auth UserPool not configured
The error means an Amplify Auth call ran before Amplify.configure did.
3 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
ESLint says an element in a list is missing its key prop
react/jsx-key
react/jsx-key fires when an element returned from map, Array.from or an array literal has no key.
3 causes, each with a fix