![nginx 无法使用 systemd 在系统启动时启动](https://linux22.com/image/1133389/nginx%20%E6%97%A0%E6%B3%95%E4%BD%BF%E7%94%A8%20systemd%20%E5%9C%A8%E7%B3%BB%E7%BB%9F%E5%90%AF%E5%8A%A8%E6%97%B6%E5%90%AF%E5%8A%A8.png)
我主要使用 nginx 作为 http(s) 代理,为此我需要提供一些域名,nginx 会在启动时尝试解析这些域名(DNS)。
如果我在 PC 完全启动后手动启动 nginx(使用 systemctl start),这就可以正常工作,但是当仅启用它自动启动时,我收到以下问题(mydomainname 通常会真正解决,我只是在这里隐藏了它):
$ systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2016-02-13 10:41:47 CET; 45min ago
Process: 844 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)
Feb 13 10:41:47 mysystem systemd[1]: Starting A high performance web server and a reverse proxy server...
Feb 13 10:41:47 mysystem nginx[844]: nginx: [emerg] host not found in upstream "mydomainname" in /etc/nginx/sites-enabled/default:8
Feb 13 10:41:47 mysystem nginx[844]: nginx: configuration file /etc/nginx/nginx.conf test failed
Feb 13 10:41:47 mysystem systemd[1]: nginx.service: Control process exited, code=exited status=1
Feb 13 10:41:47 mysystem systemd[1]: Failed to start A high performance web server and a reverse proxy server.
Feb 13 10:41:47 mysystem systemd[1]: nginx.service: Unit entered failed state.
Feb 13 10:41:47 mysystem systemd[1]: nginx.service: Failed with result 'exit-code'.
我使用的是 ubuntu 15.10,我使用标准 nginx 包。我已经尝试过:
- 将 systemd 脚本中的 after 从 network.target 更改为 network-online.target
- 将 nss-lookup.target 添加到 After
- 将其放入所有三个:network.target network-online.target nss-lookup.target
但它一直给出那个错误。
答案1
经过一番研究,我发现启用 systemd 等待网络管理器完成的组件默认情况下未启用。按照以下方式启用后,它可以完美运行:
sudo systemctl enable NetworkManager-wait-online.service