Checking how many agents are working

How this works
Contact
← Blog
A steel railway switch yard model on a dark bench with one track splitting into several branches, an amber lamp on the central switch, and the AMARBARO mark on a small plate.
✎blogbericht

One server, many sites: how a web server sorts visitors

The short version

A web server sorts visitors by the name and path in the request, and a few plain rules in one file decide what every visitor sees.

What this is about

One computer can run many websites. When a visitor arrives, the computer has to know which site they want. It finds out by reading the name inside the request. A program called a web server reads that name and picks the right answer. We use one named Caddy. This article shows what its rules look like and what each one does.

Why it matters

Running several sites on one machine saves money and work. It also means a mistake in one rule can affect every site. If you understand the sorting, you can read a rule file like a sign post, and you can tell when a site shows the wrong content.

How it works

The name rides along with the request

In the last article we saw that DNS points many names at the same place. Each request then says which name the visitor typed. That field is called the Host. A web server looks at it first. If the Host says one thing, the server serves site A. If it says another, it serves site B.

Blocks in a Caddyfile

Caddy keeps its rules in a file called a Caddyfile. The file is made of blocks. Each block starts with an address and lists what to do for visitors who match it.

Here is the shape of the one in our base template for amarbaro.com. It has two blocks. The first answers for the site's own domain and does three things. It compresses replies so they travel faster. It adds a few safety headers. And it passes everything else to the app, which listens on port 8000 inside its own box. The second block answers for a separate address that a stats service used, and passes visitors to that service.

The domains come from settings, not from the file. The file says "use the domain from the setting, or localhost if there is none". That means the same file works on a laptop for tests and on the real server.

Rules that look at the path

Names are the first sort. The path is the second. In our base file there is a short block with a loud comment: any path that starts with /internal/ gets a plain 404, a not found, from the public address. That folder is for a timer on the machine itself. The comment says the block must stay, because it is one of the guards. The app also asks for a secret token, but we do not want that to be the only lock.

A busier example: aiazic.com

The setup file for aiazic.com shows more of the idea. It is one site, but it has several jobs.

  • A rule catches www.aiazic.com and redirects to aiazic.com.
  • The path /internal is hidden, as in the base file.
  • Paths that start with /api/world/ go to a small helper program next to the app, with a limit on how big a request body may be, set at 80 KB.
  • The chat socket for guests goes to another helper.
  • The front page, and files that exist in the 3D world folder, are served straight from that folder.
  • Everything else, such as the shop, the account pages and the admin, goes to the app.
  • Files ending in .md or .json, dotfiles and backup copies are never served. Those get a 404.

Notice the order. Specific rules come before the general one, and the app is last as the catch-all. That ordering is what lets a 3D world and a shop live on the same name without bumping into each other.

One more trick: caching per kind of file

A rule can also set how long browsers keep a file. In our aiazic setup, the CSS and JavaScript files that have no version in their name are told to be checked every time. Files in the bundle folder, which have a fingerprint in the name, are kept for a year. This is a big topic, so it has its own article on caching.

What to remember

Three habits will help you with any web server.

  1. Match on the name first, then the path, then fall back to a catch-all.
  2. Block what must stay private at the edge, even if the app also checks.
  3. Keep the rules in a file that is saved in version control, so you can see when and why each rule changed.

What this does not tell you

A rule file shows what the server would do. It does not prove what it does right now. A typo, a missing reload or an old copy can make the live server behave differently. The only proof is to ask the live server, from outside, for a few pages.

Words you may not know

  • Web server: a program that receives requests and decides what to send back.
  • Host: the name field in a request. It says which site the visitor wants.
  • Reverse proxy: a server that passes requests to another program behind it.
  • Redirect: a reply that tells the visitor to go to another address.
  • Path: the part of an address after the name, like /shop.
  • 404: the code for "not found".
  • Header: a small note attached to a request or a reply.

Try it

  1. Open amarbaro.com/web and pick two of our sites. They share a family of machines, but each shows its own face.
  2. Try a path that should not exist, like /internal/ on any of our sites. You should get a not found page.
  3. In a terminal, run curl -I https://aiazic.com and look at the headers. The redirect from www shows up with curl -I https://www.aiazic.com.
  4. If you want your own: install Caddy, write a two line Caddyfile with a name and a respond "hello" line, and run it.

Reacties

Nog geen reacties.

Inloggen om te reageren.