ixe013
ixe013

Reputation: 10191

http.nonProxyHosts ignored on localhost only

I run a proxy on localhost:2080 for debugging purposes. I can see trafic going through my proxy using either:

curl http://localhost:8888/stuff --proxy localhost:2080
curl http://some.server.com:8888/stuff --proxy localhost:2080

Using a Spring application, or a bare bones HttpURLConnectionExample Java application, I see requests go through the proxy when I call some.server.com, but never on localhost, even though I set http.nonProxyHosts to a random value (to avoid the default of localhost).

I made sure my JVM arguments were taken into accounts by adding the following code:

System.out.println("java.version="+props.getProperty("java.version"));
System.out.println("http.proxyHost="+props.getProperty("http.proxyHost"));
System.out.println("http.proxyPort="+props.getProperty("http.proxyPort"));
System.out.println("http.nonProxyHosts="+props.getProperty("http.nonProxyHosts"));

That produces the following output:

java.version=1.8.0_131
http.proxyHost=localhost
http.proxyPort=2080
http.nonProxyHosts=dummy.snafu.com
https.proxyHost=localhost
https.proxyPort=2080
https.nonProxyHost=dummy.snafu.com
Testing 1 - Send Http GET request

Sending 'GET' request to URL : http://localhost:8888/stuff
Response Code : 200
hello

Why are HTTP requests to localhost not going through my proxy when run with the following JVM arguments?

Upvotes: 3

Views: 5907

Answers (1)

hmble
hmble

Reputation: 140

This is a limitation of DefaultProxySelector, It always appends local Uris(localhost, 127.*, etc) to whatever you configure as nonProxyHosts, essentially bypassing proxy for all local Uris(localhost|127. *|[::1]|0.0.0.0|[::0]). Fortunately, there is a workaround by creating your own proxy selector and registering it as default.

Upvotes: 4

Related Questions