nginx empty regex in server name "~"
Fix empty regex in server name "~" in nginx. Check for a space after the tilde, attach the pattern to it, or use an exact host name.
$ nginx -t2026/11/23 02:24:54 [emerg] 1#1: empty regex in server name "~" in /etc/nginx/nginx.conf:5nginx: [emerg] empty regex in server name "~" in /etc/nginx/nginx.conf:5nginx: configuration file /etc/nginx/nginx.conf test failedRead the value between the quotes. The lone "~" is one complete server-name argument as nginx received it. A pattern elsewhere on the same line can still produce this error.
[1] Inspect the arguments on the reported line. Each of these settings failed separately with the message above.
| Setting tested | First name nginx reads |
|---|---|
server_name ~; | ~ |
server_name ~ ^api[0-9]+\.example\.com$; | ~ |
In the second case a space separates the pattern from the tilde. The PCRE-enabled parser in 1.28.0 returns an error for a one-character argument beginning with a tilde before it tries to compile a pattern. For generated configurations, inspect the rendered file at the reported line. nginx 1.28.0 source
[2] Attach the pattern directly to the tilde to match a family of hosts. Replace the faulty line in the same server block.
server_name ~^api[0-9]+\.example\.com$;This pattern describes names such as api1.example.com and api23.example.com. The anchors mark the start and end of the name. The escaped dots match literal periods. In a server name, the tilde and pattern form one argument. nginx server names
[3] Use a literal name if the server handles a single host. Drop both the tilde and the regex escapes.
server_name api1.example.com;[4] Test the edited configuration. The attached pattern and the literal name each produced this result.
$ nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successfulAfter applying the change, check a host that should match and one that belongs to another server. If the match set changed unexpectedly, restore the previous working setting and retest. For a duplicate name on the same address and port, see conflicting server name ignored.
Analyze your Nginx log in the browser ↗ — nothing is uploaded.
Check it with the Regex tester (JS · grep -E · grep -P) tool ↗ — calculated in your browser.