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.
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
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
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.