You spin up a VPS, configure SSH, and everything works beautifully — until you try to reach it from a network that blocks port 22 and anything that looks like a raw tunnel. Suddenly your connection dies, your client times out, and you’re staring at a blank terminal at 1 AM. That’s usually the moment people start searching for an SSH CloudFront CDN setup tutorial, because fronting your tunnel with a CDN edge is one of the few tricks that still works when the network decides to be difficult.
What SSH CloudFront Setup Really Means
Let’s clear up a common misconception first. Amazon CloudFront does not proxy raw TCP or raw SSH traffic. It speaks HTTP and HTTPS, plus WebSocket. So “SSH over CloudFront” actually means: you wrap your SSH session inside a WebSocket connection, then let CloudFront terminate TLS at the edge and forward those WebSocket frames to your origin server.
The upside is real. You get TLS on port 443, a globally distributed edge network, and traffic that looks like ordinary HTTPS to any firewall in between. Your SSH daemon never touches the public internet directly.
Is it more work than a plain ssh user@host? Yes. Worth it? In my experience, absolutely — especially for servers you need to reach from locked-down corporate or campus networks.
What You Need Before You Start
- An Ubuntu 22.04 or 24.04 server (a $5 instance is plenty — no need for the biggest box from the best hosting you can find)
- A domain you control. A cheap domain works fine here; CloudFront only needs a valid ACM certificate for it
- An AWS account with CloudFront and ACM access
- A WebSocket-to-TCP bridge on the origin. I use
wstunnel, thoughwebsocatworks too - A client that supports WS tunnels — HTTP Injector, NekoBox, or v2rayNG all handle this
One thing people forget: you also need a certificate in us-east-1. CloudFront only accepts ACM certs from that specific region, no matter where your server lives. This trips up almost everyone the first time.
Step 1 — Get the Origin Talking WebSocket
On your server, keep OpenSSH listening on 127.0.0.1:22 only. Then run the bridge on a local port that CloudFront can reach over plain HTTP.
wstunnel server --restrict-to 127.0.0.1:22 ws://0.0.0.0:8080Now put Nginx in front so the bridge is reachable on port 80 with a normal path, like /tunnel:
location /tunnel {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 3600s;
}
Learn more about Top 5 Free SSH Servers with.Those two Upgrade headers are non-negotiable. Miss them and CloudFront will happily return 200 OK while your WebSocket silently fails to upgrade. Fun debugging session, trust me.
Step 2 — Create the CloudFront Distribution
- Open the CloudFront console and create a distribution with your server as a custom origin.
- Set the origin protocol to HTTP only and the port to
80. CloudFront handles the TLS layer for you. - Add your domain as an alternate CNAME and attach the ACM certificate from
us-east-1. - Set the viewer protocol policy to Redirect HTTP to HTTPS.
- Attach the managed CachingDisabled cache policy — tunnel traffic must never be cached.
- Attach the managed AllViewer origin request policy so headers pass through untouched.
- Allow
GET,HEAD,OPTIONS,PUT,POST,PATCH,DELETE.
WebSocket support is automatic once the distribution is HTTP/HTTPS-based. No toggle to flip.
Shortcut warning: skip the default cache policy. If you leave caching on and your tunnel path gets cached, you’ll get stale 200 responses instead of a live connection — and the error message tells you nothing useful.
Step 3 — Point Your Client at the Edge
On your laptop, the local side of the tunnel needs a matching client. With wstunnel it looks like this:
wstunnel client -L 2222:127.0.0.1:22 wss://ssh.yourdomain.com/tunnelThen connect normally:
ssh -p 2222 [email protected]If you prefer a GUI-style client, NekoBox or HTTP Injector can point at the same wss:// endpoint with the path set to /tunnel. Same result, less typing.
Test the edge before you test SSH. A quick curl -I https://ssh.yourdomain.com/tunnel should return 101 Switching Protocols or a 400 from the bridge — either means CloudFront is reaching your origin correctly. A 502 or 504 means the origin or the security group is the problem, not the client.
Step 4 — Lock It Down
Once it’s working, tighten things up. Restrict origin access with a CloudFront custom header and reject anything at Nginx that doesn’t carry it. Enable AWS WAF rate limiting on the distribution — you’d be surprised how fast a public /tunnel path attracts scanners.
Also set ClientAliveInterval 60 and ClientAliveCountMax 3 in sshd_config. CloudFront idle times out around 60 seconds on quiet connections, and keepalives keep the socket warm. For more on this, see SSH WebSocket Tutorial for Beginners.
Practical Tips From Real Deployments
- Use one path per service.
/tunnelfor SSH,/vpnfor anything else. Cleaner logs, easier WAF rules. - Keep the origin private. Security group allows port 80 from CloudFront IP ranges only, SSH from your own IP.
- Watch the data transfer bill. CloudFront egress is cheaper than most VPS egress, but heavy SSH sessions still add up. Check the pricing page before running a 40 GB sync.
- Don’t stack too many layers. A premium VPN on top of a WS tunnel over a CDN is fine, but every extra hop adds latency and another failure point.
Sensible defaults beat clever configs. Every time.
FAQ
Can I use plain SSH through CloudFront without WebSocket?
No. CloudFront only handles HTTP, HTTPS, and WebSocket. Raw TCP on port 22 won’t pass through.
Does CloudFront cache my tunnel traffic?
Only if you let it. Attach the CachingDisabled policy and it won’t. This is the single most common misconfiguration I see.
Is this useful without AWS?
The concept works anywhere — Cloudflare, Fastly, or any CDN with WebSocket support. CloudFront just happens to have generous free-tier limits and solid edge coverage.
Will this work from a phone?
Yes, with the right client. HTTP Injector and NekoBox both handle WSS tunnels without root. Pair it with decent cloud storage for your config backups and you’re set.
Wrapping It Up
The whole trick boils down to three moving parts: a WebSocket bridge on the origin, a CloudFront distribution with caching disabled, and a client that speaks WSS. Get those aligned and port 22 becomes irrelevant.
Setting this up the first time takes maybe 30 minutes. The second time, ten.
Want a ready-made environment to test against instead of building from scratch? Grab a free account at claudessh.com and start tunneling.
If this server is full, RouterSSH Hysteria2 is another option with 6,500+ servers across 196 countries.
