bazariara.ge is now served from Tbilisi: an nginx cache in front of Next.js on our own server in Georgia
In October we moved Bazari Ara, an online store of Georgian food, gifts and outdoor gear in Tbilisi, from Vercel to RelaxDev. We told the story in “A year on Vercel and one evening of moving”. The app and the database live on a server in Moscow.
Almost all of the store's customers are in Georgia, and every request they made travelled to Moscow and back. We had a small server in Tbilisi, so we put nginx on it as a caching node in front of the site. The app and the database stayed on RelaxDev; pages, scripts and images now come to visitors from a disk in Tbilisi.
- ⚡ from Tbilisi the page arrives in 15–64 ms on Magticom and Cloud 9, down from 0.42 s in October; first byte in 3–10 ms instead of 120–140;
- 📈 PageSpeed on mobile is still 100 out of 100, LCP 1.0 s instead of 1.3, Speed Index 1.7 s instead of 2.4;
- 🇷🇺 Russia got slower: 0.24–0.53 s instead of 0.12–0.20 — details below;
- 🧩 the site doesn't know about the Tbilisi server: the whole setup is one nginx config.
How it works
Customer
│
▼
nginx in Tbilisi ── page in cache? ── yes ──▶ served from disk
│ no
▼
RelaxDev in Moscow: Next.js + PostgreSQLThe domain now points to the Tbilisi server. nginx talks to the app over HTTPS with the same name, bazariara.ge, in the Host header and in SNI, so the app sees no difference. nginx keeps up to 32 connections to Moscow open, so a cache miss doesn't pay for a new TLS handshake.
What is cached and for how long
| What | How long | Why |
|---|---|---|
📦 Next scripts and styles (/_next/static) | 1 year | The file name contains a hash: a new version gets a new name |
🖼 Images from the Next optimizer (/_next/image) | 30 days | The Accept header is part of the cache key: webp and jpeg are stored separately |
📄 Pages /ru, /ka, /en | 2 minutes | After that a visitor instantly gets the stored copy while a fresh one is fetched in the background |
🛒 /api, admin, orders, chat | not cached | Always go to the app |
Prices and stock on the site update within a couple of minutes: the first visitor after a copy expires still sees the old one and triggers a background refresh, everyone after that sees the new one.
If a hundred people open a page that isn't cached at the same moment, one request goes to Moscow. If the main server is down, visitors see the last stored copies of the pages. Ordering won't work in that case — /api always goes to the app.
Why not Redis
Redis was the first idea. But for pages and images the nginx disk cache is simpler and more reliable: every response is a file that survives a restart, and nginx serves files from disk itself, without an extra service. Only the key index lives in memory, in a 64 MB zone. Up to 8 GB on disk; anything unused for 30 days is removed.
Pitfalls
A language cookie on every page. Next sets a language cookie in every response. A response with Set-Cookie must not be cached, or one visitor's cookie would go to everyone. nginx hides the app's cookie and sets the same one itself, based on the language in the URL. Nothing changes for the visitor.
One URL, two different responses. The Next App Router serves either HTML or an RSC stream for in-site navigation from the same URL, depending on the request headers. Cache them under one key and someone gets raw data instead of a page. The RSC and Next-Router-Prefetch headers are part of the cache key, and in-site navigation bypasses the cache.
The redirect from / differs per visitor. Bare bazariara.ge leads to /ru, /en or /ka depending on the cookie and the browser language. That response isn't cached, nor is any URL without a language. We also changed the redirect from 308 to 307: browsers remember a permanent redirect and would keep sending people to the old language after they switched.
502 after the switch. We put the first version on live traffic before testing it, and for a few minutes the site answered 502. We rolled back with a script and found the cause: nginx verifies the app's certificate, and the default chain verification depth is 1. The Let's Encrypt chain is longer, so it needs proxy_ssl_verify_depth 4. Now a new config first comes up on port 8443, pages are checked against it, and only then does it move to 443.
Two certificates for one domain. The Tbilisi server has its own Let's Encrypt certificate. nginx forwards the challenges for the RelaxDev certificate of the same domain to Moscow, so the main server keeps renewing its certificate too: if we ever need to roll back, it will be valid.
The server wasn't empty. Other services were already running on the machine. Before installing nginx we checked what was running and which ports were taken. nginx uses only 80 and 443.
Supplier images. The store builds its New Year section from a supplier's website, and that site doesn't open from Russian servers — neither directly nor through a proxy. A script running on the Tbilisi server moved the product photos: it downloaded them and uploaded them to the store's storage through the admin panel.
Numbers
Globalping: Tbilisi
The same three providers as in October, three rounds in a row, the full /ru page (GET).
| Provider | First byte | Full response ¹ | Cache status |
|---|---|---|---|
| Cloud 9 | 3–4 ms | 15–52 ms | HIT, STALE |
| Magticom | 9–10 ms | 28–64 ms | HIT, UPDATING |
| TradeZone | 110–126 ms | 233–357 ms | HIT, UPDATING |
| October: directly to Moscow | 120–140 ms | 420 ms | — |
¹ Including the DNS lookup: 37–119 ms in the first round, then the address was cached — 1–2 ms.
For TradeZone even the TCP connection to a server in the same city takes 31–37 ms, against 0–3 ms for the other two providers: the route from this provider to the data center is longer. Still, the page reaches it faster than in October.
By the third round the stored copy had just expired. nginx served it right away (STALE, UPDATING) and refreshed it in the background — visitors didn't notice: 15–29 ms on Cloud 9 and Magticom.
From the Tbilisi server itself
curl five times in a row, a new connection each time. Times are counted from the start of the request.
| Where to | Connect | First byte | Full response |
|---|---|---|---|
| Directly to Moscow, as before the cache | 77–83 ms | 293–308 ms | 427–446 ms |
| Local nginx, served from cache | 0–7 ms | 116–123 ms | 119–125 ms |
About 110 ms in both rows is curl's own TLS overhead on this server: for Globalping visitors the handshake with the same nginx takes 9–14 ms. So what matters here is the difference: the round trip to Moscow cost every page about 0.3 s. A request that found no copy in the cache (MISS) took 307 ms.
PageSpeed Insights, mobile
Lighthouse, Moto G Power profile, slow 4G.
| Metric | October: directly to Moscow | November: via Tbilisi |
|---|---|---|
| Performance | 100 | 100 |
| Accessibility | 100 | 100 |
| Best practices | 100 | 100 |
| SEO | 100 | 100 |
| First Contentful Paint | 1.0 s | 1.0 s |
| Largest Contentful Paint | 1.3 s | 1.0 s |
| Total Blocking Time | 30 ms | 30 ms |
| Cumulative Layout Shift | 0 | 0 |
| Speed Index | 2.4 s | 1.7 s |
To be fair about this test: Google's test server isn't in Georgia, and the home page itself changed over the month. So the difference isn't all down to the cache. The cache's share is that the page is already rendered and the app doesn't have to build it. In October the test ran on /ru, in November PageSpeed landed on /en — both use the same template.
Visitors from Russia
There is one domain, so visitors from Russia also go through Tbilisi. Globalping, the same probes as in October, three rounds:
| Provider | First byte | Full response ¹ |
|---|---|---|
| Timeweb, Moscow | 78–81 ms | 242–324 ms |
| Yandex.Cloud, Moscow | 76–81 ms | 294–354 ms |
| Selectel, Saint Petersburg | 67–134 ms | 276–444 ms |
| Hosting technology, Moscow ² | 151 ms | 529 ms |
| October: directly to Moscow | 20–30 ms | 120–200 ms |
² Showed up in one round out of three.
Every response came from the Tbilisi cache, but the route from Russia to Georgia costs 65–82 ms just to connect. For Russia the page got 0.1–0.3 s slower and still arrives in under half a second. If Russian traffic grows noticeably, visitors can be split with GeoDNS: Russia straight to Moscow, Georgia to Tbilisi.
Also done along the way
- 🔒 The site can no longer be shown in an iframe on other domains:
X-Frame-Options: SAMEORIGINandframe-ancestors 'self'. HSTS,nosniff,Referrer-PolicyandPermissions-Policywere already in place. - ↪️ Redirects from URLs without a language are now 307 instead of 308.
- 🗺 The sitemap has 1,356 URLs instead of 928: the store got a new New Year section.
Summary
- ✅ From Tbilisi the page arrives in 15–64 ms on two providers out of three, first byte in 3–10 ms.
- ✅ Still one app and one database, on RelaxDev. No site code was changed for the cache.
- ✅ PageSpeed on mobile: 100 out of 100, LCP 1.0 s, Speed Index 1.7 s.
- ✅ If the main server is down, the site keeps showing stored pages.
- ✅ A new config is tested on a separate port first; rollback is one command.
- ⚖️ The cost is Russia: 0.24–0.53 s instead of 0.12–0.20. If needed, traffic can be split by country with GeoDNS.
This is a translation of the original Russian article. Comments are on the Russian page.