3

Right now, I have a site entirely on an EC2 instance including all static files like css, js, and images. I'm wondering about the best way to handle moving them. For example, in my <HEAD> on a given html page I have this reference to a .js file:

<script src="js/somefile.js"></script>

What I want to avoid is having to go in and update this (and a bunch of others) to something like:

<script src="https://SOMETHING.cloudfront.net/js/somefile.js"></script>

I'm not worried about moving the files to S3 (yet), but more about how to rewrite these links. I'm thinking there has to be some way around this.

For completeness, I'm currently using a load balancer in front of the EC3 instance (which is also where I have my SSL). I'm also using Route 53 for DNS.

Any insights would help.

1

You lose much of the benefit of moving the static files to S3 if you don't want to change the links, as this means you'll still need to run kind of reverse proxy to route requests correctly to either S3 or your EC2 instance, depending on whether it's a static file or not.

That being said, there are plenty of recipes for hooking up e.g. nginx as a reverse proxy, for instance https://coderwall.com/p/rlguog/nginx-as-proxy-for-amazon-s3-public-private-files. You could then put nginx on each of your load balanced EC2 instances, and server from S3 or from local services depending on whether the content is static or not.

  • yes, you can hook location /js/ and proxy to S3 behind the scene with using Nginx S3 proxy configuration. More to say you can cache files and revalidate by time or on deployment hook: gist.github.com/mikhailov/9639593 – Anatoly Mar 27 '15 at 13:54
0

Various rewrite modules for say Apache, nginx, and even IIS can do this on the fly, as can most CMS. But that adds a lot of overhead to each request.

To do the replacement automatically and safely, you really need an HTML parser for whatever scripting language. Regex for this case would be difficult to do perfectly safely unless you always use the exact same spacing and order of arguments in tags and CSS references. And you also are sure you don't have HTML in embedded <code> or <pre> or <script> tags. Even more challenging would be URLs it hat re part of strings in JS code.

If the files aren't actually HTML, but PHP or whatever, you'll need a full parser for that language to be safe. Or use a regex search and replace with manual confirmation.

A final option would be a full-proxy CDN with custom origin. Cloudflare comes to mind. Cloudfront can do that too, but custom SSL certain are very expensive with cloudfront.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.