27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Serving requests<br />

the changes, you can deploy them to your server using the simple cap deploy<br />

:migrations command. This will update the code on your application, run bundle<br />

install, and then run any new migrations you may have added.<br />

There’s much more to Capistrano than this, and you can get to know more of it by<br />

reading the Capistrano Handbook 27 or by asking questions on the Capistrano Google<br />

Group at http://groups.google.com/group/capistrano.<br />

To run this application and make it serve requests, you could use rails server like<br />

in development, but there are a couple of problems with this approach. For starters, it<br />

requires you to always be running a terminal session with it running, which is just<br />

hackish. Secondly, this process is only single-threaded, meaning it can only serve a single<br />

request at a time.<br />

There’s got to be a better way!<br />

14.6 Serving requests<br />

Rather than taking this approach, we’re going to show you how to use the Passenger<br />

gem along with the nginx web server to host your application. The benefit of this is<br />

that when a request comes into your server, it’s handled by nginx and an nginx module<br />

provided by the Passenger gem, as shown in figure 14.3.<br />

When the client sends a request to the server on port 80, nginx will receive it.<br />

nginx then looks up what is supposed to be serving that request and sees that Passenger<br />

is configured to do that, and so passes the request to Passenger.<br />

Passenger manages a set of Rails<br />

instances (referred to as a pool) for<br />

you. If Passenger hasn’t received a<br />

request in the last five minutes, Passenger<br />

will start a new instance, 28<br />

passing the request to that instance,<br />

with each instance serving one<br />

request at a time. The more instances<br />

you have, the more (theoretical) 29<br />

requests you can do. If there has been<br />

a request within that timeframe, then<br />

the request is passed to one of the<br />

instances in the pool already<br />

launched by a previous request. 30<br />

web browser<br />

nginx<br />

passenger<br />

instance #1<br />

instance #2<br />

Figure 14.3 Nginx request path<br />

27 https://github.com/leehambley/capistrano-handbook/blob/master/index.markdown.<br />

28 The passenger_pool_idle_time configuration option is responsible for this: http://www.modrails.com/<br />

documentation/Users%20guide%20Nginx.html#PassengerPoolIdleTime.<br />

29 There’s a hardware limit (when you run out of CPU and RAM) that will be reached if too many instances are<br />

started up. Things can get slow then.<br />

30 Passenger will scale up instances depending on the speed of requests coming to the application. The maximum<br />

number of application instances running at any one time by default is six, and can be configured by the<br />

passenger_max_pool_size setting: http://www.modrails.com/documentation/Users%20guide%20Nginx<br />

.html#PassengerMaxPoolSize.<br />

407

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!