Apache · December 17, 2023

Apache for Newbie: Set up Apache with mod_request

Apache for Newbies: Set up Apache with mod_request

Apache is one of the most popular web servers in the world, known for its flexibility, reliability, and security. If you are new to Apache and want to learn how to set it up with mod_request, this article is for you. Mod_request is an Apache module that allows you to modify HTTP request headers, making it a powerful tool for customizing your web server's behavior.

What is mod_request?

Mod_request is an Apache module that provides a set of directives for manipulating HTTP request headers. With mod_request, you can add, remove, or modify request headers before they are processed by your web server. This can be useful for a variety of purposes, such as rewriting URLs, blocking certain requests, or adding custom headers for authentication or caching.

Installing Apache with mod_request

Before you can use mod_request, you need to have Apache installed on your server. If you haven't installed Apache yet, you can follow these steps:

  1. Update your system's package manager: sudo apt update
  2. Install Apache: sudo apt install apache2
  3. Start Apache: sudo systemctl start apache2
  4. Enable Apache to start on boot: sudo systemctl enable apache2

Once Apache is installed, you can enable mod_request by running the following command:

sudo a2enmod request

This command enables the mod_request module and creates a symbolic link in the /etc/apache2/mods-enabled directory.

Configuring mod_request

After enabling mod_request, you need to configure it to modify the request headers. The configuration file for Apache is located at /etc/apache2/apache2.conf. Open this file in a text editor and add the following lines:

<IfModule mod_request.c>
  RequestHeader set X-Forwarded-Proto "https"
</IfModule>

In this example, we are using the RequestHeader directive to set the value of the X-Forwarded-Proto header to "https". This can be useful if you are using a reverse proxy or load balancer that terminates SSL/TLS connections.

After making the changes, save the file and restart Apache for the changes to take effect:

sudo systemctl restart apache2

Testing mod_request

To test if mod_request is working correctly, you can use the curl command-line tool to send an HTTP request to your server and check the response headers. Run the following command:

curl -I http://localhost

You should see the X-Forwarded-Proto header in the response headers with the value "https". This confirms that mod_request is modifying the request headers as expected.

Summary

Setting up Apache with mod_request allows you to modify HTTP request headers, giving you greater control over your web server's behavior. Mod_request is a powerful tool that can be used for various purposes, such as URL rewriting, request blocking, or adding custom headers. By following the steps outlined in this article, you can easily install and configure Apache with mod_request. For more information about VPS hosting and how it can benefit your website, visit Server.HK.