next.js: next/image "url" parameter is valid but upstream response is invalid

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: win32
      Arch: x64
      Version: Windows 10 Pro
    Binaries:
      Node: 18.15.0
      npm: 9.5.0
      Yarn: N/A
      pnpm: N/A
    Relevant Packages:
      next: 13.4.14-canary.0
      eslint-config-next: 13.4.7
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.3
    Next.js Config:
      output: N/A

Which area(s) of Next.js are affected? (leave empty if unsure)

Image optimization (next/image, next/legacy/image)

Link to the code that reproduces this issue or a replay of the bug

https://github.com/luca-vogels/bug-next-image

To Reproduce

Run npm run dev or npm run build && npm run start

Describe the Bug

When using next with a custom server like express images are not displayed anymore.

Default next image uses following route for fetching optimized images: /_next/image/?url=...&w=...&q=...

However opening this route returns following error: "url" parameter is valid but upstream response is invalid

Expected Behavior

Optimized image is returned

Which browser are you using? (if relevant)

115.0.5790.111

How are you deploying your application? (if relevant)

npm run start

NEXT-1681

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 31
  • Comments: 76 (6 by maintainers)

Commits related to this issue

Most upvoted comments

I found out that this error still exist in 13.5.6. How can I fix this?

Also randomly experiencing this issue. 13.4.9 fixed it for me at the moment… This needs to be a high priority issue if not already. It’s been over a month. Not sure how a company can push so many issues in production releases.

this was broken on 13.5.3 for me but with 13.5.4 seems like it’s fixed

It is still not fixed yet. I upgraded to version 13.5.4 but still have the image error as above. Version 13.5.4 will have the following error message: UND_ERR_INVALID_ARG

For me without any hack, I simply used https://nextjs.org/docs/messages/sharp-missing-in-production by adding

yarn add sharp

And the issue is gone.

I’ve encountered a similar issue in the project I’m working on. It occurred when I attempted the following version upgrade:

- "next": "13.4.12",
+ "next": "13.4.13",

Yeah, I confirm that versions above v13 don’t work. Only v12 doesn’t have problems with the images.

The same issue seems to occur when upgrading from:

- "next": "13.5.2",
+ "next": "13.5.3",

I’m experiencing the same issue too in NextJS 14.0.0

Still getting this on v13.5.3

It seems to work locally also when navigating to /_next/static/media/image.2b36cbd7.png I do get the image.

Not sure why it’s connecting to port 3000 when the PORT is set to 8080 in our custom server?

looking into the code it seems to be something going on in the fetch perhaps it’s not using the correct port? https://github.com/stefanprobst/next.js/blob/canary/packages/next/server/image-optimizer.ts#L291

upstream image response failed for /_next/static/media/image.2b36cbd7.png TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:11576:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async invokeRequest (/opt/application/node_modules/.pnpm/next@13.5.3_@babel+core@7.22.20_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/lib/server-ipc/invoke-request.js:17:12)
    at async /opt/application/node_modules/.pnpm/next@13.5.3_@babel+core@7.22.20_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/next-server.js:362:39
    at async imageOptimizer (/opt/application/node_modules/.pnpm/next@13.5.3_@babel+core@7.22.20_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/image-optimizer.js:537:13)
    at async cacheEntry.imageResponseCache.get.incrementalCache (/opt/application/node_modules/.pnpm/next@13.5.3_@babel+core@7.22.20_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/next-server.js:519:61)
    at async /opt/application/node_modules/.pnpm/next@13.5.3_@babel+core@7.22.20_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/response-cache/index.js:102:36 {
  cause: Error: connect ECONNREFUSED ::1:3000
      at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16)
      at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
    errno: -111,
    code: 'ECONNREFUSED',
    syscall: 'connect',
    address: '::1',
    port: 3000
  }
}

using an import instead of the source fixes the errors for me:

import Image from 'next/image'
import profilePic from './me.png'
 
export default function Page() {
  return (
    <Image
      src={profilePic}
      alt="Picture of the author"
      // width={500} automatically provided
      // height={500} automatically provided
      // blurDataURL="data:..." automatically provided
      // placeholder="blur" // Optional blur-up while loading
    />
  )
}

You can read more about this here: https://nextjs.org/docs/app/building-your-application/optimizing/images#local-images

Same issue with NextJS 14

Same problem in NextJS 14.0.0

We have also encountered a similar issue: images loaded via /_next/image?url=<path>&w=<num>&q=<num> is failing and complaining that "url" parameter is valid but upstream response is invalid.

However, when attempting to load the actual image in the url parameter it works. This issue is only reproducible in production build and does not seem to affect when running the development server locally.

The same problem in NextJS 14

Still getting this on next: v13.5.4

upstream image response failed for /_next/static/media/illustrations.885dc0f2.png TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:11600:11)
    at async globalThis.fetch (/home/dev/turboschema-server/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:13:9347)
    at async invokeRequest (/home/dev/turboschema-server/node_modules/next/dist/server/lib/server-ipc/invoke-request.js:16:12)
    at async /home/dev/turboschema-server/node_modules/next/dist/server/next-server.js:362:35
    at async imageOptimizer (/home/dev/turboschema-server/node_modules/next/dist/server/image-optimizer.js:537:13)
    at async cacheEntry.imageResponseCache.get.incrementalCache (/home/dev/turboschema-server/node_modules/next/dist/server/next-server.js:517:61)
    at async /home/dev/turboschema-server/node_modules/next/dist/server/response-cache/index.js:102:36 {
  cause: InvalidArgumentError: invalid connection header
      at processHeader (/home/dev/turboschema-server/node_modules/next/dist/compiled/undici/index.js:1:102994)
      at new Request (/home/dev/turboschema-server/node_modules/next/dist/compiled/undici/index.js:1:100295)
      at [dispatch] (/home/dev/turboschema-server/node_modules/next/dist/compiled/undici/index.js:1:64352)
      at Intercept (/home/dev/turboschema-server/node_modules/next/dist/compiled/undici/index.js:2:93153)
      at [Intercepted Dispatch] (/home/dev/turboschema-server/node_modules/next/dist/compiled/undici/index.js:1:115359)
      at Client.dispatch (/home/dev/turboschema-server/node_modules/next/dist/compiled/undici/index.js:1:115591)
      at [dispatch] (/home/dev/turboschema-server/node_modules/next/dist/compiled/undici/index.js:2:264035)
      at [Intercepted Dispatch] (/home/dev/turboschema-server/node_modules/next/dist/compiled/undici/index.js:1:115258)
      at Pool.dispatch (/home/dev/turboschema-server/node_modules/next/dist/compiled/undici/index.js:1:115591)
      at [dispatch] (/home/dev/turboschema-server/node_modules/next/dist/compiled/undici/index.js:1:29874)
      at Agent.Intercept (/home/dev/turboschema-server/node_modules/next/dist/compiled/undici/index.js:2:93153)
      at Agent.dispatch (/home/dev/turboschema-server/node_modules/next/dist/compiled/undici/index.js:1:115591)
      at node:internal/deps/undici/undici:11474:55
      at new Promise (<anonymous>)
      at dispatch (node:internal/deps/undici/undici:11474:16)
      at httpNetworkFetch (node:internal/deps/undici/undici:11375:73)
      at httpNetworkOrCacheFetch (node:internal/deps/undici/undici:11277:39)
      at httpFetch (node:internal/deps/undici/undici:11126:43)
      at schemeFetch (node:internal/deps/undici/undici:11054:24)
      at node:internal/deps/undici/undici:10930:26
      at mainFetch (node:internal/deps/undici/undici:10947:11)
      at fetching (node:internal/deps/undici/undici:10904:7)
      at fetch2 (node:internal/deps/undici/undici:10782:20)
      at Object.fetch (node:internal/deps/undici/undici:11598:18)
      at fetch (node:internal/process/pre_execution:274:25)
      at T (/home/dev/turboschema-server/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:11:354915)
      at /home/dev/turboschema-server/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:13:9860
      at NextTracerImpl.trace (/home/dev/turboschema-server/node_modules/next/dist/server/lib/trace/tracer.js:90:20)
      at globalThis.fetch (/home/dev/turboschema-server/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:13:9371)
      at invokeRequest (/home/dev/turboschema-server/node_modules/next/dist/server/lib/server-ipc/invoke-request.js:16:18)
      at /home/dev/turboschema-server/node_modules/next/dist/server/next-server.js:362:74
      at imageOptimizer (/home/dev/turboschema-server/node_modules/next/dist/server/image-optimizer.js:537:19)
      at DevServer.imageOptimizer (/home/dev/turboschema-server/node_modules/next/dist/server/next-server.js:357:20)
      at cacheEntry.imageResponseCache.get.incrementalCache (/home/dev/turboschema-server/node_modules/next/dist/server/next-server.js:517:72)
      at /home/dev/turboschema-server/node_modules/next/dist/server/response-cache/index.js:102:42 {
    code: 'UND_ERR_INVALID_ARG'
  }
}
ImageError: "url" parameter is valid but upstream response is invalid
    at imageOptimizer (/home/dev/turboschema-server/node_modules/next/dist/server/image-optimizer.js:549:19)
    at async cacheEntry.imageResponseCache.get.incrementalCache (/home/dev/turboschema-server/node_modules/next/dist/server/next-server.js:517:61)
    at async /home/dev/turboschema-server/node_modules/next/dist/server/response-cache/index.js:102:36 {
  statusCode: 500
}

I am giving absolute path value to src attribute.

I seriously can’t believe that I cannot insert and display a simple local png image on a page after 2 hours of trial and error. It works in both dev mode and production mode on localhost but whenever I upload it to my server, it doesn’t show. Interestingly other images work fine. I thought maybe the image’s itself has a problem yet browser nicely loading it if I try to access the url under /_next-static. Of course, using HTML img tag works as well. My server is behind ngnix proxy but I don’t imagine it would be the problem because other images are being loaded fine. Tried with 13.5.2, 5.3, .5.4 and finally 13.4.x. having the same result. In the end, I changed the image format to SVG and it worked (probably because next/image won’t re-encode SVG images). My guess is next/image encoding method has problems. I read somewhere Next will use sharp for resizing if it’s installed. I have it and tried to remove in case it’s related but nothing changed.

Honestly, I have encountered many issues with Next 13, I supported you for your hard work and continue supporting but not being able to add an image to web page is simply a humiliating experience.

My server is behind ngnix proxy but I don’t imagine it would be the problem because other images are being loaded fine

@mertafor I think this may be a proxy configuration issue? Try adding:

location / {
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $proxy_protocol_addr;
  proxy_set_header X-Forwarded-For $proxy_protocol_addr;
  proxy_pass <your server>;
}

Mine started working with additional configuration.

Can verify it does not work in production on 13.5.3, downgrading to 13.5.2 fixed it.

I can confirm that 13.5.4 and 13.5.3 dosent work but 13.5.2 did…

Confirmed pinning explicitly to 13.5.2 also resolves the issue for me. In any other version, I was seeing strange, non-deterministic behavior where sometimes images would succeed, and other times the same images would fail with that 500 error complaining about the url parameter.

13.5.4 works for us… Stable and well, don’t see images with 500 errors whatsoever… Had a little heart attack when i noticed the error live… (bad testing in staging on our side… shame)

this was broken on 13.5.3 for me but with 13.5.4 seems like it’s fixed

It is still not fixed yet. I upgraded to version 13.5.4 but still have the image error as above. Version 13.5.4 will have the following error message: UND_ERR_INVALID_ARG

Same here, the issues happening again in production:

upstream image response failed for /test.png TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:11576:11)
    at async globalThis.fetch (/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:109:9276)
    at async invokeRequest (/node_modules/next/dist/server/lib/server-ipc/invoke-request.js:16:12)
    at async /node_modules/next/dist/server/next-server.js:362:35
    at async imageOptimizer (/node_modules/next/dist/server/image-optimizer.js:537:13)
    at async cacheEntry.imageResponseCache.get.incrementalCache (/node_modules/next/dist/server/next-server.js:517:61)
    at async /node_modules/next/dist/server/response-cache/index.js:102:36 {
  cause: InvalidArgumentError: invalid connection header
      at processHeader (/node_modules/next/dist/compiled/undici/index.js:1:102994)
      at new Request (/node_modules/next/dist/compiled/undici/index.js:1:100295)
      at [dispatch] (/node_modules/next/dist/compiled/undici/index.js:1:64352)
      at Intercept (/node_modules/next/dist/compiled/undici/index.js:2:93153)
      at [Intercepted Dispatch] (/node_modules/next/dist/compiled/undici/index.js:1:115359)
      at Client.dispatch (/node_modules/next/dist/compiled/undici/index.js:1:115591)
      at [dispatch] (/node_modules/next/dist/compiled/undici/index.js:2:264035)
      at Pool.dispatch (/node_modules/next/dist/compiled/undici/index.js:1:115591)
      at [dispatch] (/node_modules/next/dist/compiled/undici/index.js:1:29874)
      at Agent.Intercept (/node_modules/next/dist/compiled/undici/index.js:2:93153) {
    code: 'UND_ERR_INVALID_ARG'
  }
}

I’ve encountered a similar issue in the project I’m working on.

It occurred when I attempted the following version upgrade:

- "next": "13.4.12",
+ "next": "13.4.13",

cause: InvalidArgumentError: invalid connection header

@tkafka are you running behind nginx? Maybe this will help: https://github.com/vercel/next.js/issues/56038#issuecomment-1752876655

