我有一些应用程序,写在Python3.7上,在ASGI和Starlette上运行。我需要应用程序接受只HTTP的请求。
我已经通过docker-compose和add HTTPSRedirectMiddleware打开了端口443和80,以确保所有连接都将重定向到https
docker-compose
version: '3.4'
services:
api:
image: api
stop_grace_period: 30s
environment:
API_VERSION: $API_VERSION
deploy:
mode: replicated
replicas: 1
restart_policy:
condition: on-failure
update_config:
delay: 1s
monitor: 10s
order: stop-first
parallelism: 1
failure_action: rollback
ports:
- 80:80
443:443
networks:
default:
driver: overlayDockerfile
ARG PYTHON_VERSION=3.7.3
FROM python:${PYTHON_VERSION} as builder
ENV PYTHONBUFFERED 1
WORKDIR /wheels
COPY ["requirements.txt","/wheels/requirements/"]
RUN pip install -U pip \
&& pip wheel -r ./requirements/requirements.txt
FROM python:${PYTHON_VERSION} as release
COPY --from=builder /wheels /wheels
ARG API_PORT
ENV PYTHONPATH=/app \
GUWORKERS=0.5\
WORKERS_PER_CORE=1\
GUWORKERS_CONNECTIONS=50
COPY ["./", "/"]
RUN pip install -r /wheels/requirements/requirements.txt\
-f /wheels\
&& rm -rf /wheels \
&& rm -rf /root/.cache/pip/* \
&& chmod +x /scripts/entrypoint.sh
WORKDIR /app/
ENTRYPOINT ["/scripts/entrypoint.sh"]Python
def create_app():
app = Starlette()
app.add_exception_handler(HTTPException, error_handler)
app.add_middleware(ServerErrorMiddleware, handler=server_error_handler)
app.add_middleware(HTTPSRedirectMiddleware)
app.add_route("/", calculate, methods=["POST"])
return app但是,当我尝试通过本地主机上运行的容器上的邮递员发送POST请求时,我得到了无法连接的错误,并且在应用程序的日志中我会得到
[WARNING] Invalid HTTP request received.发布于 2020-03-16 20:25:35
您需要指定SSL证书和私钥,HTTPS才能工作。
$ gunicorn --keyfile=./key.pem --certfile=./cert.pem -k uvicorn.workers.UvicornWorker example:app你可以在这里找到更多:https://www.uvicorn.org/deployment/#running-with-https
https://stackoverflow.com/questions/56732578
复制相似问题