How to configure nginx as reverse proxy for the site, which is behind squid proxy

2.1k views Asked by At

I need to configure nginx, which is acting as reverse proxy for my site (myportal.com). nginx can connect to the actual site via squid proxy.

client -> nginx -> Squid Proxy -> web server.

Note:

a. Client assumes nginx as the web server (of myportal.com).

b. nginx fetches the content from actual server (myportal.com), via squid proxy.

Thanks Tarun. We have successfully configured nginx as reverse proxy when the destination web server is reachable directly. But while connect to the web serve behind squid. We are facing issue. As the Client has to connect to the nginx assuming it is the destination server (hence no proxy headers sent). –

We tried the following too.

upstream @squid {
    server localhost:3128;
}

server {
    listen 80;
    server_name relay.example.com;

    location / {
        proxy_pass http://@squid/$scheme://$host$uri;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Request-URI $request_uri;

        proxy_redirect off;
    }
}
1

There are 1 answers

2
nbari On

Give a try to this configuration, it is using Nginx as a reverse proxy and caching, probably could help you get an idea:

    ...
    proxy_buffering    on;
    proxy_cache_path   /home/cache levels=1:2 keys_zone=myportal:256m max_size=5g inactive=24h use_temp_path=off;
    proxy_buffer_size  8k;
    proxy_buffers      8 24k;

    server {
        listen       80;
        server_name  _;

        location / {
            proxy_cache myportal;
            proxy_cache_use_stale updating error timeout invalid_header http_500 http_502 http_503 http_504;
            proxy_cache_revalidate on;
            proxy_cache_min_uses 1;
            proxy_cache_lock on;
            proxy_cache_valid 200 301 304 2d;
            proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;

            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header Host myportal.tld;
            proxy_set_header Connection "";
            proxy_http_version 1.1;
            proxy_pass https://myportal.tld/;
        }
    }
    ...