Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for How I Setup My Own Personal CDN using Cloudflare and S3
JavaScript Joel
JavaScript Joel

Posted on • Originally published atjoel.net

     

How I Setup My Own Personal CDN using Cloudflare and S3

What is a CDN?

According toCloudflare a CDN is:

A content delivery network (CDN) refers to a geographically distributed group of servers which work together to provide fast delivery of Internet content.

A CDN allows for the quick transfer of assets needed for loading Internet content including HTML pages, javascript files, stylesheets, images, and videos. The popularity of CDN services continues to grow, and today the majority of web traffic is served through CDNs, including traffic from major sites like Facebook, Netflix, and Amazon.

A properly configured CDN may also help protect websites against some common malicious attacks, such asDistributed Denial of Service (DDOS) attacks.

--What is a CDN?

tl;dr A CDN is a network of computers around the world that deliver content efficiently.

What Can a CDN Provide?

  • Caching to reduce the load on my webserver
  • Geographic Distribution for speedy delivery regardless of where you are
  • DDoS Protection from sudden spikes in traffic
  • Compression of files for faster delivery for user on slow internet

Why Did I Want a CDN?

I often find myself needing to host public static files. I want to do so in a way that is fast, efficient, reliable, and scalable.

Recentlyan article of mine hit #1 on the HN main page.

Screenshot_20201001-222913__01.jpg

I also found out it was trending on Reddit and who knows where else. So long story short. My domain had a load of traffic in the blink of an eye.

joelnet-article-screenshot.png

I was lucky enough to have recently movedjoel.net toHashnode just weeks earlier. My previous web host would not have been able to handle that amount of traffic. Even during this blast of trafficjoel.net was online the entire time.

Since I also host other files, I better be prepared. So I plan on going full CDN.

Cloudflare

This is whereCloudflare comes in. I use them to host the DNS forjoel.net. Cloudflare will provide these CDN featuresby default through their DNS offering, which is also FREE. 🔥

Now all I need is a place to host static files.

AWS Simple Cloud Storage (S3)

You could use any static web host, but I'm choosing S3 for this because it's what I am already familiar with.

It just needs to be online and also allow a custom domain name.

Setting Up My S3 Bucket

The S3 bucket name must match the domain name. In this case, I am usingcdn.joel.net for my bucket name.

image.png

Click Next a bunch of times until you see this screen.

image.png

Be sure to uncheckBlockall public access. Then check the acknowledge popup above.

Keep clicking Next until you can clickCreate Bucket.

Now I need to configure this bucket for Public access. So I head over to Permissions, then Bucket Policy.

image.png

I insert my bucket name,cdn.joel.net, intoResource and then paste this into theBucket policy editor.

{"Version":"2012-10-17","Statement":[{"Sid":"PublicReadGetObject","Effect":"Allow","Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::cdn.joel.net/*"}]}
Enter fullscreen modeExit fullscreen mode

Next, I setupStatic website hosting underProperties.

image.png

I checkUse this bucket to host a website and set up myindex.html and also my404/404.html. I will create a directory for my 404 so that I can host assets related to the 404 in there as well.

image.png

At this point, I can upload a file and make sure it works by accessing theEndpoint from this page.

Configuring Cloudflare

The last step is to set up my Cloudflare DNS to point to my freshly created S3 bucket.

I do this by adding a newCNAME record with the namecdn and the target set as the domain for the S3 bucket.

image.png

At this point, I should be able to access my bucket using my custom domain name. So let's test my 404 pagehttp://cdn.joel.net. Looks like it's working!

image.png

Now just to check one last thing, I open the Developer Console, click theNetwork tab, click the requestcdn.joel.net and go toHeaders.

Here I can seecontent-encoding: br. That means the HTML was compressed using the Brotli compression algorithm all automatically for me by Cloudflare.

image.png

End

Cheers 🍻

Top comments(20)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
matteojoliveau profile image
Matteo Joliveau
My name is Matteo and I'm a cloud solution architect and tech enthusiast. In my spare time, I work on open source software as much as I can. I simply enjoy writing software that is actually useful.
  • Location
    Milan, Italy
  • Work
    Software Engineer at Prima
  • Joined

I would definitely re-title this article. "How I setup my own personal CDN" sounds like you're building your own network of servers to cache and serve content, while instead you're configuring a commercially available CDN.

