[server]
The [server] block of nvs.toml sets where nvs serve listens, how many requests it runs and how long it waits.
Every key is optional. With no block, the server listens on 127.0.0.1:8000 and uses one worker for each CPU core. listen is a list. An entry is an address with a port, or the path of a Unix socket. max_in_flight is the number of requests that may run at the same time. A request over that number gets the response 503. So does a request when the server's memory has no room for it.
Four keys are timeouts for a connection that sends nothing or reads nothing. A slow upload that still sends data is not closed. drain_timeout is the longest time that a connection with no request stays open after a stop. You cannot turn a timeout off: false and 0 are errors.
Good to know: a running server applies a change to this block without a restart. listen, socket_mode and workers are the exception and need a restart.
The example below reads two keys that the file sets and one key that it does not set.
Examples
The examples below run with this configuration file.
# The server of a small shop. Every key in `[server]` is optional.[server]listen = ["127.0.0.1:8080"]health_path = "/up"max_in_flight = 500keepalive_timeout = "60s"The settings of this server
<?nvs// Shows how a program reads the `[server]` block of the `nvs.toml` beside this file.
// `Core\Config::get` returns the value of a setting as a string.// This prints `/up`, the path that a load balancer checks.echo 'health check path: ', Core\Config::get('server.health_path') ?? 'off', "\n";
// This prints `500`. A request over this number gets the response `503`.echo 'requests at one time: ', Core\Config::get('server.max_in_flight') ?? '(no value)', "\n";
// The file does not set `drain_timeout`, so the result is the default, `30s`.echo 'drain timeout: ', Core\Config::get('server.drain_timeout') ?? '(no value)', "\n";
// A program cannot change a setting in this block. A name written directly in the// call does not compile. This name is in a variable, so the call runs and returns `false`.string $name = 'server.max_in_flight';echo 'changed by the program: ', Core\Config::set($name, '100000') ? 'yes' : 'no', "\n";health check path: /uprequests at one time: 500drain timeout: 30schanged by the program: noserver
Settings for the HTTP server: where it listens, how it finds the code for a request, and how long it waits.
The block sets the addresses or Unix sockets to listen on and the directory that every mount must be in. It also sets which proxies are trusted, how many requests run at once, the number of workers and the timeouts. When nothing is set, the server listens on 127.0.0.1, port 8000.
When [mode] default is development, the server runs the .nvs file at the path in the URL and serves static files. In production mode, every request runs the entry file of the mount, and no static file is served. Your own dispatch or static value replaces these defaults.
Good to know: the server applies a change to this block while it runs. listen, socket_mode and workers need a restart. A program cannot change any setting in this block.
The example prints the address, the header timeout and the trusted proxies. It then tries to change them, and Core\Config::set returns false each time.
Only nvs.toml sets this key. A request may not change it.
A change in the file takes effect a few seconds after you save it. The server does not restart.
The shape of the front door
<?nvs// Shows how to read the `[server]` block: where the server listens, which proxies// it trusts, and how long it waits for a request's headers.//// [server]// listen = ["127.0.0.1:8000"] # or an absolute path: a Unix socket// root = "/www"// dispatch = "entry"// trusted_proxies = [] # trust no forwarding header// header_timeout = "10s"// keepalive_timeout = "75s"
// This file does not write `[server]`. `listen` is a list, so `get` returns `null`.// `header_timeout` is the default, `10s`.echo 'listening on: ', Core\Config::get('server.listen') ?? '(a list: loopback on port 8000)', "\n";echo 'header wait: ', Core\Config::get('server.header_timeout') ?? '(no value)', "\n";echo 'believed proxy: ', Core\Config::get('server.trusted_proxies') ?? '(nothing written — no forwarding header is believed)', "\n";
// A program cannot change any setting in this block.
// `Core\Config::set('server.listen', ...)` does not compile,// because a program may not change `server.listen`.
// This program keeps the names in variables. The compiler does not check a variable, so each `set` call runs and returns `false`.// Each call prints `not allowed`.string $listen = 'server.listen';echo 'listen somewhere else: ', Core\Config::set($listen, '0.0.0.0:80') ? 'accepted' : 'not allowed', "\n";
// A trusted proxy decides which client address the logs and rate limits use.// With an empty list, the server trusts no forwarding header.string $trustedProxies = 'server.trusted_proxies';echo 'believe my proxy: ', Core\Config::set($trustedProxies, '0.0.0.0/0') ? 'accepted' : 'not allowed', "\n";
// The header timeout closes a connection that sends its headers too slowly.string $headerTimeout = 'server.header_timeout';echo 'wait all day for me: ', Core\Config::set($headerTimeout, '86400s') ? 'accepted' : 'not allowed', "\n";listening on: (a list: loopback on port 8000)header wait: 10sbelieved proxy: (nothing written — no forwarding header is believed)listen somewhere else: not allowedbelieve my proxy: not allowedwait all day for me: not allowedserver.listen
The addresses where the server accepts connections.
Each entry is an address and a port, such as 127.0.0.1:8000, or the full path of a Unix socket. When the file does not set it, the server listens on 127.0.0.1, port 8000. Unix sockets work only on Unix systems, such as Linux and macOS. On Windows, the server listens on TCP.
A program cannot change this setting. The server reads it only when it starts. If you change it in the file, the server keeps its old addresses until you restart it. The server writes the new value to its log once, in a configuration restart pending line.
Good to know: a port below 1024 needs special permissions. The server gives them up after it starts, so it cannot move to such a port while it runs.
Only nvs.toml sets this key. A request may not change it.
A change in the file takes effect only when the server starts again.
Where the server accepts connections
<?nvs// Shows how to read `[server] listen`, the addresses where the server accepts connections.//// [server]// listen = ["127.0.0.1:8000"] # an address and a port, or the path of a Unix socket
// When the file does not set it, the server listens on 127.0.0.1, port 8000.echo 'listening on: ', Core\Config::get('server.listen') ?? '127.0.0.1:8000 (the default)', "\n";
// `Core\Config::set('server.listen', ...)` does not compile,// because a program may not change `server.listen`.
// This program keeps the name in a variable. The compiler does not check a variable, so `set` runs and returns `false`.// This prints `not allowed`.string $name = 'server.listen';echo 'listen on every address: ', Core\Config::set($name, '0.0.0.0:80') ? 'accepted' : 'not allowed', "\n";
// A new value in the file takes effect after a restart. Until then, the server writes it to its log.listening on: 127.0.0.1:8000 (the default)listen on every address: not allowedserver.socket_mode
The file permissions of the Unix sockets the server listens on.
A Unix socket is a file, and every account that may write to it may connect to the server. The value is written as an octal number, the same way chmod writes permissions. When the file does not set it, the value is 0660: the account the server runs as and the socket's group may connect. To let another account connect, add it to that group.
This setting has no effect on TCP addresses. The server uses it when it creates a socket, so a new value takes effect after a restart. A program cannot change it.
Only nvs.toml sets this key. A request may not change it.
A change in the file takes effect only when the server starts again.
Who may connect to a unix socket
<?nvs// Shows how to read `[server] socket_mode`, the file permissions of the server's Unix sockets.//// [server]// listen = ["/run/nvs/web.sock"]// socket_mode = "0660" # the owner and the socket's group may connect
// When the file does not set it, the value is `0660`.echo 'socket mode: ', Core\Config::get('server.socket_mode') ?? '(no value)', "\n";
// Every account that may write to the socket may connect to the server. A program cannot change this setting.
// `Core\Config::set('server.socket_mode', ...)` does not compile,// because a program may not change `server.socket_mode`.
// This program keeps the name in a variable. The compiler does not check a variable, so `set` runs and returns `false`.// This prints `not allowed`.string $name = 'server.socket_mode';echo 'let everyone connect: ', Core\Config::set($name, '0666') ? 'accepted' : 'not allowed', "\n";
// The server uses this value when it creates a socket. A new value takes effect after a restart.socket mode: 0660let everyone connect: not allowedserver.workers
How many workers the server runs to accept and serve requests. Each worker runs on its own CPU core.
When the file does not set it, the server starts one worker for each CPU core the machine gives it. Set a lower number to leave cores free for other programs on the same machine.
The server creates its workers when it starts, so a new value takes effect only after a restart. Until then, the server writes the new value to its log once, in a configuration restart pending line. A program cannot change this setting.
Only nvs.toml sets this key. A request may not change it.
A change in the file takes effect only when the server starts again.
How many cores serve requests
<?nvs// Shows how to read `[server] workers`, the number of workers that accept and serve requests.//// [server]// workers = 4 # when not set: one worker for each CPU core
// This file does not set it, so there is one worker for each CPU core. That number// differs on each machine, so this compares it with `Core\Os::cpuCount` and prints `yes`.echo 'one per core: ', (Core\Config::get('server.workers') as ?int) == Core\Os::cpuCount() ? 'yes' : 'no', "\n";
// `Core\Config::set('server.workers', ...)` does not compile,// because a program may not change `server.workers`.
// This program keeps the name in a variable. The compiler does not check a variable, so `set` runs and returns `false`.// This prints `not allowed`.string $name = 'server.workers';echo 'use 64 workers: ', Core\Config::set($name, '64') ? 'accepted' : 'not allowed', "\n";
// The server creates its workers when it starts. A new value takes effect after a restart.one per core: yesuse 64 workers: not allowedserver.watchdog_margin
How long a worker may stay past the deadline of its oldest request before the server reports that the worker has stopped. A worker is the thread that accepts and runs requests on one CPU core.
A request that runs too long is stopped by its own time limit. This setting is for a worker that does not run at all any more, for example because of a bug in the engine. The server then writes one line to its error output. The line names the CPU of that worker.
When the file does not set it, the margin is 5 seconds. The value is a duration, such as "5s" or "500ms". A bare number is seconds. The values 0 and false are not allowed, and the server does not start with either of them.
The server reads this setting when it starts, so a new value takes effect only after a restart. Until then, the server writes the new value to its log once, in a configuration restart pending line. A program cannot change this setting.
Only nvs.toml sets this key. A request may not change it.
A change in the file takes effect only when the server starts again.
How long a stopped worker goes unreported
<?nvs// Shows how to read `[server] watchdog_margin`, the time before the server reports a stopped worker.//// [server]// watchdog_margin = "5s" # when not set: 5 seconds
// This file does not set it, so this prints the default, `5s`.echo 'watchdog margin: ', Core\Config::get('server.watchdog_margin') ?? '(no value)', "\n";
// `Core\Config::set('server.watchdog_margin', ...)` does not compile,// because a program may not change `server.watchdog_margin`.
// This program keeps the name in a variable. The compiler does not check a variable, so `set` runs and returns `false`.// This prints `not allowed`.string $name = 'server.watchdog_margin';echo 'use 1 second: ', Core\Config::set($name, '1s') ? 'accepted' : 'not allowed', "\n";
// The server reads this setting when it starts. A new value takes effect after a restart.watchdog margin: 5suse 1 second: not allowed