Nginx 无法启动:“未找到主机”

Nginx 无法启动:“未找到主机”

每次我打开电脑时,我的 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 Qui 2017-07-27 07:11:02 BRT; 59s ago
  Process: 1006 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)

Jul 27 07:11:01 note-gui systemd[1]: Starting A high performance web server and a reverse proxy server...
Jul 27 07:11:02 note-gui nginx[1006]: nginx: [emerg] host not found in upstream "localhost" in /etc/nginx/sites-enabled/default:27
Jul 27 07:11:02 note-gui nginx[1006]: nginx: configuration file /etc/nginx/nginx.conf test failed
Jul 27 07:11:02 note-gui systemd[1]: nginx.service: Control process exited, code=exited status=1
Jul 27 07:11:02 note-gui systemd[1]: Failed to start A high performance web server and a reverse proxy server.
Jul 27 07:11:02 note-gui systemd[1]: nginx.service: Unit entered failed state.
Jul 27 07:11:02 note-gui systemd[1]: nginx.service: Failed with result 'exit-code'.
~

如果我做:

sudo systemctl restart nginx

它运行良好,但每次启动 Ubuntu 时我都必须这样做。

我该如何解决这个问题,这样我就不必每次都重新启动 nginx?我使用 Ubuntu 16.04。

答案1

该服务nginx -t在启动服务器之前运行(测试),并引发错误:

host not found in upstream "localhost" in /etc/nginx/sites-enabled/default:27

这意味着您正在引用 localhost,可能是作为proxy_pass第 27 行指令的一部分(只是猜测)/etc/nginx/sites-enabled/default。而 Nginx 无法解析它。

这可能会引起更广泛的关注,但解决这个问题最简单的方法就是用localhost替换127.0.0.1

相关内容