我正在学习如何在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-two是将运行实际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.
答案 0 :(得分:0)
覆盖您删除了http上下文的整个nginx.conf。根据{{3}}
,仅在http {}块内允许上游Debian / ubuntu使用/ etc / nginx / sites-available / *进行站点定义,这就是您需要创建自己的配置文件的地方,如果需要,每个虚拟主机一个。然后启用它,使其与/ etc / nginx / sites-enabled / *符号链接。或者为简单起见,使用/ etc / nginx / sites-available / default作为现有配置文件,然后在此处进行更改。保存原始文件,直到可以使用为止。并在更改后重新加载nginx。当然,还可以还原原始的/etc/nginx/nginx.conf。