<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>lighty's life: X-Sendfile's new friend: X-Rewrite-*</title>
    <link>http://blog.lighttpd.net/articles/2006/07/22/x-sendfiles-new-friend-x-rewrite</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>X-Sendfile's new friend: X-Rewrite-*</title>
      <description>&lt;p&gt;Today on &lt;span class="caps"&gt;IRC I&lt;/span&gt; got a question on:&lt;/p&gt;


&lt;pre&gt;
&amp;lt;b&amp;gt;Fobax&amp;lt;/b&amp;gt; can you x-send-file from fast-cgi to a proxy request?
&lt;/pre&gt;

	&lt;p&gt;Or asked in another way ?&lt;/p&gt;


&lt;pre&gt;
Can you use a mod_proxy_core backend to rewrite a URL or a Host header ?
&lt;/pre&gt;

	&lt;p&gt;After 30 minutes of coding &amp;#8230; &lt;b&gt;Yes&lt;/b&gt;, you can &amp;#8230;&lt;/p&gt;
&lt;p&gt;Some tweaking and here and there and we had:&lt;/p&gt;


&lt;pre&gt;
proxy-core.allow-x-rewrite = "enable" 
&lt;/pre&gt;

	&lt;p&gt;which allows you to send:&lt;/p&gt;


&lt;pre&gt;
HTTP/1.0 200 OK
X-Rewrite-URI: /pi.php
X-Rewrite-Host: www.foobar.com
&lt;/pre&gt;

	&lt;p&gt;as response and lighty will replace both values and the original request and start the whole request from scratch. Here I rewrite the incoming request to use another internal Host and a new &lt;span class="caps"&gt;URI&lt;/span&gt;.&lt;/p&gt;


&lt;h3&gt;What is this good for ?&lt;/h3&gt;

	&lt;ul&gt;
	&lt;li&gt;rewriting &lt;kbd&gt;http://user.example.org/&lt;/kbd&gt; to &lt;kbd&gt;http://xample.org/~user/&lt;/kbd&gt;&lt;/li&gt;
		&lt;li&gt;denying access to host with unwanted content based on the Hostname&lt;/li&gt;
		&lt;li&gt;filtering in general&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Do you have other examples ?&lt;/p&gt;</description>
      <pubDate>Sat, 22 Jul 2006 11:37:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:a7be37dd-e4ae-498a-82e0-1ec8692d46cb</guid>
      <author>jan</author>
      <link>http://blog.lighttpd.net/articles/2006/07/22/x-sendfiles-new-friend-x-rewrite</link>
      <trackback:ping>http://blog.lighttpd.net/articles/trackback/1824</trackback:ping>
    </item>
    <item>
      <title>"X-Sendfile's new friend: X-Rewrite-*" by Jan Kneschke</title>
      <description>It would be something like this:

&lt;pre&gt;
$HTTP["host"] == "external.com" {
  $HTTP["url"] !~ "\.php$" {
    proxy-core.protocol = "fastcgi"
    proxy-core.backends = ( "/tmp/php-fcgi.socket" )

    proxy-core.allow-x-rewrite = "enable" 
    proxy-core.rewrite-request = ( 
      "_uri"  =&gt; ( "(.*)" =&gt; "/rewrite.php?$1"))
  }
}
$HTTP["host"] == "internal1.external.com" {
  proxy-core.backends = ( "192.168.1.1" )
}
$HTTP["host"] == "internal2.external.com" {
  proxy-core.backends = ( "192.168.1.2" )
}
&lt;/pre&gt;</description>
      <pubDate>Mon, 24 Jul 2006 09:52:34 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:443de99a-aabe-4cb5-9d21-83d03f9da364</guid>
      <link>http://blog.lighttpd.net/articles/2006/07/22/x-sendfiles-new-friend-x-rewrite#comment-1832</link>
    </item>
    <item>
      <title>"X-Sendfile's new friend: X-Rewrite-*" by Fobax</title>
      <description>This is great for doing much more complex rewrites, like lookups to a database/memcache, with logic. It means converting one url style to another on the fly is possible without having to necessarily keep a backward compatible version on the filesystem (think of the case where extra data is added so a regex isn't possible).

Another use would be things like much more complex authentication or bandwidth control.

The big question is, can you do custom proxy balancing this way? The reason for this is the balance modes right now are: round robin, least connection, or hash. But all of those assume all backends have all the data. With the hash it will increase cache locality and keep only a small subset in ram, but it must at least have it all on disk. If you have millions of files or terabytes of data that won't work. The goal here is to only have to keep it on one or two backends, and have the rewrite specify which backend has the data.
I guess this can be done by having an fcgi block on the external domain, and a bunch of proxy blocks for the internal servers, one each. The rewrite would rewrite the host header to be to the correct backend domain. Do I have this correct Jan?

ie:
&lt;pre&gt;
$HTTP["host"] == "external.com" {
  fastcgi.server = ( ".php" =&gt; ("socket" =&gt; "/tmp/php-fcgi.socket", 
  proxy-core.allow-x-rewrite = "enable" ))
  url.rewrite = ( "(.*)" =&gt; "/rewrite.php?$1")
}
$HTTP["host"] == "internal1.external.com" {
  proxy.server = ( "" =&gt; ( "host" =&gt; "192.168.1.1", "port" =&gt; 80 ))
}
$HTTP["host"] == "internal2.external.com" {
  proxy.server = ( "" =&gt; ( "host" =&gt; "192.168.1.2", "port" =&gt; 80 ))
}
&lt;/pre&gt;</description>
      <pubDate>Sat, 22 Jul 2006 23:44:53 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:6a4dbff0-eeab-4898-aaf4-eed3d29394f3</guid>
      <link>http://blog.lighttpd.net/articles/2006/07/22/x-sendfiles-new-friend-x-rewrite#comment-1827</link>
    </item>
    <item>
      <title>"X-Sendfile's new friend: X-Rewrite-*" by Jan Kneschke</title>
      <description>Yes.</description>
      <pubDate>Sat, 22 Jul 2006 16:58:25 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:ea075808-e343-4eae-9ca6-f1d394b3b52b</guid>
      <link>http://blog.lighttpd.net/articles/2006/07/22/x-sendfiles-new-friend-x-rewrite#comment-1826</link>
    </item>
    <item>
      <title>"X-Sendfile's new friend: X-Rewrite-*" by Yusuf</title>
      <description>Just to clarify this, does this mean you can have one set of backends which only does rewrites and another set of backends which can handle the request post-rewrite ?</description>
      <pubDate>Sat, 22 Jul 2006 16:33:19 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:aaca2c29-3027-44d6-b48b-7cd73240ad9e</guid>
      <link>http://blog.lighttpd.net/articles/2006/07/22/x-sendfiles-new-friend-x-rewrite#comment-1825</link>
    </item>
  </channel>
</rss>
