【问题标题】:Aiohttp and NGINX running in Docker在 Docker 中运行的 Aiohttp 和 NGINX
【发布时间】:2019-03-05 06:16:08
【问题描述】:

长话短说

我想在 nginx 网络服务器上运行 aiohttp 后端服务。两者都应该在 docker 容器中运行。此外,我的前端 Angular 应用程序应该访问我的后端服务。

预期行为

我希望 nginx 网络服务器可以连接到我的后端系统 aiohttp,在 docker 中运行。

实际行为

当我尝试在我的 aiohttp 后端服务上调用 GET 请求时,我总是在 docker 日志中收到错误消息。

nginx_1 | 2018/09/29 13:48:03 [error] 6#6: *1 connect() failed (111: Connection refused) while connection to upstream, client: 172.19.0.1, server: , request: "GET /toolservice/波动性?command=pslist HTTP/1.1”,上游:“http://172.19.0.2:80/toolservice/volatility?command=pslist”,主机:“localhost” nginx_1 | 172.19.0.1 - - [29/Sep/2018:13:48:03 +0000] "GET /toolservice/volatility?command=pslist HTTP/1.1" 502 576 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36" "-"

Docker-compose.yml

version: '3'

services:

    nginx:
      build: ./nginx
      restart: always
      depends_on:
        - toolservice
        - ifs
      ports:
       - "80:80"

    ifs:
      restart: always
      build: ../ifsbackend
      ports:
        - "8002:8000"

    toolservice:
      restart: always
      build: ../ToolService
      ports:
        - "8001:8000"

Dockerfile nginx 网络服务器

FROM nginx:1.13-alpine

RUN rm /etc/nginx/conf.d/default.conf
COPY conf/server.conf /etc/nginx/conf.d/

Dockerfile aiohttp 后端

FROM python:3.6.6-alpine
COPY tool /
COPY requirements.txt /
COPY toolservice_config.yaml /
RUN apk update && apk add \
    python3-dev \
    musl-dev \
    gcc \
    && pip install -r requirements.txt \
    && pip install virtualenv
RUN python3 -m virtualenv --python=python3 virtualenv
EXPOSE 8080
CMD [ "python", "server.py" ]

Nginx 网络服务器配置

#upstream toolservice {
 # server 0.0.0.0:8001 fail_timeout=0;
#}

server {
    listen 80;

    #server_name localhost;
    proxy_buffers 8 16k;
    proxy_buffer_size 32k;

    location /toolservice {
        proxy_pass http://toolservice;
        proxy_redirect default;

        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


    }
    location /ifs {
      proxy_pass http://ifs;
      proxy_redirect default;

      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    }
}

Aiohttp 工具服务后端

from aiohttp import web
from routes import setup_routes
from settings import config

app = web.Application()
setup_routes(app)
app['config'] = config
web.run_app(app, port=8001)

【问题讨论】:

    标签: python python-3.x docker nginx aiohttp


    【解决方案1】:

    Aiohttp 运行在容器 toolservice 的 8001 端口上,但你的代理是 80 端口。

    proxy_pass http://toolservice;
    

    尝试代理到 8001:

    proxy_pass http://toolservice:8001;
    

    也许您需要修复 toolservice 容器的端口发布问题 - 我不能 100% 确定:

      ports:
        - "8001:8001"
    

    【讨论】:

      猜你喜欢
      • 2015-12-16
      • 2019-11-24
      • 2020-07-02
      • 1970-01-01
      • 2022-01-24
      • 2022-01-14
      • 2015-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多