
Introduction
Apache is a web server application that enables the hosting and delivery of web applications using your domain. Direct user requests to the Apache web server increase the server load and contribute to your general web application performance. You can integrate the Apache web server with your Vultr CDN Zone URL to deliver cached assets to your site visitors.
This article explains how to use Vultr CDN Zones with the Apache web server. You will set up a new Apache virtual host configuration to forward specific request patterns to your Vultr CDN to improve the general web server performance.
Prerequisites
Before you begin:
- Create a Vultr CDN Pull Zone or a Vultr CDN Push Zone to deliver cached assets.
- Deploy a LAMP instance using the Vultr Marketplace application.
- Set up a domain A record that points to the public server IP address.
- Access the server using SSH as a non-root user with sudo privileges.
Locate the Apache Virtual Host Configuration
Test the active Apache configuration for errors.
console$ apachectl configtest
Switch to the main Apache data directory.
console$ cd /etc/apache2/
Search all files in the Apache directory using your domain name to verify the active virtual host configuration file. Replace
example.comwith your actual domain name.console$ grep -r "example.com" /etc/apache2/
Output:
/etc/apache2/sites-available/example.com.conf: ServerName www.example.comThe above output returns
example.confas the active virtual host configuration file associated with theexample.comdomain. By default, Apache uses the/etc/apache2/sites-available/000-default.conffile when no virtual host configuration is available on your server.View your virtual host configuration file and verify the working
DocumentRootdirectory.console$ cat /etc/apache2/sites-available/example.com.conf | grep DocumentRoot
Output:
DocumentRoot /var/www/htmlSwitch to your document root directory.
console$ cd /var/www/html/
Create a new
imagesdirectory to test the Apache request patterns.console$ sudo mkdir images
Switch to the directory.
console$ cd images
Upload sample files to the directory to test your Apache request patterns.
Method 1: Set Up Response Modification
Apache response modification matches requests and modifies the original request URL with your Vultr CDN URL. The response modification process uses mod_substitute and mod_filter modules to filter and modify requests to your Vultr CDN URL. Follow the steps below to set up response modification and forward request patterns to your Vultr CDN.
Enable the Apache
mod_substitutemodule.console$ sudo a2enmod substitute
Output:
Enabling module substitute. To activate the new configuration, you need to run: systemctl restart apache2Enable the Apache
mod_filtermodule to filter requests by MIME type.console$ sudo a2enmod filter
Restart Apache to activate the modules.
console$ sudo systemctl restart apache2
Open the Apache virtual host configuration file using a text editor such as Nano.
console$ sudo nano /etc/apache2/sites-available/example.com.conf
Add the following configurations to the file before the
</VirtualHost>directive. Replaceexample.comandcdn-example88.vultrcdn.comwith your actual values.apacheconfAddOutputFilterByType SUBSTITUTE text/html Substitute s|example.com/images/|cdn-example88.vultrcdn.com/images/|i Substitute s|example.com/css/|cdn-example88.vultrcdn.com/css/|i Substitute s|example.com/js/|cdn-example88.vultrcdn.com/js/|i
Save and close the file.
The above response modification configuration matches all requests with either the
images,css, orjsdirectory pattern. All files under the matching request pattern are modified to the Vultr CDN URL. For example, the requestexample.com/images/image.pngis modified tocdn-example88.vultrcdn.com/images/image.png. Within the configuration:AddOutputFilterByType SUBSTITUTE: Filters the request patterns by MIME type./text/htmlmatches HTML requests including embedded elements such as images, stylesheets, and scripts. Other MIME types includetext/css,application/javascript,application/custom,image/jpg,image/png,image/webpwhich filter requests depending on your web application structure.Substitute s|.....|...|: Converts the sources|URL to match with the destination|Vultr CDN URL. For example,Substitute s|example.com/images/|cdn-example88.vultrcdn.com/images/|imodifies theexample.com/imagesrequest pattern to your Vultr CDN URLcdn-example88.vultrcdn.com/images.$1: Loads the initial request type to match the correct Vultr CDN URL.|i: Matches all casing formats in the request pattern. For example, bothexample.com/images/IMAGE.pngandexample.com/images/logo.pngare modified to the respective Vultr CDN URL.
To match additional request patterns in your configuration, add new
Substitutedirectives to modify request patterns to your Vultr CDN URL. Your modified Apache virtual host configuration should look like the one below:apacheconf<VirtualHost *:80> <Directory /var/www/html/> Options -Indexes Require all granted </Directory> ServerName example.com DocumentRoot /var/www/html DirectoryIndex index.htm index.html AddOutputFilterByType SUBSTITUTE text/html Substitute s|example.com/images/|cdn-example88.vultrcdn.com/images/|i Substitute s|example.com/css/|cdn-example88.vultrcdn.com/css/|i Substitute s|example.com/js/|cdn-example88.vultrcdn.com/js/|i </VirtualHost>
Test the Apache configuration for errors.
console$ apachectl configtest
Restart Apache to apply your configuration changes.
console$ sudo systemctl restart apache2
Method 2: Set Up Redirects
Apache redirects specific request types by rewriting the original request URL with your Vultr CDN URL using the mod_rewite module. Each request pattern is redirected to the respective Vultr CDN URL that delivers cached data in response to the request. For example, a request to example.com/image.png redirects to the CDN URL cdn-example88.vultrcdn.com/image.png. Follow the steps below to redirect specific request patterns to your Vultr CDN URL.
Enable the Apache
mod_rewritemodule.console$ sudo a2enmod rewrite
Output:
Enabling module rewrite. To activate the new configuration, you need to run: systemctl restart apache2Restart the Apache web server to activate the module.
console$ sudo systemctl restart apache2
Open the Apache virtual host configuration file.
console$ sudo nano /etc/apache2/sites-available/example.com.conf
Add the following configurations to the file before the
</VirtualHost>directive. Replaceexample.comandcdn.example88.vultrcdn.comwith your actual values.apacheconfRewriteEngine On RewriteCond %{HTTP_HOST} ^example.com RewriteRule \.(jpg|jpeg|png|gif|webp|css|js|scss|svg|pdf|txt)$ https://cdn-example88.vultrcdn.com%{REQUEST_URI} [L,R=301]
Save and close the file.
The above configuration redirects all request types that match the file types
.jpeg,.jpg,.png,,.webp,.js,.cssto your Vultr CDN URL. Within the configuration:RewriteEngine On: Enables the Apachemod_rewritemodule.RewriteCond %{HTTP_HOST}: Sets the request rewriting conditions to match your host domain URL pattern.RewriteRule: Defines the redirect rule to rewrite matching request patterns in a regular expression format. The value\.(jpg|jpeg|png|gif|webp|css|js|scss|svg|pdf|txt)matches all requests with the declared file types to your Vultr CDN URL. For example, the requesthttps://example.com/image.pngredirects tohttps://cdn-example88.vultrcdn.com/image.png[R=301,L]: Permanently redirects all matching requests to your Vultr CDN URL with a301 Moved Permanentlystatus code.Lsets the redirect condition tolastwithout additional rewrite rules.
Your modified Apache virtual host configuration should look like the one below:
apacheconf<VirtualHost *:80> <Directory /var/www/html/> Options -Indexes Require all granted </Directory> ServerName example.com DocumentRoot /var/www/html DirectoryIndex index.htm index.html RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com RewriteRule \.(jpg|jpeg|png|gif|webp|css|js|scss|svg|pdf|txt)$ https://cdn-example88.vultrcdn.com%{REQUEST_URI} [L,R=301] </VirtualHost>
Test the Apache configuration for errors.
console$ apachectl configtest
Restart the Apache web server to apply the configuration changes.
console$ sudo systemctl restart apache2
Conclusion
You have set up an Apache web server to forward request patterns to your Vultr CDN Zone's URL. You can either apply the request forwarding rules in your .htaccess file or the virtual host configuration file to improve your web application performance and user experience. For each configuration method, view your browser developer tools and navigate to the Network or Sources tab to verify all files delivered from your Vultr CDN Zone's URL.