在nginx代理后组织主服务器Docker容器

scyqe7ek  于 2023-03-01  发布在  Nginx
关注(0)|答案(2)|浏览(546)

我有一个主服务器,主要用于自托管的应用程序(notes,git server,Jenkins server,等等),我想组织这个主服务器,这样我就不必记住每个容器的所有端口。

我希望根端点路由到一个运行google的cadvisor的容器,以监视我所有容器的状态,/jenkins路由到Jenkins,/pihole路由到pi-hole,等等。
下面是我如何根据我对配置模式的理解来设置我的docker-compose和nginx.conf文件:
docker-compose.yml:

version: '3'
services:
    webserver:
        image: nginx:alpine
        container_name: webserver
        restart: unless-stopped
        tty: true
        ports:
            - "80:80"
            - "443:443"
        volumes:
            - ./:/var/www
            - ./nginx/conf.d/:/etc/nginx/conf.d/
        restart: unless-stopped

    cadvisor:
        image: 'google/cadvisor'
        volumes:
            - /:/rootfs:ro
            - /var/run:/var/run:ro
            - /sys:/sys:ro
            - /var/lib/docker/:/var/lib/docker:ro
            - /dev/disk/:/dev/disk:ro
        ports:
            - "8090:8090"

    pi-hole:
        container_name: pihole
        image: pihole/pihole:latest
        ports:
            - "53:53/tcp"
            - "53:53/udp"
            - "67:67/udp"
            - "8070:8070/tcp"
            - "4437:4437/tcp"
        environment:
            TZ: 'America/Chicago'
            # WEBPASSWORD: 'set a secure password here or it will be random'
        # Volumes store your data between container upgrades
        volumes:
            - './etc-pihole/:/etc/pihole/'
            - './etc-dnsmasq.d/:/etc/dnsmasq.d/'
        dns:
          - 127.0.0.1
          - 1.1.1.1
        # Recommended but not required (DHCP needs NET_ADMIN)
        #   https://github.com/pi-hole/docker-pi-hole#note-on-capabilities
        cap_add:
            - NET_ADMIN
        restart: unless-stopped

    cowyo:
        image: 'schollz/cowyo'
        ports:
            - "8050:8050"
        volumes:
            - './cowyo/data/:/data/'
        restart: unless-stopped

    jenkins:
        image: 'bitnami/jenkins:2'
        ports:
            - '8080:8080'
            - '8443:8443'
            - '50000:50000'
        volumes:
            - 'jenkins_data:/bitnami'
        restart: unless-stopped

volumes:
    jenkins_data:
        driver: local

nginx.conf:

worker_processes 1;

events { worker_connections 1024; }

http {

    sendfile on;

    upstream docker-cadvisor {
        server cadvisor:8090;
    }

    upstream docker-pihole {
        server pi-hole:8070;
    }

    upstream docker-cowyo {
        server cowyo:8050;
    }

    upstream docker-jenkins {
        server jenkins:8080;
    }

    server {
        listen 80;

        location / {
            proxy_pass         http://docker-cadvisor;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }
    }

    server {
        listen 80;

        location /pihole {
            proxy_pass         http://docker-pihole;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }
    }

    server {
        listen 80;

        location /cowyo {
            proxy_pass         http://docker-cowyo;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }
    }

    server {
        listen 80;

        location /jenkins {
            proxy_pass         http://docker-jenkins;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }
    }

}

你知道我做错了什么吗?任何帮助都将不胜感激。
编辑:它现在运行方式的问题是,当我去上面的一个网址,我得到一个“这个网站不能到达”的错误。
Edit 2:上面的问题是由一个坏的nginx.conf文件引起的,该文件导致Docker容器无法正常启动。解决该问题后,我得到一个“502坏网关”。

kzmpq1sx

kzmpq1sx1#

你得到的错误是什么?Tre下面的配置,如果你没有得到预期的结果,然后查看相应的错误日志.

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

   upstream docker-cadvisor {
        server cadvisor:8090;
    }

    upstream docker-pihole {
        server pi-hole:8070;
    }

    upstream docker-cowyo {
        server cowyo:8050;
    }

    upstream docker-jenkins {
        server jenkins:8080;
    }

server {
   listen 80;
   server_name home.server;
   fastcgi_param home.server $host;

   location / {
       proxy_set_header X-Forwarded-Host $host:$server_port;
       proxy_set_header X-Forwarded-Server $host;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header Host $host;
       proxy_connect_timeout      300;
       proxy_send_timeout         300;
       proxy_read_timeout         300;
       proxy_pass         http://docker-cadvisor;
   }
           access_log /tmp/cadvisor-access.log main;
           error_log /tmp/cadvisor-error.log error;
}
server {
   listen 80;
   server_name home.server;
   fastcgi_param home.server $host;

   location /jenkins {
       proxy_set_header X-Forwarded-Host $host:$server_port;
       proxy_set_header X-Forwarded-Server $host;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header Host $host;
       proxy_connect_timeout      300;
       proxy_send_timeout         300;
       proxy_read_timeout         300;
       proxy_pass         http://docker-jenkins;
   }
           access_log /tmp/jenkins-access.log main;
           error_log /tmp/jenkins-error.log error;
}
server {
   listen 80;
   server_name home.server;
   fastcgi_param home.server $host;

   location /cowyo {
       proxy_set_header X-Forwarded-Host $host:$server_port;
       proxy_set_header X-Forwarded-Server $host;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header Host $host;
       proxy_connect_timeout      300;
       proxy_send_timeout         300;
       proxy_read_timeout         300;
       proxy_pass         http://docker-cowyo;
   }
           access_log /tmp/cowyo-access.log main;
           error_log /tmp/cowyo-error.log error;
}
server {
   listen 80;
   server_name home.server;
   fastcgi_param home.server $host;

   location /pihole {
       proxy_set_header X-Forwarded-Host $host:$server_port;
       proxy_set_header X-Forwarded-Server $host;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header Host $host;
       proxy_connect_timeout      300;
       proxy_send_timeout         300;
       proxy_read_timeout         300;
       proxy_pass         http://docker-pihole;
   }
           access_log /tmp/pihole-access.log main;
           error_log /tmp/pihole-error.log error;
}
}
thtygnil

thtygnil2#

我觉得你应该换个

upstream docker-cadvisor {
    server 127.0.0.1:8090;
}

upstream docker-pihole {
    server 127.0.0.1:8070;
}

upstream docker-cowyo {
    server 127.0.0.1:8050;
}

upstream docker-jenkins {
    server 127.0.0.1:8080;
}

相关问题