3

I have this on my machine:

$ cat /etc/profile.d/proxy.sh 
export http_proxy=http://192.168.1.30:3128
export https_proxy=https://192.168.1.30:3128

That works great, until I need to use an HTTP interface with an application on the localhost.

$ wget localhost
--2023-03-02 06:54:52--  http://localhost/
Connecting to 192.168.1.30:3128... connected.
Proxy request sent, awaiting response... 503 Service Unavailable
2023-03-02 06:54:52 ERROR 503: Service Unavailable.

$ wget 127.0.0.1
--2023-03-02 06:55:20--  http://127.0.0.1/
Connecting to 192.168.1.30:3128... connected.
Proxy request sent, awaiting response... 403 Forbidden
2023-03-02 06:55:20 ERROR 403: Forbidden.

Is there a way to prevent localhost and 127.0.0.1 requests from being forwarded to the proxy?


Details:

This machine isn't connected directly to the internet. It has no gateway or default route. But it is on a LAN with a proxy machine (192.168.1.30) that has a proxy server installed (port 3128) and has an internet connection.

$ ip addr
1: lo: ...
    inet 127.0.0.1/8 scope host lo
2: eno1: ...
    altname enp24s0f0
    inet 192.168.1.100/24 brd 192.168.1.255 scope global eno1

$ ip route
192.168.1.0/24 dev eno1 proto kernel scope link src 192.168.1.100

$ cat /etc/hosts
127.0.0.1   localhost

$ cat /etc/network/interfaces
auto lo
iface lo inet loopback

auto eno1
iface eno1 inet static
    address 192.168.1.100
    netmask 255.255.255.0

1 Answer 1

8

From wget man page:

ENVIRONMENT

Wget supports proxies for both HTTP and FTP retrievals. The standard way to specify proxy location, which Wget recognizes, is using the following environment variables:

http_proxy and https_proxy

If set, the http_proxy and https_proxy variables should contain the URLs of the proxies for HTTP and HTTPS connections respectively.

ftp_proxy

This variable should contain the URL of the proxy for FTP connections. It is quite common that http_proxy and ftp_proxy are set to the same URL.

no_proxy

This variable should contain a comma-separated list of domain extensions proxy should not be used for. For instance, if the value of no_proxy is .mit.edu, proxy will not be used to retrieve documents from MIT.

So, you should change your /etc/profile.d/proxy.sh to:

export http_proxy=http://192.168.1.30:3128
export https_proxy=https://192.168.1.30:3128
export no_proxy="127.0.0.1,localhost"

If you have a GUI web browser that does not use the *_proxy environment variables, then you could create a proxy autoconfiguration file, name the file proxy.pac, have your local HTTP server provide it with MIME type application/x-ns-proxy-autoconfig.

Then configure your desktop environment and/or web browser(s) to use that autoconfiguration file, by specifying a proxy autoconfiguration URL like http://localhost/some/where/proxy.pac (or maybe even file:///some/location/proxy.pac) in the proxy settings of the desktop environment or web browser.

In your case, the contents of the file could be:

function FindProxyForURL(url, host)
{
    // If not a http: or https: URL, go direct always.
    if (!shExpMatch(url,"http*") {
        return "DIRECT";
    }

    // If the destination is:
    // - any 127.*.*.* address
    // - or anything like "localhost*"
    // - or the web browser host's own IP address
    // Then go direct to destination.

    else if (shExpMatch(host, "127.*")
        || shExpMatch(host, "localhost*")
        || host == myIpAddress()) {
        return "DIRECT";
    }
    else {
        // Otherwise, use the proxy specified here.
        return "PROXY 192.168.1.30:3128";
    }
}
2
  • Fantastic! I didn't look into man wget because I've reproduced the problem with curl and python3-urllib3, so I figured it was system-wide and not application-specific. I didn't realize that these applications just all happen to have code to handle these variables.
    – Stewart
    Commented Mar 2, 2023 at 16:09
  • 1
    One might say that those environment variables might be the closest thing to an universal standard for configuring a HTTP(S) proxy we have for unix-like operating systems. The various desktop environments can have their own system-wide and per-user proxy settings, but those may not always apply to command-line programs or services executed outside a user's login session.
    – telcoM
    Commented Mar 2, 2023 at 23:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .