“nginx: [emerg] …” 是 nginx 启动和 reload 失败时的所有报错的前缀。这篇在本机用自己的 nginx 把常见的配置错误一个个构造出来,记录 nginx -t 的原文,再测 nginx -t 通过却启动失败、reload 失败后旧配置是否继续生效,以及 proxy_pass 里域名解析的两种写法。
指令名拼错(退出码 1):
nginx: [emerg] unknown directive "locatoin" in nginx.conf:8
nginx: configuration file nginx.conf test failed
指令后面漏了分号(退出码 1):
nginx: [emerg] directive "listen" is not terminated by ";" in nginx.conf:8
nginx: configuration file nginx.conf test failed
return 少了分号就直接接 }(退出码 1):
nginx: [emerg] unexpected "}" in nginx.conf:8
nginx: configuration file nginx.conf test failed
多了一个 }(退出码 1):
nginx: [emerg] unexpected "}" in nginx.conf:10
nginx: configuration file nginx.conf test failed
少了一个 }(退出码 1):
nginx: [emerg] unexpected end of file, expecting "}" in nginx.conf:10
nginx: configuration file nginx.conf test failed
指令放错位置(server 里写 upstream)(退出码 1):
nginx: [emerg] "upstream" directive is not allowed here in nginx.conf:8
nginx: configuration file nginx.conf test failed
参数个数不对(退出码 1):
nginx: [emerg] invalid number of arguments in "listen" directive in nginx.conf:8
nginx: configuration file nginx.conf test failed
用了没定义的变量(退出码 1):
nginx: [emerg] unknown "no_such_var" variable
nginx: configuration file nginx.conf test failed
同一个 server 里两个相同的 location(退出码 1):
nginx: [emerg] duplicate location "/a" in nginx.conf:8
nginx: configuration file nginx.conf test failed
include 的文件不存在(退出码 1):
nginx: [emerg] open() "<tmp>/conf.d/missing.conf" failed (2: No such file or directory) in nginx.conf:8
nginx: configuration file nginx.conf test failed
include 通配符没有匹配到文件(退出码 0):
nginx: the configuration file nginx.conf syntax is ok
证书文件不存在(退出码 1):
nginx: [emerg] cannot load certificate "<tmp>/nope.pem": BIO_new_file() failed (SSL: error:80000002:system library::No such file or directory:calling fopen(<tmp>/nope.pem, r) error:10000080:BIO routin
nginx: configuration file nginx.conf test failed
proxy_pass 的主机名解析不了(退出码 1):
nginx: [emerg] host not found in upstream "nonexistent-host.invalid" in nginx.conf:8
nginx: configuration file nginx.conf test failed
日志目录不存在(退出码 1):
nginx: [emerg] open() "<tmp>/nodir/access.log" failed (2: No such file or directory)
nginx: configuration file nginx.conf test failed
同一个端口重复 listen 同一个 server_name(退出码 0):
nginx: [warn] conflicting server name "a.test" on 127.0.0.1:9780, ignored大多数带文件名和行号;unknown variable 和日志目录那两个没有行号。
nginx -t:nginx: configuration file <tmp>/nginx.conf test is successful
第二个 nginx(另一个 pid 文件)用同一个端口启动(退出码 1):
nginx: [emerg] bind() to 127.0.0.1:9780 failed (48: Address already in use)
nginx: [emerg] bind() to 127.0.0.1:9780 failed (48: Address already in use)
…(共 6 行,同样的 bind() 失败会重试几次)
别的程序占着 9781,nginx -t(退出码 0):nginx: configuration file <tmp>/nginx.conf test is successful
真正启动(退出码 1):
nginx: [emerg] bind() to 127.0.0.1:9781 failed (48: Address already in use)
nginx: [emerg] bind() to 127.0.0.1:9781 failed (48: Address already in use)include 通配符没匹配到文件静默通过;重复的 server_name 只是警告。
reload 前:version 1
配置里 return 拼成 retrun 之后 nginx -s reload(退出码 1):
nginx: [emerg] unknown directive "retrun" in nginx.conf:8
reload 之后访问:version 1(仍然是旧配置)
改好之后再 reload:version 2端口被占用只会在真正启动时暴露。
用变量:nginx -t(退出码 0):nginx: configuration file <tmp>/nginx.conf test is successful
请求时:HTTP 502
error.log: [error] *2 no resolver defined to resolve nonexistent-host.invalid, request: "GET / HTTP/1.1", host: "127.0.0.1:9780"
配了一个连不上的 resolver(127.0.0.1:9):HTTP 502,耗时 1.00 s
error.log: [error] recv() failed (61: Connection refused) while resolving, resolver: 127.0.0.1:9
error.log: [error] *2 nonexistent-host.invalid could not be resolved (60: Operation timed out), request: "GET / HTTP/1.1", host: "127.0.0.1:9780"配置有错时旧配置继续生效;先 nginx -t 再 reload。
非 root 监听 127.0.0.1:80:失败(退出码 1):nginx: [emerg] bind() to 127.0.0.1:80 failed (13: Permission denied)
非 root 运行时配置了 user nobody;nginx -t(退出码 0):
nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in nginx.conf:3
nginx: the configuration file nginx.conf syntax is ok写死域名启动时解析;用变量推迟到请求时,需要 resolver。