nginx proxypass 到 Flask app 时出现 504 错误,但是 docker 端口映射时就正常了

问题描述 投票:0回答:1

问题: 首先,我在建立多个容器的 AWS EC2 实例中收到 504 错误。

我使用 nginx 容器将“POST”请求代理传递到我的 Flask 后端容器。 nginx向客户端发送504后,我的Flask后端容器反馈。但是,当我使用docker映射时,它可以正确登录。

代码如下:

docker 撰写配置:

services:
    nginx:
      container_name: nginx-https
      image: nginx:latest
      restart: always
      ports:
       - "80:80"
       - "443:443"
      links:
       - web
       - twittor
       - sketch
      volumes:
       - ./certbot/conf:/etc/letsencrypt
       - ./conf.d:/etc/nginx/conf.d/:rw
       - ./certbot/www/:/var/www/certbot/:ro
    sketch:
      ports:
       - "8002:8002"
      container_name: sharing_sketch_20240711
      environment:
       - PYTHONUNBUFFERED=1
      image: pss_websocket

码头工人:

CONTAINER ID   IMAGE           COMMAND                  CREATED        STATUS        PORTS                                                                      NAMES
fc69badf4f09   pss_websocket   "./run_server.sh"        47 hours ago   Up 46 hours   0.0.0.0:8002->8002/tcp, :::8002->8002/tcp                                  sharing_sketch_20240711
2e8770807b4b   nginx:latest    "/docker-entrypoint.…"   3 days ago     Up 3 days     0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp   nginx-https

nginx 配置

server {
    listen 80;
    server_name 52.195.89.113;

    location / {
        proxy_pass http://sharing_sketch_20240711:8002;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        # WebSocket support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        # Set timeouts
        proxy_connect_timeout 120s;
        proxy_read_timeout 120s;
        proxy_send_timeout 120s;
        proxy_cache off;
    }
}

烧瓶
后端 运行服务器.sh

#!/bin/sh

gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -w 1 --threads 5 -b 0.0.0.0:8002 PSS.wsgi:application --access-logfile ./access.log --error-logfile ./error.log

wsgi.py

import os
import sys

sys.path.insert(0, os.getcwd())
from PSS import create_app

application = create_app()

路线.py

def index():
    # if current_user.is_authenticated:
    #     return redirect(url_for('index'))
    form=Loginform(meta={'csrf': False})
    print("index")
    if form.validate_on_submit():
        print("submit")
        u=User.query.filter_by(username=form.username.data).first()
        print("query")
        if u is None or not u.check_password(form.password.data):
            flash("Invalid username or password")
            return redirect(url_for('index'))
        session.clear()
        login_user(u,remember=form.remember_me.data)
        session['roomname']=form.roomname.data
        session['username']=form.username.data
        return redirect(url_for('room'))
    return render_template('index.html',form=form)

网站演示:

您可以使用账号登录。[IP:8002->用户名:123;密码:123;房间:123] http://52.195.89.113:8002/ [如果没有端口则无法登录。]

调试尝试

1.Nginx无法连接后端:

但是,我可以从 nginx 容器内部(通过使用 docker exec)卷曲到后端容器。[curl http://sharing_sketch_20240711:8002; 并获取 index.html 代码]

2.后端服务器繁忙:

但是,当我使用docker端口映射时,登录是好的。

3.后端没有收到nginx容器发来的消息:

我尝试使用tcpdump,并在容器的web.xml中收到错误504。我认为 nginx 容器正确发送到后端,但是后端不会对 nginx 做出反应,直到出现错误 504。

tcpdump 日志(sudo tcpdump -i br-2c93da463f14)

08:24:51.123625 IP ip-172-25-0-5.ap-northeast-1.compute.internal.39846 > ip-172-25-0-3.ap-northeast-1.compute.internal.8002: Flags [.], ack 1, win 502, options [nop,nop,TS val 711757314 ecr 296518398], length 0

08:24:51.123675 IP ip-172-25-0-5.ap-northeast-1.compute.internal.39846 > ip-172-25-0-3.ap-northeast-1.compute.internal.8002: Flags [P.], seq 1:668, ack 1, win 502, options [nop,nop,TS val 711757314 ecr 296518398], length 667
08:26:01.810397 IP ip-172-25-0-5.ap-northeast-1.compute.internal.http > 223-137-21-214.emome-ip.hinet.net.18981: Flags [.], ack 586, win 501, options [nop,nop,sack 1 {585:586}], length 0
08:26:51.199534 IP ip-172-25-0-5.ap-northeast-1.compute.internal.39846 > ip-172-25-0-3.ap-northeast-1.compute.internal.8002: Flags [F.], seq 668, ack 1, win 502, options [nop,nop,TS val 711877390 ecr 296518398], length 0
08:26:51.201095 IP ip-172-25-0-5.ap-northeast-1.compute.internal.http > 223-137-21-214.emome-ip.hinet.net.18981: Flags [P.], seq 1:330, ack 586, win 501, length 329: HTTP: HTTP/1.1 504 Gateway Time-out
08:26:51.241080 IP ip-172-25-0-3.ap-northeast-1.compute.internal.8002 > ip-172-25-0-5.ap-northeast-1.compute.internal.39846: Flags [.], ack 669, win 504, options [nop,nop,TS val 296638516 ecr 711877390], length 0
08:26:51.313524 IP 223-137-21-214.emome-ip.hinet.net.18981 > ip-172-25-0-5.ap-northeast-1.compute.internal.http: Flags [.], ack 330, win 511, length 0
08:26:51.364082 IP ip-172-25-0-3.ap-northeast-1.compute.internal.8002 > ip-172-25-0-5.ap-northeast-1.compute.internal.39846: Flags [P.], seq 1:431, ack 669, win 504, options [nop,nop,TS val 296638639 ecr 711877390], length 430
08:26:51.364106 IP ip-172-25-0-5.ap-northeast-1.compute.internal.39846 > ip-172-25-0-3.ap-northeast-1.compute.internal.8002: Flags [R], seq 2748267786, win 0, length 0

那么,如何检查是哪部分导致了504错误,并修改代码呢?

docker flask nginx docker-compose gunicorn
1个回答
0
投票

向面临同样问题的人问好。 经过多次尝试,我将Nginx配置修改如下,成功了。

server {
    listen 80;
    # using new aws EC2 instance IP 
    server_name 57.180.251.162;

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_connect_timeout 120s;
        proxy_read_timeout 120s;
        proxy_send_timeout 120s;
        proxy_cache off;
        proxy_pass http://sharing_sketch_20240711:8002;
    }
    location /room {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_connect_timeout 120s;
        proxy_read_timeout 120s;
        proxy_send_timeout 120s;
        proxy_cache off;
        proxy_pass http://sharing_sketch_20240711:8002;
    }
    location /socket.io {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_pass http://sharing_sketch_20240711:8002/socket.io;
    }
}

有必要指定 Nginx 代理通道应该升级哪些端点以避免错误。

© www.soinside.com 2019 - 2024. All rights reserved.