我有一个 Flask 应用程序在带有 Gunicorn 的 AWS App Runner 中运行。它运行良好,但我添加了一个新功能,其中包括调用不同端点的 API,并需要大约 2-3 分钟才能返回响应。
在本地,它工作正常,但部署后,每次我调用此 API 时,它都会返回 502 Bad Gateway。日志看起来很好,没有错误,我也看不出与本地运行有任何差异。所有其他端点都工作正常。
Dockerfile
FROM python:3.11
ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
RUN pip cache purge
RUN pip install --upgrade pip
# Install dependencies:
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Run the application:
COPY . .
CMD ["gunicorn", "--config", "gunicorn_config.py", "app:app"]
Gunicorn_config.py
import os
workers = int(os.environ.get('GUNICORN_PROCESSES', '2'))
threads = int(os.environ.get('GUNICORN_THREADS', '4'))
bind = os.environ.get('GUNICORN_BIND', '0.0.0.0:8080')
#timeout = int(os.environ.get('GUNICORN_TIMEOUT', '600'))
forwarded_allow_ips = '*'
secure_scheme_headers = { 'X-Forwarded-Proto': 'https' }
AWS App Runner 应用程序有 120 秒的超时限制,请参阅此处的文档:
HTTP 请求总共有 120 秒的请求超时限制。 120 秒包括应用程序读取请求(包括正文)以及完成写入 HTTP 响应所需的时间。