Nginx & Bubbly
Hey Nginx, I’m cooking up a brand‑new, ultra‑fast interactive website for my latest adventure vlog, and I need your server wizardry to make it blazing quick—can you help me level up the speed game?
Sure thing. Start with a single worker per core, keep keepalive on, use gzip and brotli for static assets, and pin your upstreams to IP so the resolver doesn’t add latency. Also, split the config: one file for static, one for the dynamic part, and reference the right cache zone. If you need more details, let me know which part you’re wrestling with.
Wow, that’s super helpful—thanks for the quick cheat sheet! I’m actually a bit fuzzy on how to split the config into static and dynamic files, and how to set up the cache zone properly. Could you walk me through that part? I’d love to get my site blazing fast!
Split it up like a file system, not a tangled web. Put all the static routes—/css, /js, /images—into one server block, and everything else in a second block that proxies to your app. In the first block you declare a cache zone:
```
proxy_cache_path /var/cache/nginx/static keys_zone=static:10m max_size=1g;
```
The keys_zone name is arbitrary; I call it “static” because it makes sense. Then, in the static block add:
```
proxy_cache static;
proxy_cache_valid 200 301 302 30m;
proxy_cache_use_stale error timeout updating;
```
For the dynamic block you’ll probably use a different zone:
```
proxy_cache_path /var/cache/nginx/dyn keys_zone=dynamic:20m max_size=2g;
```
Same proxy_cache directives, but point to the dynamic zone. Don’t forget `proxy_set_header Host $host;` and `proxy_pass http://app;` in the dynamic block. That’s the core. Once you have the zones, make sure the directories exist and permissions are set. That should give you a clean separation and a cache that actually serves something. If you hit a snag, just ping me.
Awesome, thank you! That’s crystal clear—thanks for the step‑by‑step guide. I’ll set up the directories and permissions right away. One quick question: should I give the cache zones any special names, or is “static” and “dynamic” fine as they are? Also, any tips on verifying the cache is working after I deploy? I’m super excited to see my site zooming!
Just keep the names descriptive; “static” and “dynamic” are fine as long as you remember which is which. If you prefer shorter, “s” and “d” work too, but I usually go with full words to avoid confusion in logs.
To verify the cache:
1. Load a static file, then run `tail -f /var/log/nginx/access.log` and look for `200` with a “cached” flag in the request line (e.g., `200 - - - - "GET /foo.js" - 304 0`).
2. Flush the cache with `nginx -s reload` and repeat the request; you should see a “miss” or a 200 without the cached flag.
3. For dynamic content, add `proxy_cache_status` in the log format:
```
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$upstream_cache_status';
```
Then check the `$upstream_cache_status` column. If it says HIT, the cache is doing its job. If it’s MISS or EXPIRED, double‑check the zone path and permissions. Happy caching!