<?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>maxgarrick.com &#187; nginx</title>
	<atom:link href="http://maxgarrick.com/tag/nginx/feed/" rel="self" type="application/rss+xml" />
	<link>http://maxgarrick.com</link>
	<description>Come take a look under the hood</description>
	<lastBuildDate>Sun, 08 Mar 2009 23:32:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Reverse proxy with nginx</title>
		<link>http://maxgarrick.com/reverse-proxy-with-nginx/</link>
		<comments>http://maxgarrick.com/reverse-proxy-with-nginx/#comments</comments>
		<pubDate>Sun, 30 Nov 2008 05:08:41 +0000</pubDate>
		<dc:creator>max</dc:creator>
				<category><![CDATA[System Administration]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[centos]]></category>
		<category><![CDATA[mod_rpaf]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[reverse proxy]]></category>

		<guid isPermaLink="false">http://maxgarrick.com/?p=55</guid>
		<description><![CDATA[In this post I hope to show how to configure nginx as a reverse proxy to a back-end CentOS 5 server running Apache.
When you add an nginx reverse proxy layer on top of Apache, Apache thinks that all connections originate from the server running nginx.  This creates a couple annoying problems:

Every entry in the [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I hope to show how to configure nginx as a reverse proxy to a back-end CentOS 5 server running Apache.</p>
<p>When you add an nginx reverse proxy layer on top of Apache, Apache thinks that all connections originate from the server running nginx.  This creates a couple annoying problems:</p>
<ul>
<li>Every entry in the Apache access logs appears to come from the IP of the nginx server</li>
<li>Securing sessions by checking that a user&#8217;s IP address hasn&#8217;t changed becomes more difficult.
<ul>
<li>This is especially true when using open source software.  OS packages usually look for the client&#8217;s IP in the REMOTE_ADDR variable.</li>
</ul>
</li>
</ul>
<p>To resolve these issues, we&#8217;ll use the Apache mod_rpaf module to populate the REMOTE_ADDR using a special HTTP header inserted by nginx.  A typical request would work as follows:</p>
<ul>
<li>1.2.3.4 sends HTTP request to nginx server</li>
<li>nginx determines that it needs to proxy pass the request to a back-end Apache server (e.g. by looking at the content-type or virtual host).</li>
<li>nginx adds an HTTP header &#8220;X-Forwarded-For&#8221; with the client&#8217;s real IP</li>
<li>nginx forwards (proxy_pass) the request to back-end Apache server</li>
<li>mod_rpaf in Apache detects that the request is coming from the nginx IP, then substitutes REMOTE_ADDR with the contents of X-Forwarded-For</li>
<li>Apache handles request as normal.  Applications do not need to be aware of the reverse proxy.</li>
</ul>
<p>To install mod_rpaf on the CentOS 5 box:</p>
<pre class="brush: bash">
wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz
tar zxvf mod_rpaf-0.6.tar.gz
cd mod_rpaf-0.6

# Patch Makefile so it looks for &#039;apxs&#039; instead of &#039;apxs2&#039; (required
# when compiling under CentOS 5):
sed -ie &#039;s/apxs2/apxs/&#039; Makefile

make rpaf-2.0
make install-2.0
</pre>
<p>Create /etc/httpd/conf.d/rpaf.conf:</p>
<pre class="brush: php">
# Path to mod_rpaf-2.0.so, relative to /etc/httpd/
LoadModule rpaf_module modules/mod_rpaf-2.0.so

RPAFenable On
RPAFsethostname On

# Define our reverse proxy IP.  Only substitute client IP in
# when we receive a request from this IP.
RPAFproxy_ips 192.168.0.1

# The header where the real client IP address is stored.
RPAFheader X-Forwarded-For
</pre>
<p>Configure nginx to reverse proxy our CNAME IP address to the back-end container.  We won&#8217;t go into installing nginx here, instead I&#8217;ve included the relevant configuration section in /etc/nginx/nginx.conf.  This configuration says to reverse proxy all requests to the virtual host &#8216;myvirtualhost.com&#8217;:</p>
<pre class="brush: php">
server {
listen 80;
server_name myvirtualhost.com;

location / {
proxy_pass http://192.168.0.56;
proxy_redirect default;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
</pre>
<p>After restarting Apache &amp; nginx, you should be able to successfully connect to your back-end Apache via the nginx reverse proxy layer.  Inspecting the Apache environment will show a couple new headers, but other than that requests look the same as if clients were connecting directly without the proxy.</p>
]]></content:encoded>
			<wfw:commentRss>http://maxgarrick.com/reverse-proxy-with-nginx/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
