A Virtual Server for Both Dynamic and Static Content - Linux Web Server Development (2015)

Linux Web Server Development (2015)

Chapter 10 A Virtual Server for Both Dynamic and Static Content

Implementing Multiple Rules

We next enable the PHP Server to also provide static HTML site. A new Document Root for the static content was previously created:

$ sudo mkdir /var/www2/www

Recall that the file /var/www2 was set as the document root for the PHP Server.

Using gedit or another editor, create the file index.html in the www directory:

$ sudo gedit /var/www2/www/index.html

Enter the following HTML code in index.html:

<!DOCTYPE html>

<html lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html;

charset=utf-8"/>

<title>Cherokee Server</title>

</head>

<body bgcolor="yellow">

</body>

</html>

Next, add the new directory to the Directory Indexes textbox of the virtual server, separating the two Directory Indexes with a comma:

index.php, www/index.html

Click the Behavior tab and then click the Rule Management button. Here you will create a rule for managing static HTML content. Click the Add Behavior Rule button, the button with a blue plus sign in the upper-left area of the window.

In the dialogue window that appears, select Extensions in the Rule Type drop-down menu and enter html in the Extensions check box. Click the Add button.

The new rule, called Extensions html, appears in the rule list in the left area of the Administration Panel:

Click next at the Rule tab. The previous entries in the dialogue box are reflected here.

Click the Handler tab. In the Handler drop-down menu, select the Static Content option. In the Document Root text field, enter:

/var/www2/www

Click the SAVE button to confirm the changes and then click the Graceful Restart button to commit the changes.

Testing the Extended Server

Using the online network tool tools.pingdom.com, you’ll test your site. Enter the URL webserver.dynu.com in the text field. The Directory index of the PHP Server (index.php) is dispatched, as previously described in Chapter 9. Run the test for a second time and enter the URL webserver.dynu.com/index.html. This time the Static Server provides the new colored page index.html that we created in this section.

Using Logical Operators with the Cherokee Rules

Next, modify the Extensions html rule to create a compound rule with two rules combined with the AND logical operator. Cherokee provides three logical operators to combine rules: AND, OR and NOT. The rule handler remains the same as with Extensions html. When a request for a file with extension html arrives on port 8080, the handler Static Content responds with the directory index (which is an HTML file).

Select the Rule tab and click on the Extension html rule in the list on the left to modify this rule. Click the AND button and a new Rule Type menu appears. Select rule type Incoming IP/Port and then Port 8080 in the Incoming IP/Port menu. Click the SAVE button and restart the server. The compound rule, called Extensions html AND Port 8080 is activated.

To ensure that a request that breaks this rule will not be served, disable the Default rule. In the rule list in the Handler tab, click Default and then select the option None in the Handler menu. This will reject, for example, a request for index.html on port 8181.

To test the new rule, use the online network tool tools.pingdom.com. This time, you can use the public IP address of the router to test the server. Find your current IP address with an online tool or just type find my ip in the Google search textbox. Your current IP address, for example 87.202.110.63, will be displayed. In the Test Now textbox field of tools.pingdom.com, enter:

87.202.110.63:8080/index.html

Click on the Test Now button to view the results. The following page is dispatched:

Run the test once more. This time, use port number 8181 (which does not satisfy the second condition of the compound rule):

87.202.110.63:8181/index.html

The result is displayed bellow:

Click on the browser’s window to zoom in. The HTTP 405 Error message (Method Not Allowed) is displayed.