How to use NGINX as forward proxy for any requested location?

Your code appears to be using a forward proxy (often just “proxy”), not reverse proxy and they operate quite differently. Reverse proxy is for server end and something client doesn’t really see or think about. It’s to retrieve content from the backend servers and hand to the client. Forward proxy is something the client sets up in order to connect to rest of the internet. In turn, the server may potentially know nothing about your forward proxy.

Nginx is originally designed to be a reverse proxy, and not a forward proxy. But it can still be used as a forward one. That’s why you probably couldn’t find much configuration for it.

This is more a theory answer as I’ve never done this myself, but a configuration like following should work.

server {
    listen       8888;

    location / {
        resolver 8.8.8.8; # may or may not be necessary.
        proxy_pass http://$http_host$uri$is_args$args;
    }
}

This is just the important bits, you’ll need to configure the rest.

The idea is that the proxy_pass will pass to a variable host rather than a predefined one. So if you request http://example.com/foo?bar, your http header will include host of example.com. This will make your proxy_pass retrieve data from http://example.com/foo?bar.

The document that you linked is using it as a reverse proxy. It would be equivalent to

        proxy_pass http://localhost:80;

Leave a Comment