title: nginx docker container as reverse proxy for services running in docker containers
body: <p>I have nginx running in a docker container which I want to use as reverse proxy for services running in docker containers. ATM I have two services, django and owncloud.</p>
  
  <p>Both services works when accessing them directory e.g. <code>localhost:4000</code> and <code>localhost:5000</code>.</p>
  
  <p>When I'm accessing <code>localhost/owncloud</code> I get <code>502 Bad Gateway</code><br>
  Nginx log:<br>
  <code>2020-04-27T13:21:54.852082794Z 2020/04/27 13:21:54 [error] 6#6: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: , request: "GET /owncloud/ HTTP/1.1", upstream: "http://172.18.0.4:4000/", host: "localhost"</code></p>
  
  <p>When I'm accessing <code>localhost/django</code> I get <code>page not found</code> from Django, which is fine, but I if I try to access <code>localhost/django/admin</code> I get <code>page not found</code> from Nginx.<br>
  Nginx log:<br>
  <code>2020-04-27T13:29:11.173933857Z 2020/04/27 13:29:11 [error] 6#6: *4 "/etc/nginx/html/sv/admin/index.html" is not found (2: No such file or directory), client: 172.18.0.1, server: , request: "GET /sv/admin/ HTTP/1.1", host: "localhost"</code></p>
  
  <p>I'm new to Nginx and have messed around with the <code>nginx.conf</code> a lot with no success. Here are the relevant files:</p>
  
  <p><code>/docker-compose.yml</code>  </p>
  
  <pre><code>version: '3'
  networks:
    nginx-net:
  </code></pre>
  
  <p><code>/nginx/docker-compose.yml</code>  </p>
  
  <pre><code>version: '3'
  services:
    internal-nginx:
      build:
        context: ./nginx
      restart: unless-stopped
      image: web-nginx
      container_name: nginx
      ports:
        - "80:80"
        - "443:443"
      networks:
        - nginx-net
      depends_on: 
        - internal-django
        - internal-owncloud
  </code></pre>
  
  <p><code>/nginx/Dockerfile</code>  </p>
  
  <pre><code>FROM nginx
  
  COPY nginx.conf /etc/nginx/nginx.conf
  COPY nginx.conf /etc/nginx/nginx-raw.conf
  
  RUN rm /etc/nginx/conf.d/default.conf || true
  COPY nginx.conf /etc/nginx/conf.d
  COPY html /usr/share/nginx/html
  EXPOSE 80
  EXPOSE 443
  </code></pre>
  
  <p><code>/nginx/nginx.conf</code>  </p>
  
  <pre><code>user  nginx;
  worker_processes  1;
  
  error_log  /var/log/nginx/error.log warn;
  pid        /var/run/nginx.pid;
  
  events {
      worker_connections  1024;
  }
  http {
      include       /etc/nginx/mime.types;
  
      default_type  application/octet-stream;
  
      log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';
      access_log off;
  
      sendfile        on;
  
      keepalive_timeout  65;
  
      client_max_body_size 30000M;
  
      proxy_max_temp_file_size 0;
  
      server {
          listen  80 default;
  
          location /django/ {
              proxy_pass http://internal-django:5000;
              proxy_set_header Host $host;
              proxy_set_header X-Real-IP $remote_addr;
              rewrite ^/django(.*)$ $1 break;
          }
          location /owncloud/ {
              proxy_pass http://internal-owncloud:4000;
              proxy_set_header Host $host;
              proxy_set_header X-Real-IP $remote_addr;
              rewrite ^/owncloud(/.*)$ $1 break;
          }
      }
  }
  </code></pre>
  
  <p><code>/owncloud/docker-compose.yml</code>  </p>
  
  <pre><code>version: '3'
  
  volumes:
    files:
      driver: local
    mysql:
      driver: local
    backup:
      driver: local
    redis:
      driver: local
  
  services:
    internal-owncloud:
      image: owncloud/server:${OWNCLOUD_VERSION}
      restart: unless-stopped
      ports:
        - ${HTTP_PORT}:8080
      depends_on:
        - db
        - redis
      environment:
        - OWNCLOUD_DOMAIN=${OWNCLOUD_DOMAIN}
        - OWNCLOUD_DB_TYPE=mysql
        - OWNCLOUD_DB_NAME=owncloud
        - OWNCLOUD_DB_USERNAME=owncloud
        - OWNCLOUD_DB_PASSWORD=owncloud
        - OWNCLOUD_DB_HOST=db
        - OWNCLOUD_ADMIN_USERNAME=${ADMIN_USERNAME}
        - OWNCLOUD_ADMIN_PASSWORD=${ADMIN_PASSWORD}
        - OWNCLOUD_MYSQL_UTF8MB4=true
        - OWNCLOUD_REDIS_ENABLED=true
        - OWNCLOUD_REDIS_HOST=redis
      healthcheck:
        test: ["CMD", "/usr/bin/healthcheck"]
        interval: 30s
        timeout: 10s
        retries: 5
      volumes:
        - files:/mnt/data      
      networks: 
        - nginx-net
  
    db:
      image: webhippie/mariadb:latest
      restart: unless-stopped
      environment:
        - MARIADB_ROOT_PASSWORD=owncloud
        - MARIADB_USERNAME=owncloud
        - MARIADB_PASSWORD=owncloud
        - MARIADB_DATABASE=owncloud
        - MARIADB_MAX_ALLOWED_PACKET=128M
        - MARIADB_INNODB_LOG_FILE_SIZE=64M
      healthcheck:
        test: ["CMD", "/usr/bin/healthcheck"]
        interval: 30s
        timeout: 10s
        retries: 5
      volumes:
        - mysql:/var/lib/mysql
        - backup:/var/lib/backup
      networks: 
        - nginx-net
  
    redis:
      image: webhippie/redis:latest
      restart: unless-stopped
      environment:
        - REDIS_DATABASES=1
      healthcheck:
        test: ["CMD", "/usr/bin/healthcheck"]
        interval: 30s
        timeout: 10s
        retries: 5
      volumes:
        - redis:/var/lib/redis
      networks: 
        - nginx-net
  </code></pre>
  
  <p><code>.env</code>  </p>
  
  <pre><code>OWNCLOUD_VERSION=10.4
  OWNCLOUD_DOMAIN=localhost
  ADMIN_USERNAME=admin
  ADMIN_PASSWORD=admin
  HTTP_PORT=4000
  </code></pre>
  
  <p><code>/owncloud/Dockerfile</code>  </p>
  
  <pre><code>FROM owncloud/server
  EXPOSE 8080
  </code></pre>
  
  <p><code>/django/docker-compose.yml</code>  </p>
  
  <pre><code>version: '3'
  services:
    internal-django:
      build:
        context: ./django
      ports:
      - "5000:5000"
      restart: unless-stopped
      container_name: django
      image: web-django
      networks: 
        - nginx-net
  </code></pre>
  
  <p><code>/django/Dockerfile</code>  </p>
  
  <pre><code>FROM python:3.7
  COPY requirements.txt /
  RUN pip install -r requirements.txt
  COPY . /app
  WORKDIR /app
  CMD ["python3", "manage.py", "runserver", "0.0.0.0:5000"]
  EXPOSE 5000
  </code></pre>
  
  <p><strong>EDIT 1</strong><br>
  Running <code>docker network inspect web_nginx-net</code>  </p>
  
  <pre><code>[
      {
          "Name": "web_nginx-net",
          "Id": "ae511269418b963e1d347d3776903161cfbaf68dd024700e35204ffce98b5503",
          "Created": "2020-04-23T00:00:46.073855454+02:00",
          "Scope": "local",
          "Driver": "bridge",
          "EnableIPv6": false,
          "IPAM": {
              "Driver": "default",
              "Options": null,
              "Config": [
                  {
                      "Subnet": "172.21.0.0/16",
                      "Gateway": "172.21.0.1"
                  }
              ]
          },
          "Internal": false,
          "Attachable": true,
          "Ingress": false,
          "ConfigFrom": {
              "Network": ""
          },
          "ConfigOnly": false,
          "Containers": {
              "4c74c231d03483793bf113544e712befe8a6eba7a52eb430d4231dc2f50d598f": {
                  "Name": "web_db_1",
                  "EndpointID": "e72ad329b7814df8dbd8eb0ade7e6335508d17546bdb9d23065e3a380c0e3b23",
                  "MacAddress": "02:42:ac:15:00:04",
                  "IPv4Address": "172.21.0.4/16",
                  "IPv6Address": ""
              },
              "c0b1d933dae330fb9cd06ce5636338750c1dcb6c1d77d62535aaec0933f24c0e": {
                  "Name": "nginx",
                  "EndpointID": "0fd231bdc4349f39129bc29bfa065356fad302b4564ce5ebd12c3e6c9342259e",
                  "MacAddress": "02:42:ac:15:00:06",
                  "IPv4Address": "172.21.0.6/16",
                  "IPv6Address": ""
              },
              "c3835352577257841cec782fa6aed5a3dbcba5da9b02167338f7dfc0836e2cea": {
                  "Name": "web_internal-owncloud_1",
                  "EndpointID": "fcf5241f793a3de30bcfff51ebcc9075f66322d7e13320baf7754d464a3f676d",
                  "MacAddress": "02:42:ac:15:00:05",
                  "IPv4Address": "172.21.0.5/16",
                  "IPv6Address": ""
              },
              "c974cec76348654f93f77e76ab109287dde3049a9508204f0ad9f3c7906b6d2d": {
                  "Name": "django",
                  "EndpointID": "86500ce8e5db41b4a6f527d39b0c578ff5fccd9cbfd4382815d73271e1b05ecb",
                  "MacAddress": "02:42:ac:15:00:02",
                  "IPv4Address": "172.21.0.2/16",
                  "IPv6Address": ""
              },
              "eeeaed65ec89283d52ebfafb5d3770e731a8490c44217041d3d407c41a8707d4": {
                  "Name": "web_redis_1",
                  "EndpointID": "275754d8bd4d7a12d86280cd6812ad7a461079913aee185ba6d67b16815e1205",
                  "MacAddress": "02:42:ac:15:00:03",
                  "IPv4Address": "172.21.0.3/16",
                  "IPv6Address": ""
              }
          },
          "Options": {},
          "Labels": {
              "com.docker.compose.network": "nginx-net",
              "com.docker.compose.project": "web",
              "com.docker.compose.version": "1.23.2"
          }
      }
  ]
  </code></pre>