5
votes

Long story short

I would like to run aiohttp backend services on a nginx webserver. Both should be running in docker containers. Furthermore my frontend angular application should access my backend services.

Expected behaviour

I expect that the nginx webserver could connect to my backend system aiohttp, running in docker.

Actual behaviour

I am always getting an error in the docker logs while I am trying to call a GET request on my aiohttp backend service.

nginx_1 | 2018/09/29 13:48:03 [error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.19.0.1, server: , request: "GET /toolservice/volatility?command=pslist HTTP/1.1", upstream: "http://172.19.0.2:80/toolservice/volatility?command=pslist", host: "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 webserver

FROM nginx:1.13-alpine

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

Dockerfile aiohttp backend

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 webserver config

#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 toolservice backend

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)
1

1 Answers

3
votes

Aiohttp is running on the port 8001 in the container toolservice, but your proxying to the port 80.

proxy_pass http://toolservice;

Try proxying to 8001:

proxy_pass http://toolservice:8001;

Maybe you will need to fix publishing of the port for toolservice container - I'm not 100% sure:

  ports:
    - "8001:8001"