I’ve been looking for ways to speed up my site recently and came across this interesting article on seamlessly integrating nginx with Apache to handle asset files without requiring a CDN subdomain. This works by checking the requests file extension for .js, .jpg, .pdf etc and if not found, proxies the request to Apache and serves the results.
Benefits
You won’t need to modify all your pages/posts updating asset locations to point to a subdomain! Everything will ‘just work’.
Issues/Drawbacks
There are 2 issues I’ve found with this setup:
- Because Apache is now running on port 8080, your mod_rewrite redirects will now redirect to that port. You won’t be able to use RedirectMatch anymore, however below is the solution I came up with:
RewriteEngine on RewriteRule ^foo\.php$ http://%{HTTP_HOST}:80/bar.php [R=301,L] - You can no longer use .htaccess redirects for any asset files nginx is serving. Instead, use nginx redirects. Below is an example:
rewrite ^\/foo\.jpg$ http://173.255.221.210/bar.jpg permanent;
For more information on nginx redirects, see the official documentation.