502 Bad Gateway - Dockerized Django Gunicorn NGINX on AWS EBS

Viewed 1014

So I am trying to deploy over two days and I can't seem to to manage to get fix this error. I am getting a 502 bad gateway error. I am trying to deploy a two-container app to elastic beanstalk. The two containers are:

  • Nginx reverse proxy container
  • Django app with gunicorn

I SSHed to my EC-2 instance and fetched the nginx logs

2020/05/09 21:48:25 [error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 84.119.15.244, server: , request: "GET / HTTP/1.1", upstream: "http://172.17.0.2:8000/", host: "http://xxx-xxx-xxx.us-east-1.elasticbeanstalk.com/"
84.119.15.244 - - [09/May/2020:21:48:25 +0000] "GET / HTTP/1.1" 502 560 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" "-"
2020/05/09 21:48:25 [error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 84.119.15.244, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://172.17.0.2:8000/favicon.ico", host: "http://xxx-xxx-xxx.us-east-1.elasticbeanstalk.com/", referrer: "http://xxx-xxx-xxx.us-east-1.elasticbeanstalk.com/"

It seems like the upstream refused to connect. I also took the gunicorn logs and it looks like server started successfully

[2020-05-09 21:48:07 +0000] [1] [INFO] Starting gunicorn 19.9.0
[2020-05-09 21:48:07 +0000] [1] [INFO] Listening at: http://127.0.0.1:8000 (1)
[2020-05-09 21:48:07 +0000] [1] [INFO] Using worker: sync
[2020-05-09 21:48:07 +0000] [9] [INFO] Booting worker with pid: 9

here is my nginx default.conf file

upstream app {

    server app:8000;
}


server {
    listen 80;


    location / {

        proxy_pass http://app;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;

    }

    location /staticfiles/ {
        alias /app/staticfiles/;
    }
}

my nginx Dockerfile:

FROM nginx
COPY ./default.conf /etc/nginx/conf.d/default.conf

my django app Dockerfile(Which also starts the gunicorn server):

FROM python:3.7
WORKDIR '/app'

COPY ./requirements.txt ./
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000


ENTRYPOINT ["/app/entrypoint.sh"]

CMD ["gunicorn", "instabot.wsgi:application", "--bind 0.0.0.0:8000"]

and my Dockerrun.aws.json file:

{
    "AWSEBDockerrunVersion": 2,

    "Authentication": {
        "Bucket": "elasticbeanstalk-us-east-1-xxxx",
        "Key": ".dockercfg"
    },
    "containerDefinitions": [{
            "name": "app",
            "image": "registry.gitlab.com/xxx/tester:latest",
            "hostname": "app",
            "essential": false,
            "memory": 128
        },

        {
            "name": "nginx",
            "image": "registry.gitlab.com/xxx/tester/nginx:latest",
            "hostname": "nginx",
            "essential": true,
            "portMappings": [{
                "hostPort": 80,
                "containerPort": 80
            }],
            "links": ["app"],
            "memory": 128

        }

    ]
}

I can't seem to understand whats causing the refused connection - gunicorn is listening on port 8000. And I set the upstream and the reverse proxy there. Will be happy to hear some thoughts!

0 Answers
Related