如何解决Nginx反向代理后面具有Node.js服务的AWS EC2上的Docker Swarm
请忽略此内容作为背景信息,请参阅此以获得更新的描述:Correctly handle Socket.io on Docker Swarm using either Traefik or NGINX
我不知道为什么这行不通,因为几个月前我做了类似的设置。
我有一个两节点AWS EC2集群,在其上部署了Docker Swarm。如果有任何区别,则这些节点位于公共子网上。另外,在将其部署到AWS EC2上的Docker Swarm之前,我在笔记本电脑上建立了多节点Multipass网络,在其上部署了Swarm,并在其中部署了完全相同的设置,效果很好。
如果有所不同,则EC2节点位于us-west-2中的不同可用区中。
更新:作为一个实验,我在常规托管服务提供商(数字海洋)上设置了两个节点的群集。我获得了相同的经验-使用服务的一个实例运行正常,而使用两个实例的故障与下面相同。 FWIW该问题并非AWS独有。 / UPDATE
UPDATE2:在下面的NGINX配置中,有两个位置块,一个用于/socket.io
URL,另一个用于其他所有位置。我尝试将配置更改为仅使用该位置块,因为它包含升级连接的代码。但是结果仍然是相同的体验。 / UPDATE2
UPDATE3:我修复了应用程序和配置的许多问题。人们应该忽略此问题,因为此处的信息不完整。主要问题仍未解决。有关更新的问题,请参见:Correctly handle Socket.io on Docker Swarm using either Traefik or NGINX / UPDATE3
有两个域名指向此。 todo.geekwisdom.net
有两个A地址(轮询),每个地址指向不同的EC2节点。 fodo.geekwisdom.net
有一个A地址。
部署到Swarm的是配置为反向代理的NGINX容器。有一个使用Socket.IO的Node.js服务,因此需要在代理中支持WebSockets。您会注意到NGINX配置有一个路由来处理socket.io URL。并且该服务具有REDIS服务器,这是多节点Socket.IO设置所必需的。还有一个MySQL数据库。
如果我部署一个Node.js服务实例,那么一切都将正常运行。部署两个或多个服务实例无法正常工作。
在网络浏览器中,我收到这样的错误:
[Error] Failed to load resource: the server responded with a status of 400 (Bad Request) -- http://fodo.geekwisdom.net/socket.io/?EIO=3&transport=polling&t=NLM_mTG&sid=SRCuxWNx9wkLR30nAADE
[Error] WebSocket connection to 'ws://fodo.geekwisdom.net/socket.io/?EIO=3&transport=websocket&sid=SRCuxWNx9wkLR30nAADE' failed: WebSocket is closed before the connection is established.
然后在NGINX反向代理错误日志中,我目前正在收到这种消息。
2020/10/23 19:24:38 [info] 62#62: *2338 epoll_wait() reported that client prematurely closed connection,so upstream connection is closed too while sending request to upstream,client: 10.0.0.3,server: fodo.geekwisdom.net,request: "GET /socket.io/?EIO=3&transport=websocket&sid=BiTvvHJDIbwlOqxcAAC4 HTTP/1.1",upstream: "http://10.0.3.5:80/socket.io/?EIO=3&transport=websocket&sid=BiTvvHJDIbwlOqxcAAC4",host: "fodo.geekwisdom.net"
其他时间还有其他消息,但我丢失了这些消息。
显然,有关封闭的连接正在发生某些事情。例如后端服务出现故障。但是,正如我所说,当只有一个后端服务器实例时,完全相同的配置可以完美地工作。
此服务器的NGINX配置为:
server {
listen 80;
listen [::]:80;
server_name www.fodo.geekwisdom.net;
return 301 http://fodo.geekwisdom.net;
}
server {
listen 80;
listen [::]:80;
# Here put the domain name the server is to be known as.
server_name fodo.geekwisdom.net;
access_log /var/log/nginx/fodo.geekwisdom.net.access.log main;
error_log /var/log/nginx/fodo.geekwisdom.net.error.log debug;
# This is for handling the ACME challenge. Substitute the
# domain name here.
location /.well-known/ {
root /webroots/fodo.geekwisdom.net/;
}
# Use this for a static HTML site,specifically the default
# site supplied with the default Nginx container
# location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
# }
# Use this to proxy to Socket.IO service on the back-end service
# The reason is that Socket.IO requires upgrading the
# HTTP/1.1 connection to WebSocket.
# See:
# https://stackoverflow.com/questions/29043879/socket-io-with-nginx
location ^~ /socket.io/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy false;
proxy_pass http://todo;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
# Use this for proxying to a backend service
# The HTTPS session is terminated at this Proxy.
# The back end service will see a simple HTTP service.
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://todo;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}
这是仅HTTP的反向代理。但是使用Lets Encrypt实施HTTPS是预先设置的。为简化起见,我尚未进行设置。昨天我使用HTTPS进行设置时,也以类似方式失败。
Docker Swarm堆栈文件的相关部分是:
version: '3.8'
services:
cronginx:
image: nginx # robogeek/cronginx
ports:
- '80:80' # Public HTTP Port
- '443:443' # Public HTTPS Port
dns:
- 8.8.8.8
- 9.9.9.9
networks:
- webnet
deploy:
replicas: 1
placement:
constraints:
- "node.hostname==srv1"
volumes:
- /home/ubuntu/cronginx/etc-letsencrypt:/etc/letsencrypt
- /home/ubuntu/cronginx/webroots:/webroots
- /home/ubuntu/cronginx/logs:/var/log/nginx
- /home/ubuntu/cronginx/nginx-conf-d:/etc/nginx/conf.d
todo:
image: robogeek/todo-app:first-dockerize-redis
# container_name: todo-app
# ports:
# - "80:80"
networks:
- dbnet
- webnet
- redisnet
deploy:
replicas: 2
depends_on:
- db
- redis
dns:
- 8.8.8.8
- 9.9.9.9
redis:
image: "redis:5.0"
# container_name: redis
hostname: redis
#,"--bind","redis",# command: ["redis-server","0.0.0.0","--port","6379","--protected-mode","no"]
ports:
- "6379:6379"
deploy:
replicas: 1
networks:
- redisnet
dns:
- 8.8.8.8
- 9.9.9.9
networks:
dbnet:
driver: overlay
webnet:
driver: overlay
redisnet:
driver: overlay
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。