docker、nginx配置?

  • 回答 (3)
  • 关注 (0)
  • 查看 (531)

我有一个nginx配置的问题,想咨询一下大佬,我有一个CVM,如果IP为1.1.1.1,我在这个CVM上部署了三个docker容器A、B、C:A容器的基础镜像是nginx,暴露端口为80,B、C容器的基础镜像是nodejs,暴露端口为别是81、82。nginx如何配置,才能实现在访问1.1.1.1/a时访问1.1.1.1:81,1.1.1.1/b时访问1.1.1.1:82 ?

用户1224254用户1224254提问于
骑牛看晨曦love&peace~回答于

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

翻简书翻到的 希望对你有用~~~

Hi胡瀚

厦门市软云网络科技有限公司 · 运维工程师 (已认证)

PHPer,linux运维回答于

需要使用nginx的代理功能。针对不同的请求路径分别代理设置。这边给你推荐个博客http://blog.csdn.net/physicsdandan/article/details/45667357

可能回答问题的人

  • 腾讯云计算产品团队

    腾讯云 · 产品团队 (已认证)

    136 粉丝0 提问0 回答
  • CVM 产品团队

    24 粉丝0 提问7 回答
  • 小仙女和科学家

    9 粉丝0 提问0 回答
  • 怕冷的阳阳

    腾讯云 · 高级工程师 (已认证)

    18 粉丝1 提问0 回答
  • DRRR

    腾讯云 · 产品经理 (已认证)

    6 粉丝0 提问4 回答
  • candyxiao

    腾讯 · 高级产品经理 (已认证)

    10 粉丝0 提问7 回答

扫码关注云+社区

领取腾讯云代金券