Serving rails page caches to a facebook POST request with nginx
Posted by ezmobius Mon, 26 Nov 2007 21:15:00 GMT
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.
The problem is that facebook only sends POST requests and Nginx and most other webservers do not allow serving a static file in response to a POST request. Nginx will return a 405 error.
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:
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;
}
}
Enjoy ;)
Searching...





Thanks, this is really useful. kind regards