How a web server works

Knowing a little about how a web server works can help you to make more informed decisions about how your website is set up. Although this is starting to get a bit technical we will try and keep it simple.

Before we get too far it is worth talking about the difference between a server and a web server. A web server is a software program that does the job of finding the web page you want and sending it back to your computer. The computer that it runs on is called a server, and is a physical piece of hardware equipment.

If you haven’t already read the previous articles in this series you might want to read them now, especially if you aren’t familiar with the concept of HTML:

Let’s get started

Let’s say you are looking for a new camera and click on a link to a page with the address www.example.com/products/cameras/index.html. (That link doesn’t work, by the way.)

The first part of that address—”www.example.com”—is used by the internet to locate the particular web server that has the information you want. The bit that comes after that, “/products/cameras/index.html” tells the web server at www.example.com exactly what page you want.

Static web pages

For a static web page, the address “/products/cameras/index.html” refers to a specific file (index.html) that is stored in a specific folder (the “cameras” folder, inside the “products” folder on the server.

Diagram showing how a static web page is served from a folder structure via the web server back to your computer

This is the same as the way files are stored on your computer. For example, you might have a holiday picture stored at “/documents/pictures/holiday/myphoto.jpg”.

The file name “index.html” is a common convention used to denote the main file in that particular folder. (There are other conventions, but this is one of the most commonly used.) Web servers are often set up to assume that the main file in a folder is called “index.html”,  so it is okay not to put the file name in at all. (“www.example.com/products/cameras” will prompt the web server to get “index.html” from the “/products/cameras” folder.)

The “.html” at the end of the file name tells the web server to look for a html page, rather than another type of web page. (More on that later.)

This type of web page is referred to as static, as the information on the page does not change unless someone goes in and edits the page to change it. This the way websites were traditionally structured and stored on the server.

Dynamic web pages

Let’s say that the company that runs this web site wants the page at “www.example.com/products/cameras” to only contain information about cameras that are currently in stock in their online store. That can change on a regular basis through out the day, as cameras are sold, and new ones come into stock. Rather than having someone updating the static page regularly through the day, they feed information from the sales and inventory systems to a database on the server. When you request the page the web server checks the database and produces an updated page.

Here’s how that works.

Diagram of how a dynamic web page is delivered to your computer, via the web server, script engine and database

Dynamic pages not only contain HTML, but also a type of programming code called a scripting language. One of the most common scripting languages is called PHP, although there are others. Pages that involve PHP script often end in “.php” instead of “.html” This tells the web server that that page isn’t stored as a static file in a folder ready to be served up. Instead it has to be assembled by some special software on the server called a script engine. In this case, the PHP script engine will look in the database to find out what cameras are currently in stock. It then will only include details of those cameras on the web page which it then hands back to the web server to be sent to your browser.

This means that the content on a dynamic page can be different each time the web server sends it out, without anyone having to go in and edit the page.

Websites can include a combination of both static and dynamic pages.

Diagram showing how a website can contain both dynamic and static pages

What it all means

In the early days of the web most sites contained only static web pages. But over time many sites have needed to include at least some dynamic pages to make them more flexible. It is common now for sites to contain only dynamic pages—even when some of the content doesn’t change very much.

Static web pages are often seen as being more secure (or harder to hack). The only program involved in serving up a static page is the web server itself. These have come to be very secure over the years. Static web pages require no extra processing by the web server and so are generally delivered more quickly. They are also simpler to set up.

On the other hand, dynamic web pages involve developing and running scripts. This makes dynamic web pages more complex to develop and maintain. Also, if it has not been carefully coded a hacker may be able to exploit a weakness in the script to make it do something other than what is intended. This could include using the script to send spam emails, or to access a database where login details or even credit card details are stored. The advantage of dynamic pages is that they generally need less manual intervention to keep them up-to-date.

Many content management systems use dynamic web pages as the basis for how they work. We’ll look more at how content management systems work in our next article.

Published: Wednesday, 1 August 2012