<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Floorplanner Tech Blog &#187; proxy</title>
	<atom:link href="http://techblog.floorplanner.com/tag/proxy/feed/" rel="self" type="application/rss+xml" />
	<link>http://techblog.floorplanner.com</link>
	<description>Our latest geek adventures!</description>
	<lastBuildDate>Tue, 16 Mar 2010 18:45:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Finding the correct IP address in Rails</title>
		<link>http://techblog.floorplanner.com/2008/04/03/finding-the-correct-ip-address-in-rails/</link>
		<comments>http://techblog.floorplanner.com/2008/04/03/finding-the-correct-ip-address-in-rails/#comments</comments>
		<pubDate>Thu, 03 Apr 2008 12:30:39 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Floorplanner]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[ip adress]]></category>
		<category><![CDATA[proxy]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.suite75.net/blog/dev/finding-the-correct-ip-address-in-rails.html</guid>
		<description><![CDATA[Today, I have added a server switch to Floorplanner.com. Now, it will load the floorplanner elements from the server that is nearest to you, which can yield a significant improvement in the initial loading time of your floorplans.
To determine your location, your IP address is matched against a table of locations. This worked fine in [...]]]></description>
			<content:encoded><![CDATA[<p>Today, I have added a server switch to Floorplanner.com. Now, it will load the floorplanner elements from the server that is nearest to you, which can yield a significant improvement in the initial loading time of your floorplans.</p>
<p>To determine your location, your IP address is matched against a table of locations. This worked fine in our development version, but it didn&#8217;t work at all on the production server. After some searching, I found that our server configuration was causing this. We use Apache as our web server, which uses mod_proxy to send the request to our Mongrel cluster. This intermediary step caused the IP address that Rails would receive to always be the IP address of the Apache server: 127.0.0.1. Therefore, the location matching did not work.</p>
<p>However, I found that mod_proxy adds <a href="http://www.railsforum.com/viewtopic.php?pid=52744">an additional header</a> to the request with the original IP address: HTTP_X_FORWARDED_FOR. This header can be used for our purpose. Now, I use the following function to determine the correct IP address:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> determine_ip<span style="color:#006600; font-weight:bold;">&#40;</span>request<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#008000; font-style:italic;"># use HTTP_X_FORWARDED_FOR if available</span>
  <span style="color:#008000; font-style:italic;"># otherwise fall back to default header</span>
  request.<span style="color:#9900CC;">env</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;HTTP_X_FORWARDED_FOR&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span> request.<span style="color:#9900CC;">remote_addr</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>On a related note: to match an IP address against ranges of IP addresses in our location table, it must be converted from a string (&#8220;1.2.3.4&#8243;) to a number (16909060). I use the following oneliner, which uses some nice functional programming tricks and an application of bit-shifting:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> numeric_ip<span style="color:#006600; font-weight:bold;">&#40;</span>ip_str<span style="color:#006600; font-weight:bold;">&#41;</span>
  ip_str.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'.'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>ip_num, part<span style="color:#006600; font-weight:bold;">|</span>
            <span style="color:#006600; font-weight:bold;">&#40;</span> ip_num <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006666;">8</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> part.<span style="color:#9900CC;">to_i</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Yes, I am really proud if this function! <img src='http://techblog.floorplanner.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p><strong>UPDATE:</strong> I just <a href="http://railsauthority.com/tutorial/how-to-obtain-the-ip-address-of-the-current-user">found out</a> that <code>request.remote_ip</code> does the same as my determine_ip-function. Unfortunately, it only works in Rails 2.0.</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2008/04/03/finding-the-correct-ip-address-in-rails/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
