美文网首页nginx Unix/Linux服务器技术分享
docker容器Nginx配置upstream实现负载均衡

docker容器Nginx配置upstream实现负载均衡

作者: yaokui | 来源:发表于2016-01-20 15:39 被阅读2058次

    github

    目录

    ├── baseimg                                   基础镜像
    │   └── Dockerfile
    ├── docker-compose                            docker-compose
    │   ├── docker-compose.yml
    │   ├── nginx-php-sites-available
    │   │   └── default
    │   └── nginx-sites-available
    │       └── default
    ├── nginx                                      nginx镜像
    │   ├── Dockerfile
    │   ├── sites-enabled
    │   └── supervisor_nginx.conf
    ├── nginx-php                                  nginx+php-fpm镜像
    │   ├── Dockerfile
    │   ├── sites-enabled
    │   ├── supervisor_nginx.conf
    │   └── supervisor_php5-fpm.conf
    
    

    baseimg基础镜像

    Dockerfile

    FROM debian
    
    MAINTAINER huangyaokui <451722619@qq.com>
    
    ENV TERM xterm
    ENV TZ "Asia/Shanghai"
    
    RUN apt-get update && \
        apt-get install -qqy vim supervisor && \
        rm -rf /var/lib/apt/lists/*
    
    EXPOSE  22
    
    ENTRYPOINT ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisor/supervisord.conf"]  
    

    docker build -t='debian/baseimg:0.1' .


    nginx镜像

    FROM debian/baseimg:0.1
    
    MAINTAINER kui <451722619@qq.com>
    
    RUN apt-get update && \
     apt-get install -y nginx && \
     rm -rf /var/lib/apt/lists/*
    
    EXPOSE 80 443
    
    ADD supervisor_nginx.conf /etc/supervisor/conf.d/nginx.conf
    
    VOLUME ["/var/www/html", "/etc/nginx/sites-enabled"]
    

    docker build -t='debian/nginx:0.1' .


    nginx+php-fpm镜像

    FROM debian/nginx:0.1
    
    MAINTAINER kui <451722619@qq.com>
    
    RUN apt-get update && \
     apt-get install -y php5 php5-fpm php5-memcache php5-mysql && \
     rm -rf /var/lib/apt/lists/* 
    
    ADD supervisor_php5-fpm.conf /etc/supervisor/conf.d/php5-fpm.conf
    
    VOLUME ["/etc/php5/fpm/pool.d"]
    

    docker build -t='debian/nginx-php:0.1' .


    docker-compose

    web1:
        image: debian/nginx-php:0.1
        volumes:
            - /var/www/html:/var/www/html
            - /home/vagrant/cjw-Docker/docker-compose/nginx-php-sites-available:/etc/nginx/sites-enabled
    web2:
        image: debian/nginx-php:0.1
        volumes:
            - /var/www/html:/var/www/html
            - /home/vagrant/cjw-Docker/docker-compose/nginx-php-sites-available:/etc/nginx/sites-enabled
    nginx:
        image: debian/nginx:0.1
        ports:
            - "80:80"
        links:
            - web1:web1
            - web2:web2
        volumes:
            - /home/vagrant/cjw-Docker/docker-compose/nginx-sites-available:/etc/nginx/sites-enabled
    

    nginx容器的upstream

    upstream web{
        server web1;
        server web2;
    }
    
    server {
        listen 80 default_server;
        listen [::]:80 default_server;
    
        root /var/www/html;
    
        # Add index.php to the list if you are using PHP
        index index.html index.htm index.php index.nginx-debian.html;
    
        server_name _;
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            proxy_pass http://web; 
        }
    }
    

    nginx-php的nginx配置

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
    
        root /var/www/html;
        index index.html index.htm index.php index.nginx-debian.html;
    
        server_name _;
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
        #
        #   # With php5-cgi alone:
        #   fastcgi_pass 127.0.0.1:9000;
        #   # With php5-fpm:
            fastcgi_pass unix:/var/run/php5-fpm.sock;
        }
    }
    

    启动容器

    docker-compose up -d
    

    验证

    #/var/www/html/index.php
    <?php
    echo $_SERVER['SERVER_ADDR'];
    

    访问 localhost/index.php
    简单的nginx的负载均衡

    缺点

    nginx的upstream实现的负载均衡,容易出现单点故障。

    相关文章

      网友评论

        本文标题:docker容器Nginx配置upstream实现负载均衡

        本文链接:https://www.haomeiwen.com/subject/krrlkttx.html