Контейнер nginx docker в качестве обратного прокси для служб, работающих в контейнерах docker - PullRequest
0 голосов
/ 27 апреля 2020

У меня nginx работает в docker контейнере, который я хочу использовать в качестве обратного прокси для сервисов, работающих в docker контейнерах. У меня есть две службы: django и owncloud.

Обе службы работают при обращении к ним, например, localhost:4000 и localhost:5000.

Когда я получаю доступ к localhost/owncloud, я получаю 502 Bad Gateway
Nginx log:
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"

Когда я получаю доступ к localhost/django, я получаю page not found от Django, что хорошо, но я, если я пытаюсь для доступа localhost/django/admin я получаю page not found из Nginx.
Nginx log:
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"

Я новичок в Nginx и перепутался с nginx.conf много без успеха. Вот соответствующие файлы:

/docker-compose.yml

version: '3'
networks:
  nginx-net:

/nginx/docker-compose.yml

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

/nginx/Dockerfile

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

/nginx/nginx.conf

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;
        }
    }
}

/owncloud/docker-compose.yml

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

.env

OWNCLOUD_VERSION=10.4
OWNCLOUD_DOMAIN=localhost
ADMIN_USERNAME=admin
ADMIN_PASSWORD=admin
HTTP_PORT=4000

/owncloud/Dockerfile

FROM owncloud/server
EXPOSE 8080

/django/docker-compose.yml

version: '3'
services:
  internal-django:
    build:
      context: ./django
    ports:
    - "5000:5000"
    restart: unless-stopped
    container_name: django
    image: web-django
    networks: 
      - nginx-net

/django/Dockerfile

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

РЕДАКТ. 1
Бег docker network inspect web_nginx-net

[
    {
        "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"
        }
    }
]

1 Ответ

0 голосов
/ 27 апреля 2020

Вам понадобится несколько изменений, попробуйте выполнить следующие шаги: -

В вашем внутреннем облаке docker -составьте файл, добавьте строку container_name: owncloud Например:

internal-owncloud:
image: owncloud/server:${OWNCLOUD_VERSION}
restart: unless-stopped
container_name: owncloud #add this line
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

В вашем файле nginx удалите строки перезаписи:

    location /django/ {
        proxy_pass http://django:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        #rewrite ^/django(.*)$ $1 break; //remove this
    }
    location /owncloud/ {
        proxy_pass http://owncloud:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        #rewrite ^/owncloud(/.*)$ $1 break; //remove this
    }

Дайте мне знать, если это работает для вас.

...