nginx docker容器作为运行在docker容器中的服务的反向代理。

问题描述 投票:0回答:1

我有一个nginx运行在docker容器中,我想把它作为docker容器中服务的反向代理。目前我有两个服务,django和owncloud。

这两个服务在访问它们的目录时都能正常工作,如 localhost:4000localhost:5000.

当我进入 localhost/owncloud 我得到 502 Bad Gateway Nginx日志。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,这是很好的,但如果我尝试访问Django的 localhost/django/admin 我得到 page not found 来自Nginx。Nginx日志。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上乱搞了一番。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

EDIT 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"
        }
    }
]
django docker nginx nginx-reverse-proxy owncloud
1个回答
0
投票

你将需要多个变化,尝试使用以下步骤:-。

在你的Internal-cloud的docker-compose文件中,添加行。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
    }

如果这对你有效,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.