May I suggest something along the lines of "How I sped up my website with a CDN"? I think it would better reflect the content of your otherwise very interesting article

CollapseExpand
 
harishkotra profile image
Harish Kotra (he/him)
I do/build: Chatbots | Hackathons | WordPress | Developer Events | Web & Mobile Apps
  • Location
    Hyderabad, India
  • Work
    Platforms & Community Lead at AngelHack
  • Joined

Agree. Came here expecting a DIY CDN setup but not cloudflare and static S3.

CollapseExpand
 
joelnet profile image
JavaScript Joel
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
  • Joined

Title adjusted to add clarity as recommended 👍

CollapseExpand
 
matteojoliveau profile image
Matteo Joliveau
My name is Matteo and I'm a cloud solution architect and tech enthusiast. In my spare time, I work on open source software as much as I can. I simply enjoy writing software that is actually useful.
  • Location
    Milan, Italy
  • Work
    Software Engineer at Prima
  • Joined

Thank you very much, Joel!

Thread Thread
 
joelnet profile image
JavaScript Joel
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
  • Joined

Everyone was right. Totally agree about the title! Cheers 👍🍻

CollapseExpand
 
ishan_kanade profile image
Ishan kanade
  • Joined

The title is a bit misleading, it sounds like you setup a DIY CDN on your own without using a hosted service like Cloudflare.

CollapseExpand
 
joelnet profile image
JavaScript Joel
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
  • Joined

Title adjusted to add clarity as recommended 👍

CollapseExpand
 
musicin3d profile image
musicin3d
  • Joined
• Edited on• Edited

👎 So you DIDN'T create your own personal CDN. You used a shared CDN, one of the most popular ones at that.

CollapseExpand
 
joelnet profile image
JavaScript Joel
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
  • Joined

Title adjusted to add clarity as recommended 👍

CollapseExpand
 
onyxcode profile image
Dan
They call it "Fall" but nothing's fallen yet.......wait is that a cow?
  • Email
  • Location
    Florida, USA
  • Education
    Does myself count?
  • Work
    CEO of myself at localhost
  • Joined

Gonna have to agree with the other comments here and say that this title is a bit too much clickbait

CollapseExpand
 
joelnet profile image
JavaScript Joel
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
  • Joined

Title adjusted to add clarity as recommended 👍

CollapseExpand
 
jhg03a profile image
Jonathan G
  • Joined

Agree on the misleading title. If you are however interested in building your own CDN, I recommend investigatingtrafficcontrol.apache.org/. I use it daily to build CDN from scratch.

CollapseExpand
 
jesseflb_11 profile image
jesseflb
  • Joined

While click baits are somewhat normalized, this article title is a pretty good example of notorious click baiting and an absolute waste of time.

CollapseExpand
 
joelnet profile image
JavaScript Joel
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
  • Joined

Title adjusted to add clarity as recommended 👍

CollapseExpand
 
robcodes profile image
RobCodes
  • Joined

Just curious. Why cloudfare over aws cloudfront? With AWS Cloudfront you could assign a role, and then you would not have to make your S3 bucket private.

CollapseExpand
 
joelnet profile image
JavaScript Joel
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
  • Joined

There are many options to do this. AWS Cloudfront is also a good solution. Same as the S3 back-end. It could have been any static host.

CollapseExpand
 
robcodes profile image
RobCodes
  • Joined

What I mean is that Cloudfront is configured to pull from S3. So, what benefits did you have by using a different service provider such as Cloudfare? Was it cheaper? Easier to configure? Did you find it faster. or was this one of those "becasue I can" type of things?

Thread Thread
 
joelnet profile image
JavaScript Joel
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
  • Joined

For me, this piece with Cloudflare was easier than Cloudfront because there was no setup. All I had to do was change my DNS. If I used Cloudfront, I'd have to add that service and configure it. Cloudfront is also a good option, I just didn't need it for my specific use case.

CollapseExpand
 
shahadishraq profile image
Shahad Ishraq
  • Joined

You are basically serving from one single region. This is not geographically distributed. So a user from Europe gets the content from all the way across the Atlantic.

CollapseExpand
 
joelnet profile image
JavaScript Joel
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
  • Joined

Because I am using Cloudflare, the requests are proxied through their CDN so it is also geographically distributed!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
  • Joined

More fromJavaScript Joel

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp