Breaking News
Home / Sys Admin / Sysadmin Tutorials / How to proxy static content via Apache Server?

How to proxy static content via Apache Server?

Setup Apache to serve imagesBasically the content in a website can be categorized into to categories:

  • Dynamic content – generated by using of Web Sever/Application Server running JSP, Servlets etc. This content is dynamically assembled and severed to the browser hence called as Dynamic.
  • Static Content – as the name implies this content does not change with request and is static. All multimedia things like image, audio, css, documents etc. come under this category.

Assume a situation where you started your site with a single, yet powerful server but with limited disk space and bandwidth, generally this is the case with a shared VSP hosting. As your website content grows, you run out of disk space on the main server and decide to buy a shared hosting account from vendors like BlueHost, HostGator etc. This gives you almost unlimited space and bandwidth but processing power is low. So in order for you site to scale disk wise efficiently , you can setup you main server to serve only the dynamic content as before and setup the another server to serve the static content, thus you would able to host more static content for your website in this way.

Apache Proxy Static Content

In order to do so, we have to setup a Proxy Pass in the main Apache Server(dynamic). This can done at the Apache Server Level by adding the Proxy directive in the httpd.conf file of the server(normally located under /etc/httpd/conf) or in the .htaccess file (located in your web root public_html).

Any changes done in httpd.conf needs your server to be restarted for your changes take effect. However if you add in .htacess, these changes are applied immediately, so pickup either of the option what ever works for you.

Proxy Static content Apache Scenario – Proxy all content in given directory

Let’s say all your static content was stored in the images folder of your root directory as http://www.yourwebsite.com/images .

Now we want this to be served by another Apache Server rather than the main one. So first copy all your files and folder from images directory into the new server. Let’s say the new Apache Server is located at this address

http://images.yourwebsite.com, then add the following directive in your config file in the VirtualHost Section.

ProxyPass /images http://images.yourwebsite.com

ProxyPassReverse /images http://images.yourwebsite.com

The above example shows the new Address is an subdomain, but it could be another separate server like all together, so you can specify the IP instead as well.

ProxyPass /images http://92.293.229.24

ProxyPassReverse /images http://92.293.229.24

It could be local sever e.g Light Httpd serving static files at port 81 locally in the case this directive would be.

ProxyPass /images http://localhost:81

ProxyPassReverse /images http://localhost:81

 

Proxy Static Files based on extension Apache Scenario – Proxy content based on file extension

Lets say all your static content was stored in various folder and not just one. So in this case the above approach would not work. So in this case we would identify all the static files by extension and serve them from the static server.

Then add this in your config file in the VirtualHost Section.

RewriteEngine On
RewriteRule “^/(.*).(jpg|jpeg|gif|png|swf|pdf|avi|mpeg|mpg|mp3)$” “http://www.yourwebsite.com/$1.$2” [P] ProxyPassReverse / http://www.yourwebsite.com/

This directive will redirect all .jpg .jpeg .gif .png … file extensions to your server which hosts static files. As in first

Before you use the proxy directive, make sure the directive to load the Proxy modules and RewriteRule is in the config file.

LoadModule proxy_module modules/mod_proxy.so

LoadModule rewrite_module modules/mod_rewrite.so[pb_builder]

Advertisment ad adsense adlogger