# ch05: Web Servers

![5.Web服务器](https://tva1.sinaimg.cn/large/007S8ZIlly1ggdjavgwycj31k90t977x.jpg)

## 0. Guide

* Survey the many different types of software and hardware web servers.
* Describe how to write a simple diagnostic web server in Perl.
* Explain how web servers process HTTP transactions, step by step.

## 1. What Real Web Servers Do

![](http://ww2.sinaimg.cn/large/006tNc79ly1g5k0xkprwjj317q0u045p.jpg)

1. Set up connction: accept a client connection, or close if the client is unwanted.
2. Receive request: read an HTTP request message from the network.
3. Process request: interpret the request message and take aciton.
4. Access resource: access the resource specified in the message.
5. Construct response: create the HTTP response message with the right headers.
6. Send response: send the response back to the client.
7. Log transaction: place notes about the completed transaction in a log file.

## 2. Step 1: Accepting Client Connections

### **2.1 Handling New Connections**

Establish a connection, adds the new connection to the server's list of existing web server connections and prepares to watch for data on the connection.

### **2.2 Client Hostname Identification**

Most web servsers can be configured to convert client IP addresses into client hostnames, using "reverse DNS". (can take a very long time)

Web servers can use the client hostname for detailed access control and logging.

In Apacha, use _HostnameLookups_ configuration directive:

```
HostnameLookups off
<Files ~ "\.(html|htm|cgi)$">
        HostnameLookups on
</Files>
```

### **2.3 Determining the Client User Through ident**

The _ident_ protocol lets servers find out what username initiated an HTTP connection. This information is particularly useful for web server logging.

![](http://ww2.sinaimg.cn/large/006tNc79ly1g5k1xnne1hj31je0m40ys.jpg)

If a client supports the _ident_ protocol, the client listens on TCP port 113 for _ident_ requests.

_ident_ can work inside organizations, but it does not work well across the public Internet for many reasons.

## 3. Step 2: Receiving Request Messages

Web server reads out the data from the network connction and parses out the pieces of the request message:

![](http://ww2.sinaimg.cn/large/006tNc79ly1g5k5czzattj31jw0kigrd.jpg)

### **3.1 Connection Input/Output Processing Architectures**

Different web server architectures service requests in different ways:

![](http://ww1.sinaimg.cn/large/006tNc79ly1g5k66l1ausj30yz0u07b4.jpg)

_Multiplexed I/O servers_

​ All the connections are simultaneously watched for ativity. When a connection changes state, a small amount of processing is performed on the connection; when that processing is complete, the connection is returned to the open connection list for the next change in state. Work is done on a connection only when there is something to be done; threads and processes are not tied up waiting on idle connections.

## 4 Step 3: Processing Requests

## 5 Step 4: Mapping and Accessing Resources

Mapping the URI from the request message to the proper content or content generator on the web server.

### **5.1 Docroots**

Typically, a special folder in the web server filesystem is reserved for web content. This folder is called the _document root_, or _docroot_.

![](http://ww4.sinaimg.cn/large/006tNc79ly1g5k6q551fdj31ji0iswlx.jpg)

Configuration for Apache:

```
DocumentRoot /usr/local/httpd/files
```

Servers are careful not to let relative URLs back up out of docroot and expose other parts of the filesystem.

**5.1.1 Virtually hosted docroots**

Virtually hosted web servers host multiple web sites on the same web server, giving each site its own distinct document root on the server:

![](http://ww3.sinaimg.cn/large/006tNc79ly1g5k75musykj31ji0k847f.jpg)

**5.1.2 User home directory docroots**

![](http://ww3.sinaimg.cn/large/006tNc79ly1g5k7a82hmpj31je0jk7c8.jpg)

### **5.2 Directory Listings**

Directory URLs, the path resolves to a directory, not a file.

Most web servers look for a file named _index.html_ or _index.htm_ inside a directory to represent that directory.

Configure the set of filenames that will be interpreted as default directory files using _DirectoryIndex_:

```
DirectoryIndex index.html index.htm home.html home.htm index.cgi
```

### **5.3 Dynamic Content Resource Mapping**

Apache lets you map URI pathname components into executable program directories, and execute the program in a corresponding server directory.

Like _cgi_.

## 6. Step 5: Building Responses

### **6.1 Response Entities**

If the transaction generated a response body, the content is sent back with the response message.

### **6.2 MIME Typing**

The web server is responsible for determining the MIME type of the response body:

_mime.types_

​ Server can use the extension of the filename to indicate MIME type.

![](http://ww4.sinaimg.cn/large/006tNc79ly1g5k9i614k8j31d90u0nbg.jpg)

_Magic typing_

​ The Apache web server can scan the contents of each resource and pattern-match the content against a table of known patterns to determine the MIME type for each file.

_Explicit typing_

​ Servers can be configured to force particular files or directory contents to have a MIME type, regardless of the file extension or contents.

_Type negotiation_

​ Some web servers can be configured to store a resource in multiple document formats. In this case, the web server can be configured to determine the "best" format to sue by a negotiation process with the user.

### **6.3 Redirection**

Web servers sometimes return redirection responses instead of success messages. A web server can redirect the browser to go elsewhere to perform the request. The Location response header contains a URI for the new or preferred location of the content. Redirects are useful for:

_Permanently moved resources_

​ A resource might have been moved to a new location, or otherwise renamed, giving it a new URL. 301

_Temporarily moved resources_

​ A resource is temporarily moved or renamed. 303 See Other and 307 Temporary Redirect

## 7. Step 6: Sending Responses

Compute the **Content-Length** correctly.

## 8. Step 7: Logging
