• Posted on February 20, 2013

How Web Servers Work

Every website has a web server, but how does it work. How does it know what content you want and what code to execute? To begin, let’s get a basic understanding of what a web server is. A web server is a server that hosts a website. Often times the server will also run mail, database, FTP and SSH services. If the server is for a large website, they will often have multiple web servers running to distribute the request load to each server. They have these servers in “Server Farms”, and then they have server farms in different parts of countries and possibly other countries if they have a target audience there.

Receiving Requests

The first part the web server does, is listen on port 80, or port 443 if using HTTPS secure requests. The port can also be changed to almost anything but visitors won’t be able to access the site unless specified the port directly. Once the server gets a request on that port, the server parses the headers into all the require parameters such as cookies, file, host, user agent, post data and other information.

The server then checks the host, and loads the root directory for that host. Often times it is public_html, www, or www-data. The server then opens the requested file if it exists. If the file doesn’t exist, contains illegal characters or locations, or has code that didn’t execute properly it will throw a HTTP Error. A list of all available error codes and their description can be found at http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

Executing Code

Now that the server has the HTTP Header information and checked the file it can start getting the content it needs to return to the visitor. The server will check the mime type of the file, if it is HTML, text, CSS, JavaScript and a few other formats it will return it just how it is displayed, since nothing more is required. If the file is a PHP file or other web scripting language such as Python, Ruby, Perl and others the file gets passed to a processor. The processor executes the code, and then returns the code back to the web server in a HTML format most of the time. The web server then returns the content to the visitor. This process is performed for every file the user visits on the site, and all images, styles, JavaScript files that are also included.

File Compression

On the request the user sends to the web server, they will often include a “Accept-Encoding” header that includes the methods it allows such as gzip. Gzip is a program that compresses files and makes them much smaller and faster to transfer. When the web server sees a Accept-Encoding header, it finds the accepted methods. If the server has that encoder installed, the server will then pass the file code through the compressor and then send it to the user. The user’s web browser or software will then decompress the file and have a normal document. This saves on bandwidth and increases page loading times.

Sending and Getting Cookies

Almost all websites use cookies for tacking, keeping you logged in, showing you specific information and much more. If you have a cookie set, all the requests you make to that website will include a “Cookie” header and send the cookie information. The server will then use these cookies if requested. Cookie data is usually requested by the web server pages that are in a web scripting language such as PHP, Perl, etc. The code will check the cookies against a database and grab the information they require to show you the appropriate data to your user.

If the web server needs to change your cookie, it will respond with a “Set-Cookie” header. This header is handled by the web browser and saves the cookie to your computer if it is not expired. Set-Cookie is sometimes used to delete cookies by setting the “expire date” to a previous date and removing the value for a cookie.

Types of Web Servers

Since every site requires a web server, there are many different types but with everything else there are the most popular few and then a ton of software that is hardly used. Two popular web server’s software is Apache and Nginx. These two services are easy to setup, have a ton of features and are well supported. Routers, printers, NAS’s also have web servers built into their system, but they do not use Apache or Nginx because they don’t require all those features. Most embedded systems that have web servers will build a custom web server or use one similar to Micro-httpd or Mini-httpd.