【问题标题】:Setting up a load balancer using nginx使用 nginx 设置负载均衡器
【发布时间】:2019-08-24 10:13:30
【问题描述】:

我正在学习如何在 AWS 上使用 nginx 设置负载均衡器。

我在 AWS 上搭建了一个基本的 ubuntu 18.04 服务器,然后做了以下操作:

sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install nginx -y

然后我将 /etc/nginx/nginx.conf 替换为以下内容:

upstream backend {
    server xxx.24.20.11;
    server xxx.24.20.12;
}

server {
    listen 80;
    location / {
        proxy_pass http://backend;
    }
}

然后我尝试通过执行以下操作重新启动 nginx 服务器:

sudo service nginx stop
sudo service nginx start

但我收到错误消息:

Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xe" for details.

原来如此

systemctl status nginx.service

这就是我得到的:

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 Wed 2019-04-03 01:13:27 UTC; 23s ago
     Docs: man:nginx(8)
  Process: 1822 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
  Process: 1748 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 1865 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)
  Main PID: 1752 (code=exited, status=0/SUCCESS)

Apr 03 01:13:27 load-balancer.xxxx.com systemd[1]: Starting A high performance web server and a reverse proxy server...
Apr 03 01:13:27 load-balancer.xxx.com nginx[1865]: nginx: [emerg] "upstream" directive is not allowed here in /etc/nginx/nginx.conf:2
Apr 03 01:13:27 load-balancer.xxx.com nginx[1865]: nginx: configuration file /etc/nginx/nginx.conf test failed
Apr 03 01:13:27 load-balancer.xxx.com systemd[1]: nginx.service: Control process exited, code=exited status=1
Apr 03 01:13:27 load-balancer.xxx.com systemd[1]: nginx.service: Failed with result 'exit-code'.
Apr 03 01:13:27 load-balancer.xxx.com systemd[1]: Failed to start A high performance web server and a reverse proxy server.

我查看了两个单独的教程,它们都使用“上游”指令。有什么想法吗?

编辑:

我将 nginx.conf 恢复为原始格式:

sudo cp /etc/nginx/nginx.original /etc/nginx/nginx.conf

然后,我做了以下事情:

sudo su
echo > /etc/nginx/sites-available/load-balancer.conf

然后我将以下内容添加到 /etc/nginx/sites-available/load-balancer.conf

http {
  upstream backend {
    server docker-one.xxxxxxx.com;
    server docker-two.xxxxxxx.com;
  }

  server {
    listen 80;
    server_name load-balancer.xxxxxxx.com;
    location / {
      proxy_pass http://backend;
    }
  }
}

load-balancer.xxxxxxx.com 是我用于测试的域名,docker-one 和 docker-2 是运行实际 Web 应用程序的两个域。

然后我做了一个符号链接:

ln -s /etc/nginx/sites-available/load-balancer.conf /etc/nginx/sites-enabled/

然后我重新启动了服务器。备份后,我执行了以下操作:

sudo service nginx stop
sudo service nginx start

我收到一条错误消息,告诉我 nginx 服务失败,所以我做了:

systemctl status nginx.service

这给了我以下错误:

● 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 Wed 2019-04-03 19:42:34 UTC; 19s ago
     Docs: man:nginx(8)
  Process: 1549 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)

Apr 03 19:42:34 load-balancer.xxxxxxx.com systemd[1]: Starting A high performance web server and a reverse proxy server...
Apr 03 19:42:34 load-balancer.xxxxxxx.com nginx[1549]: nginx: configuration file /etc/nginx/nginx.conf test failed
Apr 03 19:42:34 load-balancer.xxxxxxx.com systemd[1]: nginx.service: Control process exited, code=exited status=1
Apr 03 19:42:34 load-balancer.xxxxxxx.com systemd[1]: nginx.service: Failed with result 'exit-code'.
Apr 03 19:42:34 load-balancer.xxxxxxx.com systemd[1]: Failed to start A high performance web server and a reverse proxy server.

【问题讨论】:

    标签: nginx nginx-reverse-proxy


    【解决方案1】:

    覆盖整个 nginx.conf 你删除了 http 上下文。根据https://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream

    ,上游仅允许在 http {} 块内

    Debian/ubuntu 使用 /etc/nginx/sites-available/* 来定义站点,这就是您需要创建自己的配置文件的地方,如果需要,每个虚拟主机一个。然后启用它,使其符号链接到 /etc/nginx/sites-enabled/*。或者为了简单起见,使用 /etc/nginx/sites-available/default 作为现有配置文件,在那里进行更改。保存原件,直到你得到它的工作。并在上述更改后重新加载 nginx。当然也恢复原来的/etc/nginx/nginx.conf。

    【讨论】:

    • 我用 http 上下文尝试过,它也没有工作。这与nginx找不到配置文件无关
    • 正如您的错误消息和描述所说,您删除了整个默认的 nginx.conf,并用应该位于其余配置中的配置块覆盖它。这次日志中的错误信息是什么?不仅仅是“没有工作”。如前所述,恢复 nginx 的原始状态并修改 sites-available 目录中的文件默认值。您可能还想在 nginx 全新安装后确认它按原样工作。
    • 据我所知,nginx 默认安装开箱即用。如果它报告 nginx.conf 未通过测试之类的内容,则必须更改其中的某些内容,而不是原始内容。因此,如果您搞砸了它的配置并且无法手动恢复它,最好删除并清除所述应用程序并干净地安装它,以达到它最初工作的状态。然后继续编辑正确的配置文件...
    • 是的,默认安装有效。我的问题是关于将 nginx 设置为负载均衡器,我正在关注如何做到这一点的教程
    • 是的,有几次未能阅读和理解您的帖子,这让我有点不知所措。也许其他人可以帮忙。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    • 2013-07-18
    • 2015-08-26
    相关资源
    最近更新 更多