Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx in Docker container gets `connection reset` error, but works fine without a container

I've a simple setup of 3 components that I've wrapped into docker containers:

  • PostgreSQL database, container named workflows-db
  • Django + uWSGI web server on port 8000, container named workflows-django
  • Nginx reverse proxy, container named workflows-nginx

I'm running Postgres and Django as containers and they work fine. Now, I want to add Nginx. If I simply install Nginx locally on the host machine (without Docker) and run it, my setup works fine.

But if I put exactly the same configuration of Nginx into a separate docker container, it fails to respond to https:// requests 100% of times:

This site can’t be reached

The connection was reset.
ERR_CONNECTION_RESET

Here's my configuration mysite.conf, put in /etc/nginx/sites-available/mysite.conf and /etc/nginx/sites-enabled/mysite.conf:

upstream django {
    server workflows-django:8000;
}

server {
    listen 80;
    server_name workflows.devbg.us;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;

    server_name workflows.devbg.us;
    ssl_certificate /etc/ssl/private/bostongene.crt;
    ssl_certificate_key /etc/ssl/private/bostongene.key;
    charset utf-8;

    client_max_body_size 75M;

    location /media {
        alias /srv/workflows/media;
    }

    location /static {
        alias /srv/workflows/static;
    }

    location / {
        # We can talk to upstream django either via uwsgi or just http proxy

        # uwsgi:
        uwsgi_pass django;
        include /etc/nginx/uwsgi_params;


        # http proxy:
        #proxy_set_header Host $host;
        #proxy_pass http://django

    }
}

I run nginx container after django and postgres containers with the following parameters:

docker run --name workflows-nginx --volumes-from workflows-db --volumes-from workflows-django --link workflows-django:workflows-django -p 80:80 -p 443:443 -d workflows-nginx

The file /etc/hosts on my host machine looks as follows:

127.0.0.1   localhost
127.0.0.1   workflows-db
127.0.0.1   workflows-django
127.0.0.1   workflows-nginx
127.0.0.1   workflows.devbg.us

Do you have any ideas about how to troubleshoot this? Nginx error.log doesn't contain these errors.

like image 210
Boris Burkov Avatar asked Oct 18 '25 13:10

Boris Burkov


1 Answers

The official docker image at docker hub seems to be stripped of nginx uwsgi adapter.

So, I just manually created my own Nginx Dockerfile from debian:jessie and voila. Seems that uwsgi adapter is available somewhere in nginx-common or nginx-full Debian packages.

My Dockerfile:

FROM debian:jessie

RUN apt-get update && apt-get install -y nginx \
                        ca-certificates \
                        gettext-base

COPY yoursite.conf /etc/nginx/sites-available/yoursite.conf
COPY yoursite.conf /etc/nginx/sites-enabled/yoursite.conf
COPY yoursite.crt /etc/ssl/private/
COPY yoursite.key /etc/ssl/private/

# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
    && ln -sf /dev/stderr /var/log/nginx/error.log

EXPOSE 80 443

CMD ["/usr/sbin/nginx", "-g", "daemon off;"]
like image 120
Boris Burkov Avatar answered Oct 20 '25 03:10

Boris Burkov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!