Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: What is the use of web servers (Apache / nginx / passenger)?

Hi I've been learning rails for the past half year and have a few apps up on Heroku. So for me I thought deploying apps onto the world wide web was just as simple as heroku push. However, I just got my first internship doing Rails and one of my seniors is talking about Apache and Nginx and I'm not sure how they fit in the picture, since I thought apps consisted of only Rails + cloud app platform. I have looked it up but I still don't get how and where it affects my app life cycle. Can someone explain what/where/when of using web servers?

like image 775
bigpotato Avatar asked Jan 06 '13 21:01

bigpotato


People also ask

What are Nginx and Apache used for?

Apache and Nginx are the two most common open source web servers in the world. Together, they are responsible for serving over 50% of traffic on the internet. Both solutions are capable of handling diverse workloads and working with other software to provide a complete web stack.

What web server does Rails use?

What is WEBrick. The Ruby standard library comes with a default web server named WEBrick. As this library is installed on every machine that has Ruby, most frameworks such as Rails and Rack use WEBrick as a default development web server.

What is Passenger Nginx?

The Passenger Nginx module registers Passenger-specific configuration options inside Nginx. You, the administrator, configure Passenger by adding Passenger-specific configuration options to the Nginx configuration file. Restart or reload Nginx to apply any configuration changes.

Does Rails use Apache?

The most common way to deploy a Rails application is with Apache and Passenger. Follow the steps below: Create a new file at installdir/httpd-vhosts.


1 Answers

So you've got your Rails app, and as you know you've got controllers and actions and view and what not.

When a user in their browser goes to your app on Heroku, they type in the URL which points to the Heroku servers.

The Heroku servers are web servers that listen to your users that type in the URL and connect them to your Rails application. The rails application does its thing (gets a list of blog posts or whatever) and the server sends this information back to your user's browser.

You've been using a web server the whole time, just it was abstracted away from you and made super simple thanks to Heroku.

So the life cycle is somewhat like this:

Whilst you've been building your applications on your development machine you've probably come across the command rails server. This starts a program called WEBrick which is a web server, and listens on port 3000. You go to your app via http://localhost:3000.

WEBrick listens on port 3000 and responds to requests from users, such as the "hey give me a list of posts" command.

When you push your code into production (in your experience via heroku push) you're sending your code a provider who takes care of the production equivalent of rails server for you.

A production setup (which your senior developers are talking about) is a bit more complex than your local rails server setup on your development machine.

In production you have your Rails server (often things like Unicorn, Passenger) which takes the place of WEBrick.

In a lot of production setups, another server, such as Apache or nginx is also used, and is the server that the user connects to when they go to your application.

This server exists often as a bit of a router to work out how different types of requests should be handled. For instance, requests to static files (css, images, javascript etc) that are storted on the server might just be processed directly by Apache or nginx, since it does a fantastic (and fast) job of sending static assets back to the client.

Other requests, such as "get me a list of all blog posts" get passed onto the Rails server (Unicorn, Passenger etc) who in turn do the required work and send the response to Apache/nginx, who send it back to the client.

Heroku does all this for you in a nice easy to use package, but it sounds like the place your working at manages this themselves, rather than using Heroku. They've setup their own bunch of web servers, and will have their own way doing an equivalent of heroku push which will send the code to the servers, and make sure they're up and running ready to respond to user requests.

Hope that helps!

like image 64
Michael Shimmins Avatar answered Oct 28 '22 01:10

Michael Shimmins