arifaydin.devtürkçe

back to blog

serving a static site with nginx behind cloudflare: five easy-to-miss details

Five small details that kept an Astro site from loading after moving it to an nginx server behind Cloudflare, and the fix for each.

Publishing a static site is simple on paper: copy dist to the server, point nginx at it, done. Add Cloudflare, a server that already hosts other sites, and SELinux, and there are plenty of ways to end up with “the files are there, but the site won’t load.”

This is the checklist from moving my portfolio to a new server. Every item is small, and every one of them can eat an afternoon.

1. first, check whether the request reaches the server

When a site won’t load, the reflex is to start editing the nginx config. The question to ask first is: does the request even reach the server?

Cloudflare exposes a diagnostic endpoint on every domain:

curl http://example.com/cdn-cgi/trace

If that answers, the request reached Cloudflare. Now check the access log on the server:

curl -s http://example.com/probe-12345 > /dev/null
grep probe-12345 /var/log/nginx/access.log

If Cloudflare answers but the log has no line, the problem isn’t on the server, it’s on the Cloudflare side: the DNS record points to the wrong IP, or the domain is bound to something else (a Worker, a Pages project, an old server). In my case the DNS record pointed somewhere else; no amount of nginx tweaking would have found it.

To test the server while bypassing Cloudflare, use --resolve:

curl -k --resolve example.com:443:SERVER_IP https://example.com/

2. an unknown host falls through to the default server

When nginx can’t match a request to any server_name, it hands it to the port’s default server. Unless you wrote default_server, that’s the first server block it read.

That has two consequences:

  • If your site has no config, visitors land on some other site on the same server. “Why is my domain redirecting to a different domain?” is usually this.
  • If your new file sorts alphabetically before the others, you become the default server. conf.d/*.conf files are read in alphabetical order; a-site.conf comes before b-site.conf.

I hit the second one: once the new file was added, every request arriving by bare IP started redirecting to my site. The fix was to name the file so it sorts last, or to set the default explicitly with default_server. It’s easy to check:

curl -sI -H "Host: nothing.example" http://127.0.0.1/ | grep -i location

3. SELinux makes files “unreadable”

On RHEL, AlmaLinux and Rocky Linux, SELinux is on by default (getenforce → Enforcing). nginx can only read files with the right label. A new directory like /var/www/site may be labelled var_t, and nginx returns 403, even with 644 permissions.

The lasting fix is to define a rule for the directory and apply the labels:

semanage fcontext -a -t httpd_sys_content_t "/var/www/site(/.*)?"
restorecon -R /var/www/site
ls -Z /var/www/site/index.html   # you should see httpd_sys_content_t

A label set with chcon is lost on the next restorecon. A rule defined with semanage persists.

4. getting a certificate from behind cloudflare

With the Cloudflare proxy on (orange cloud), Let’s Encrypt’s HTTP validation also comes through Cloudflare. That usually works fine, as long as the challenge path is open on both ports:

server {
    listen 80;
    server_name example.com;
    location /.well-known/acme-challenge/ { root /var/www/html; }
    location / { return 301 https://example.com$request_uri; }
}

Add the same location to the 443 block too: with “Always Use HTTPS” on in Cloudflare, the challenge arrives over HTTPS.

certbot certonly --webroot -w /var/www/html -d example.com

A certificate issued with webroot doesn’t reload nginx when it renews. Add a hook to the renewal config:

# /etc/letsencrypt/renewal/example.com.conf  ->  under [renewalparams]
renew_hook = systemctl reload nginx

Without it, the certificate is renewed on disk while nginx keeps serving the old one for three months, and the site goes down when it expires.

One more note: until the certificate arrives, a temporary self-signed certificate will do. Cloudflare’s “Full” mode doesn’t validate the origin certificate; “Full (strict)” does. The lasting answer is always a real certificate.

5. switch releases in one move

Copying dist straight over the live directory means serving a half-copied site while the copy runs. A safer layout puts each release in its own directory and points the live path at it with a symlink:

/var/www/site/
  releases/20261120-092831/
  releases/20261120-094951/
  current -> releases/20261120-094951

Swap the link in one atomic step:

ln -sfn /var/www/site/releases/$TS /var/www/site/current.new
mv -T /var/www/site/current.new /var/www/site/current

In nginx you write root /var/www/site/current;. Rolling back is just pointing the link at the previous directory.

short checklist

  • Does cdn-cgi/trace answer, and does the request show up in the access log?
  • Does the site have its own server_name block, and did the default server change?
  • Is the SELinux label httpd_sys_content_t?
  • Is the challenge path open on 80 and 443, and is renew_hook set?
  • Do releases switch through a symlink?

With all five ticked, what’s left is working on the site itself.