Welcome! Log In Create A New Profile


nginx proxy to external proxy

Posted by mabr3 
nginx proxy to external proxy
August 11, 2020 06:31AM
Hi! I need to configure an nginx proxy that directs requests to another proxy and I'm having some trouble. Here's the context: We have machine A from where the request comes from, machine B where the nginx proxy will be and machine C where an external proxy is (squid). Only C has access to the internet and A has no connectivity with C. Therefore I need to go from A to B to C to the required website.

Here's what my conf looks like (the relevant part):

server {
listen Bport;
location / {
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://Cip:Cport;

But whenever I do something like:

curl -v -x http://Bip:Bport https://google.com

I get:

About to connect() to proxy Bip port Bport (#0)
* Trying Bip... connected
* Connected to Bip (Bip) port Bport (#0)
* Establish HTTP proxy tunnel to google.com:443
> CONNECT google.com:443 HTTP/1.1
> Host: google.com:443
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.19.1 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Proxy-Connection: Keep-Alive
< HTTP/1.1 400 Bad Request
< Server: nginx/1.10.1
< Date: Tue, 11 Aug 2020 10:03:27 GMT
< Content-Type: text/html
< Content-Length: 173
< Connection: close
* Received HTTP code 400 from proxy after CONNECT
* Closing connection #0
curl: (56) Received HTTP code 400 from proxy after CONNECT

Whereas if I use the external proxy directly (starting from B):

curl -v -x http://Cip:Cport https://google.com

The request goes through and everything works OK. Can someone provide some insight on this? Thanks in advance.
Re: nginx proxy to external proxy
January 06, 2021 02:46AM
Hi, I am also having this problem. If you have got any solution, please share it
Re: nginx proxy to external proxy
January 06, 2021 04:57AM
No, I haven't found any solution to this. Basically the conclusion we came to is that Nginx isn't the right tool for this.
Re: nginx proxy to external proxy
January 06, 2021 08:46AM
I had got a solution to my issue.
What I wanted to setup was an nginx reverse proxy, so that:
- it will receive requests from the client
- replace the host part in that URL with another CDN I wanted to access
- forward this request to the CDN, through a forward proxy.

I had to access the CDN through the nginx server, because I wanted to cache the contents served, and thus reduce the number of hits to CDN.

Well, the solution I got is this:


server {
listen 8000;
server_name my-domain-name.com;

location / {

rewrite ^(.*)$ "://the-cdn-domain-name.com$1";
rewrite ^(.*)$ "https$1" break;
proxy_pass http://X.X.X.X:1234; # this is my forward proxy IP

This is what you have to include in the conf.d directory.
Re: nginx proxy to external proxy
January 06, 2021 10:54AM
Oh, don't know why I didn't think of using rewrite. Thanks!
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 94
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready