<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/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>Brainspl.at: Serving rails page caches to a facebook POST request with nginx</title>
    <link>http://www.brainspl.at/articles/2007/11/26/serving-rails-page-caches-to-a-facebook-post-request-with-nginx</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Brainspl.at</description>
    <item>
      <title>"Serving rails page caches to a facebook POST request with nginx" by J.Mihai</title>
      <description>Thanks, this is really useful. 

kind regards</description>
      <pubDate>Fri, 14 Dec 2007 17:16:18 +0000</pubDate>
      <guid>urn:uuid:a2ee066a-453a-405c-95d9-345aa4273169</guid>
      <link>http://www.brainspl.at/articles/2007/11/26/serving-rails-page-caches-to-a-facebook-post-request-with-nginx#comment-4600</link>
    </item>
    <item>
      <title>Serving rails page caches to a facebook POST request with nginx</title>
      <description>&lt;p&gt;Here is a nice little snippet for nginx that took me forever to figure out. The scenario is that you have a facebook app and you want to use rails built in page caching that writes out an html file that the webserver serves directly instead of hitting rails for the page.&lt;/p&gt;


	&lt;p&gt;The problem is that facebook only sends &lt;span class="caps"&gt;POST&lt;/span&gt; requests and Nginx and most other webservers do not allow serving a static file in response to a &lt;span class="caps"&gt;POST&lt;/span&gt; request. Nginx will return a 405 error.&lt;/p&gt;


	&lt;p&gt;The way to solve this is to catch the 405 error with an error_page directive and change it to a 200 ok request and serve the page cached file. Of course this still has to work with checking for html extensions and falling back to mongrel if there is no page cache. So here is the solution .. add this to your nginx vhost server{} block at the bottom and you will be good to go:&lt;/p&gt;


&lt;pre&gt;
    error_page   405 =200 @405;
    location @405 {
      index  index.html index.htm;
      # needed to forward user's IP address to rails
      proxy_set_header  X-Real-IP  $remote_addr;
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect false;
      proxy_max_temp_file_size 0;
      proxy_next_upstream error;
      if (-f $request_filename) {
        break;
      }
      if (-f $request_filename.html) {
        rewrite (.*) $1.html break;
      }
      if (!-f $request_filename) {
        proxy_pass http://mongrel;
        break;
      }
    }
&lt;/pre&gt;

	&lt;p&gt;Enjoy ;)&lt;/p&gt;</description>
      <pubDate>Mon, 26 Nov 2007 21:15:00 +0000</pubDate>
      <guid>urn:uuid:1b4d5035-1580-425c-8ff9-c296a7b6fce1</guid>
      <author>ezmobius</author>
      <link>http://www.brainspl.at/articles/2007/11/26/serving-rails-page-caches-to-a-facebook-post-request-with-nginx</link>
      <category>facebook</category>
      <category>nginx</category>
      <trackback:ping>http://www.brainspl.at/articles/trackback/4565</trackback:ping>
    </item>
  </channel>
</rss>

