
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.
Before you begin:
Test the active Apache configuration for errors.
Switch to the main Apache data directory.
Search all files in the Apache directory using your domain name to verify the active virtual host configuration file. Replace example.com with your actual domain name.
Output:
The above output returns example.conf as the active virtual host configuration file associated with the example.com domain. By default, Apache uses the /etc/apache2/sites-available/000-default.conf file when no virtual host configuration is available on your server.
View your virtual host configuration file and verify the working DocumentRoot directory.
Output:
Switch to your document root directory.
Create a new images directory to test the Apache request patterns.
Switch to the directory.
Upload sample files to the directory to test your Apache request patterns.
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_substitute module.
Output:
Enable the Apache mod_filter module to filter requests by MIME type.
Restart Apache to activate the modules.
Open the Apache virtual host configuration file using a text editor such as Nano.
Add the following configurations to the file before the </VirtualHost> directive. Replace example.com and cdn-example88.vultrcdn.com with your actual values.
Save and close the file.
The above response modification configuration matches all requests with either the images, css, or js directory pattern. All files under the matching request pattern are modified to the Vultr CDN URL. For example, the requestexample.com/images/image.png is modified to cdn-example88.vultrcdn.com/images/image.png. Within the configuration:
AddOutputFilterByType SUBSTITUTE: Filters the request patterns by MIME type. /text/html matches HTML requests including embedded elements such as images, stylesheets, and scripts. Other MIME types include text/css,application/javascript,application/custom,image/jpg,image/png, image/webp which filter requests depending on your web application structure.Substitute s|.....|...|: Converts the source s| URL to match with the destination | Vultr CDN URL. For example, Substitute s|example.com/images/|cdn-example88.vultrcdn.com/images/|i modifies the example.com/images request pattern to your Vultr CDN URL cdn-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, both example.com/images/IMAGE.png and example.com/images/logo.png are modified to the respective Vultr CDN URL. To match additional request patterns in your configuration, add new Substitute directives to modify request patterns to your Vultr CDN URL. Your modified Apache virtual host configuration should look like the one below:
Test the Apache configuration for errors.
Restart Apache to apply your configuration changes.
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_rewrite module.
Output:
Restart the Apache web server to activate the module.
Open the Apache virtual host configuration file.
Add the following configurations to the file before the </VirtualHost> directive. Replace example.com and cdn.example88.vultrcdn.com with your actual values.
Save and close the file.
The above configuration redirects all request types that match the file types .jpeg, .jpg, .png,,.webp,.js,.css to your Vultr CDN URL. Within the configuration:
RewriteEngine On: Enables the Apache mod_rewrite module.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 request https://example.com/image.png redirects to https://cdn-example88.vultrcdn.com/image.png[R=301,L]: Permanently redirects all matching requests to your Vultr CDN URL with a 301 Moved Permanently status code. L sets the redirect condition to last without additional rewrite rules.Your modified Apache virtual host configuration should look like the one below:
Test the Apache configuration for errors.
Restart the Apache web server to apply the configuration changes.
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.
0 Comments
Be the first to comment and share your perspective with the community.