Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

  • -Dhttp.proxyHost=localhost
  • -Dhttp.proxyPort=2080
  • -Dhttp.nonProxyHosts=dummy.snafu.com
like image 438
ixe013 Avatar asked Nov 29 '25 16:11

ixe013


1 Answers

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.

like image 167
hmble Avatar answered Dec 02 '25 04:12

hmble



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!