corporate sabotage to stay in profit. which version can we revert to to get this up and running again? Edit: I found 13.5.2 doesn’t have this issue npm install next@13.5.2

Can confirm going back to 13.5.2 from 13.5.3 solved it for me.

Confirmed they finally fixed this issue in 13.5.5. #corporateSabotage

This problem also occurs behind a proxy with https enabled, such as nginx or traefik, pointing to the default http server of nextjs. If you fetch the image url using the http schema, the image loads and the next https request also works without problem.

Did another test and 13.5.4 was completely broken for both approaches. 13.5.2 is working

Doesn’t work for domains with 13.5.4 from what I’ve tested with.

Confirmed pinning explicitly to 13.5.2 also resolves the issue for me. In any other version, I was seeing strange, non-deterministic behavior where sometimes images would succeed, and other times the same images would fail with that 500 error complaining about the url parameter.

I confirm - still broken in 13.5.4

upstream image response failed for /img/promo.png TypeError: fetch failed
...
cause: InvalidArgumentError: invalid connection header
...
code: 'UND_ERR_INVALID_ARG'

Ubuntu, node v18.17.1

like @shawngustaw after upgrade to 13.5.4 it’s already fixed

this was broken on 13.5.3 for me but with 13.5.4 seems like it’s fixed 👍

I got the same issue, and it only appear with production build run dev local can not foud it, when I’ using the ubuntu laptop to access into production web the image can load normally ??? but when I using other my laptop or phone to access into production web it’s can not load the images.

my next version: 13.5.3

Can confirm 13.5.4-canary.1 fixed the issue for me

Looks like this issue might be fixed in #55988

To everyone encountering this issue, Cause I think underlying cause of this issue is OS mismatch as an application works differently on different systems. I was encountering this issue when trying to use local images in public dir. Solution I switched to a cdn for all images. (though other assets like font etc. work as they should). Now in production, I didn’t get that error even in 13.4, 14.

Closing due to my message above.

I also think that more users are finding this issue in the past 3 weeks when they really have a different issue: https://github.com/vercel/next.js/issues/56038

That can be fixed by upgrading to next@13.5.5

If you still have problems with the latest Next.js, please create a new issue with the steps to reproduce the problem, thanks!

@ndizazzo That resolved the problem. Thanks a lot!

I allready tested multiple version of nextjs and it’s still not working. Should i just use normal img tags instead?

You can specify the full url with your domain this will be working too 😄 (in https)

Oh my gosh i deployed the wrong branch and wondered why it was not fixed hahahaaa

The latest canary didn’t fix it for me 😕 I am getting ERR_SSL_WRONG_VERSION_NUMBER as here https://github.com/vercel/next.js/issues/55706#issuecomment-1740034370

@Iulian-Dragomirescu Using the latest canary solved the issue for me in the end but that’s good advice anyways thanks https://github.com/vercel/next.js/issues/53715#issuecomment-1737030215

Still getting this on v13.5.3

It seems to work locally also when navigating to /_next/static/media/image.2b36cbd7.png I do get the image.

Not sure why it’s connecting to port 3000 when the PORT is set to 8080 in our custom server?

looking into the code it seems to be something going on in the fetch perhaps it’s not using the correct port? https://github.com/stefanprobst/next.js/blob/canary/packages/next/server/image-optimizer.ts#L291

upstream image response failed for /_next/static/media/image.2b36cbd7.png TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:11576:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async invokeRequest (/opt/application/node_modules/.pnpm/next@13.5.3_@babel+core@7.22.20_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/lib/server-ipc/invoke-request.js:17:12)
    at async /opt/application/node_modules/.pnpm/next@13.5.3_@babel+core@7.22.20_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/next-server.js:362:39
    at async imageOptimizer (/opt/application/node_modules/.pnpm/next@13.5.3_@babel+core@7.22.20_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/image-optimizer.js:537:13)
    at async cacheEntry.imageResponseCache.get.incrementalCache (/opt/application/node_modules/.pnpm/next@13.5.3_@babel+core@7.22.20_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/next-server.js:519:61)
    at async /opt/application/node_modules/.pnpm/next@13.5.3_@babel+core@7.22.20_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/response-cache/index.js:102:36 {
  cause: Error: connect ECONNREFUSED ::1:3000
      at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16)
      at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
    errno: -111,
    code: 'ECONNREFUSED',
    syscall: 'connect',
    address: '::1',
    port: 3000
  }
}

This might help you in your solution: https://github.com/vercel/next.js/issues/56038#issuecomment-1736985533