And yet I like my inject-solution better – no local variables or superfluous blocks. Why not change inject by not returning the value of the block and using it in the next iteration, why not each time pass in the argument to inject. Let’s name this function inject!

At Newminds our internal test environment is running a Nginx webserver as a frontend proxying every request to a mongrel server. Recently, I wanted to setup page caching to increase the performance of our service. So, let me share my experience with you.

This will proxy all request to the mongrel server, which is fine but doesn’t serve any static content (cached) content of course. In order to do that, we will need to extend the above block a little bit.

server {
# specify document root
# see: http://wiki.codemongers.com/NginxHttpCoreModule#root
root /home/user/apps/my_app/current/public;
location / {
# if the file exists as a static file serve it directly
if (-f $request_filename) {
break;
}
# check for index.html for directory index
# if it's there on the filesystem then rewrite
# the url to add /index.html to the end of it
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
# add .html to the end of the url and check the filesystem
# if it exists, serve it directly, else hit mongrel
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
if (-f $request_filename) {
proxy_pass http://mongrel;
break;
}
}

Great! This will serve all our page cache as static content making the application blazing fast! But there is one flaw: a GET /fruits/23 will serve public/fruits/23.html from the filesystem. That works as intended, but when someone wants to update this little fruit he will send a PUT /fruits/23. When that happens, Nginx checks the filesystem on an existing file at public/fruits/23.html, this check will return true and breaks. It will never hit mongrel. In fact Nginx even returns a 405 Method Not Allowed HTTP status code, because Nginx only knows how to GET a file, not how to PUT one.

This means we need to modify the configuration so Nginx only serves the static content if the HTTP request method is GET or HEAD. This will lead us to the final configuration block, which passes all requests except GET or HEAD immediately to mongrel.

server {
# specify document root
# see: http://wiki.codemongers.com/NginxHttpCoreModule#root
root /home/user/apps/my_app/current/public;
location / {
# only try to serve a static file if the request method is GET or HEAD
# if it's anything else (POST for example) hit mongrel.
if ($request_method !~ "GET|HEAD") {
proxy_pass http://mongrel;
break;
}
# if the file exists as a static file serve it directly
if (-f $request_filename) {
break;
}
# check for index.html for directory index
# if it's there on the filesystem then rewrite
# the url to add /index.html to the end of it
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
# add .html to the end of the url and check the filesystem
# if it exists, serve it directly, else hit mongrel
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
if (-f $request_filename) {
proxy_pass http://mongrel;
break;
}
}

Most web-applications do not appear to have a lot to do with random numbers, online poker sites excepted (I hope), but they are surprisingly useful nevertheless. That’s why I’m introducing a new notation to express chance.

# this certainly works
redirect_to home_url if rand < 0.2222
# this is better
redirect_to home_url if rand(9) < 2
# but not as beautiful as this
redirect_to home_url if 2.in 9
# the code
class Numeric
def in(other)
rand(other) < self
end
end

We’d like to introduce a new blog category: Our Daily Method. We’ll demonstrate short, general purpose methods, which might be suitable for the standard library. We’re accepting requests!

We’ll kick off with Range#coerce. The problem is: you’ve got a value and you wish to ensure that it’s within a certain interval. Sounds ideal for a Range, but it’s missing from Ruby’s Range, although there’s include? (and in Ruby 1.9 cover?) to test whether the value is in the range.

We’ve decided to start an English section on this Dutch blog. We will translate some of our more technical articles. These articles will (we hope) not appear on the front page, but in the category “english”. A separate RSS-feed should be available soon.