Nginx反向代理后面具有Node.js服务的AWS EC2上的Docker Swarm

如何解决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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-