Why nginx -t needs isolation: testing configuration without changing the host

An Nginx configuration test can open and create files. A look at separate namespaces, private tmpfs mounts and testing the exact installed configuration.

Two configurations are tested inside a transparent chamber beside a server.
Two configurations are tested inside a transparent chamber beside a server.

nginx -t is often treated as reading a configuration: if the syntax is valid, the service can start. Automating the write exposed a detail that matters: the test also performs part of Nginx initialization. Running it as unrestricted root can therefore leave files on the host.

We examined this on a separate Debian 13 VM. A lower CLI mechanism wrote the main configuration, and the real Nginx binary tested it in an isolated child process. This mechanism is now available through public CLI commands; the manual Nginx check on E‑01 was a separate exercise already described.

What test mode does

The Nginx documentation states that -t checks syntax and attempts to open files referenced by the configuration. In the 1.26.3 source, the test path also reaches PID-file handling and creation of required directories.

A successful exit therefore answers a question about configuration in that process's environment. It does not guarantee the absence of filesystem side effects or establish that a real worker is running, can read its public root or returns the intended HTTP responses.

Read the host, isolate temporary writes

Instead of running an ordinary root process, we gave the child test separate mount and network namespaces. unshare allows these parts of a process's environment to be separated. Our helper supplies the additional restrictions.

Environment componentBehavior during the test
Inherited filesystem mountsRecursively read-only in the test namespace
/run and /varPrivate tmpfs mounts for PID files, logs and temporary directories
NetworkA separate namespace with its own loopback
Process capabilitiesDropped; no-new-privileges enabled

These actions do not make the entire server's filesystem read-only. They change the child process's view. Its private tmpfs writes do not reach the host's ordinary directories. The separate network keeps test sockets apart from the host namespace, so this test does not check availability of the actual host port.

The helper first checks that isolation can be established. If a required kernel facility is unavailable, the operation stops before the managed configuration write. There is no unrestricted fallback. This is a specific implementation for Debian 13 on x86_64, rather than a universal wrapper for arbitrary untrusted code.

Test the exact installed file

The separate /etc/nginx/deploy-cli-origin.conf file is created only at an unoccupied destination, owned by root:root with mode 0644. Its contents come from the approved plan. Symlink substitution and overwriting an existing configuration are refused.

The test authenticates the bytes of the Nginx binary and configuration. Both files are reopened through read-only mounts in the new mount namespace, with inode and metadata comparisons. The original descriptors are closed so references to the initial writable mounts do not survive into the child execution.

This is the helper's exact final excerpt. The executable and config variables already refer to authenticated file descriptors; isolation and privilege restrictions have been established earlier. It is a code excerpt, not a standalone terminal command.

os.chdir('/run/nginx-syntax')
os.execve(executable, ['nginx', '-e', 'stderr', '-t', '-q', '-p', '/run/nginx-syntax/', '-c', config],
          {'PATH': '/usr/bin:/bin', 'LANG': 'C', 'LC_ALL': 'C'})

-c selects the exact configuration through its descriptor, -p sets the private working prefix and -e stderr directs errors to the process stream. -q suppresses normal test messages. The absence of a reassuring success line is therefore expected: the parent checks the exit status and bounded response.

The journal completes the write and syntax check together

The journal is already pending before configuration creation. After the file write and dependency checks, it enters the syntax step. Only a successful isolated test and another state read allow it to record committed.

This was the shortened actual lower API response in the VM; the result fingerprint and other fields are omitted:

{
  "status": "passed",
  "write_performed": true,
  "result": {
    "status": "committed",
    "replayed": false,
    "write_performed": true
  }
}

A separate SSH connection read the installed file and completed journal. Its response, also shortened:

{
  "status": "passed",
  "write_performed": false,
  "result": {
    "status": "verified",
    "write_performed": false
  }
}

Comparing state before and after showed that ordinary host PID, log and temporary paths were unchanged. After a normal reboot, the configuration and journal remained, and the read did not execute the syntax test again. Nginx remained masked and inactive: the systemd drop-in and service start require a separate transition.

Calling it through the CLI

The planning command reads current state through the existing access and dependency checks. The following is an adapted example: prepare the profile and private files for your own learning server. The dependency document contains the exact original layout, firewall, guard and package plans, along with the disabled-startup proof.

deploy-cli host nginx configuration-plan --profile demo \
  --request-file /private/administrator.json \
  --nginx-file /private/nginx.json \
  --dependencies-file /private/nginx-dependencies.json \
  --plan-file /private/configuration.json

configuration-apply uses the same files, the exact plan SHA and explicit approve-privileges flag. After writing, the CLI opens a new SSH connection for verification. The separate configuration-verify reads the result without write approval; it was also tested after a normal VM reboot. The old console evidence allowed completed state to be read, but did not authorize applying again after reboot.

A failure must not hide a partial write

If the configuration already exists but the test does not succeed, there is no automatic deletion or rewrite. The journal remains pending. Temporary-file tests separately reproduced a syntax-step failure and a lost response after file creation; another attempt did not conceal the unfinished state.

For investigation, retain the plan and journal and inspect actual files through independent access. This keeps the distinctions between a written file, tested syntax and a running service visible. Isolation makes the test controlled, but a full start still needs separate process, socket and HTTP-response checks.

  1. Operations & Security ACLs on Debian: allow public files and protect the private tree
  2. Architecture Debian service users: a separate identity for each application
  3. Architecture Nginx on Debian: two public roots and HTTP access checks
  4. Architecture Why Nginx before PHP-FPM: preparing a server for several applications
  5. Operations & Security From nginx -t to a running origin: controlled Nginx startup through the CLI

A project by the author · Weather Solution200

After reading, a look outside.

Temperature, rain and wind in your city. Check the forecast and plan the hours ahead.

Check the forecast
Next article