When I try get from nginx with proxy_pass http://localhost:3142/; I GET /dist/jammy/InRelease I think it’s why apt cacher ng refuse it. I put proxy_http_version 1.1 (but I remain see header connexion close)
I wish keep nginx as frontend to have easy certbot integration.
So there is nothing actually listening on http://localhost:3142 right? Your NGINX config is telling NGINX to proxy to http://localhost:3142. If there is nothing listening on that port then NGINX is proxying to an invalid endpoint. Try changing proxy_pass http://localhost:3142/; to something along the lines of proxy_pass http://ports.ubuntu.com/ubuntu-ports/dists/jammy/InRelease;.
Okay, so apt-cacher-ng is on port 3142, but if you query port 3142 without specifying the Ubuntu repos, what happens? By using proxy_pass ...:3142 you are proxying to apt-cacher-ng, but if the cacher isn’t then proxying to the Ubuntu repos, nothing will really happen.
Forget about running curl -x for the time being. As far as I know (and I could be wrong here) that exact behaviour cannot be replicated within NGINX. At a very simple level, if apt-cacher-ng is an http proxy, running curl http://localhost:3142 should redirect you to http://ports.ubuntu.com/ubuntu-ports/dists/jammy/InRelease. If that is the case, you could then configure NGINX the way you have to run curl http://localhost:80 instead and still reach the Ubuntu repo.
As far as I can tell, the issue lies in your apt-cacher-ng config, but I am not familiar with the tool. If the docs above don’t help, then hopefully someone else can lend a hand here.
Why? apt-cacher-ng it’s http proxy, mean don’t have content, don’t have destination defined. It’s exactly the http_proxy you configure into firefox/curl, this kind of proxy need the real destination. And then apt-cacher-ng work exactly as excepted, as http_proxy. (I have do multiple proxy in my life, include socks5 and specific protocol proxy.) And I don’t wish do a wrapper just for this.
In that case, you might not be able to use NGINX like you intend to do per my previous comment. NGINX can only act as a reverse proxy, which means it has to reach the target server/destination for it to work. You cannot natively use an intermediary proxy between the NGINX proxy and your endpoint.
There might be some third party custom modules out there that let you do this, but I do not know for sure. Best of luck!
As an aside, the following LWN article discussed reasons why tools like apt-cacher-ng exist and many prefer them to using generic web proxies for this purpose (which does also work, at least “mostly”).