Sunday, 23 November 2014

Ruby and NO_PROXY

Proxy Servers


Like most people where they work there is always a proxy server present.  Love them or hate them they are here to stay. Recently where I am working we faced a problem. All the HTTP traffic is going thorough the proxy. However there was a specific case where we did not want it to.

The Problem


When we sent a request to a URL lets call it http://example.com (please note that this is an internal address) it would resolve to a load balanced address. For some reason (I'm not a networking guy) the proxy could not connect to it.

The Solution


What I needed to do was the following:

  1. I edited the /etc/hosts file and added IP_ADDRESS example.com
  2. I added export no_proxy="example.com" so it does not use the proxy. 
However the fun did not stop there. We were using Faraday to connect to this server. What I realised is that the default Ruby HTTP implementation does not honour no_proxy environment variable. So after some researching I found that httpclient does. So all I needed to do is install the gem and then use it:

conn = Faraday.new(url: 'example.com') do |faraday|
  faraday.request  :url_encoded
  faraday.response :logger
  faraday.adapter  :excon
end

Call conn.get and everything worked.

As I found it hard to find a solution I thought I would write it down for someone else to find it useful.