Docker-compose构建nginx反向代理配置详解

一、compose简介

compose项目来源于之前的fig项目,使用python语言编写。compose项目主要用于编排部署docker的应用,本身与docker/swarm配合度很高

docker compose是docker编排服务的一部分,可以让用户在其他平台上快速的安装docker,dockercompose属于一个应用层的服务,用户可以定义哪个容器组运行哪个应用,它支持动态改变应用。

dockerfile可以让用户管理一个单独的容器,而compose则允许用户在一个模版yaml格式中定义一个相关联的容器,例如一个调度器,两个web服务器等。


一、我们先做一个基础的环境,用dockerfile做一个apache镜像。

以下是Apache的dockerfile。

[plain]  view plain  copy
  1. #FROM docker.io/centos:centos6  
  2. MAINTAINER [email protected]  
  3. RUN yum install -y httpd net-tools iputils  
  4. RUN sed 's/#ServerName www.example.com:80/ServerName www.benet.com/g' /etc/httpd/conf/httpd.conf  
  5. EXPOSE 80  
  6. CMD ["/usr/sbin/httpd","-DFOREGROUND"]  

1.编写完成之后运行dockerfile

[plain]  view plain  copy
  1. # docker build -t centos:http .  
  2. Sending build context to Docker daemon 2.048 kB  
  3. Step 1 : FROM docker.io/centos:centos6  
  4. Trying to pull repository docker.io/library/centos ...   
  5. centos6: Pulling from docker.io/library/centos  
  6.   
  7. b26de5a391ad: Downloading [=========================>                 ] 2.701 MB/70.04 MB  

2.运行完成之后查看是否有http的镜像

[plain]  view plain  copy
  1. # docker images   
  2. REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE  
  3. centos              http                33e4acf38103        24 seconds ago      302 MB  
  4. docker.io/centos    centos6             5dedbd63518e        9 days ago          194.3 MB  

3.编写nginx-dockerfile,以下的nginx的dockerfile

扫描二维码关注公众号,回复: 876830 查看本文章
[plain]  view plain  copy
  1. #images of nginx  
  2. FROM docker.io/centos:centos6  
  3. RUN yum install -y pcre-devel wget net-tools gcc zlib zlib-devel make openssl-devel  
  4. RUN useradd -M -s /sbin/nologin nginx  
  5. ADD http://nginx.org/download/nginx-1.6.2.tar.gz .  
  6. RUN tar zxvf nginx-1.6.2.tar.gz  
  7. RUN mkdir -p /usr/local/nginx  
  8. RUN cd nginx-1.6.2 && ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install  
  9. RUN ln -s /usr/local/nginx/sbin/* /usr/local/sbin/  
  10. copy nginx.conf /usr/local/nginx/conf/nginx.conf  
  11. expose 80  
  12. CMD ["nginx"]  

4.注意以上红色字段,我们需要拷贝一份nginx在当前目录。以下是nginx主配置内容

[plain]  view plain  copy
  1. # ls  
  2. dockerfile  nginx.conf  
  3. vim nginx.conf  
  4. #user  nobody;  
  5. worker_processes  1;  
  6.   
  7. #error_log  logs/error.log;  
  8. #error_log  logs/error.log  notice;  
  9. #error_log  logs/error.log  info;  
  10.   
  11. #pid        logs/nginx.pid;  
  12.   
  13.   
  14. events {  
  15.     worker_connections  1024;  
  16. }  
  17.   
  18.   
  19. http {  
  20.     include       mime.types;  
  21.     default_type  application/octet-stream;  
  22.   
  23.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  24.     #                  '$status $body_bytes_sent "$http_referer" '  
  25.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  26.   
  27.     #access_log  logs/access.log  main;  
  28.   
  29.     sendfile        on;  
  30.     #tcp_nopush     on;  
  31.   
  32.     #keepalive_timeout  0;  
  33.     keepalive_timeout  65;  
  34.   
  35.     #gzip  on;  
  36. upstream yankerp{  
  37.            server Apache1:80 weight=1;  
  38.            server Apache2:80 weight=1;  
  39.            server Apache3:80 weight=1;  
  40.     }  
  41.     server {  
  42.         listen       80;  
  43.         server_name  localhost;  
  44.   
  45.         #charset koi8-r;  
  46.   
  47.         #access_log  logs/host.access.log  main;  
  48.   
  49.         location / {  
  50.             root   html;  
  51.             index  index.html index.htm;  
  52.         proxy_pass http://yankerp;  
  53.         }  
  54.   
  55.         #error_page  404              /404.html;  
  56.   
  57.         # redirect server error pages to the static page /50x.html  
  58.         #  
  59.         error_page   500 502 503 504  /50x.html;  
  60.         location = /50x.html {  
  61.             root   html;  
  62.         }  
  63.   
  64.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  65.         #  
  66.         #location ~ \.php$ {  
  67.         #    proxy_pass   http://127.0.0.1;  
  68.         #}  
  69.   
  70.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  71.         #  
  72.         #location ~ \.php$ {  
  73.         #    root           html;  
  74.         #    fastcgi_pass   127.0.0.1:9000;  
  75.         #    fastcgi_index  index.php;  
  76.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  77.         #    include        fastcgi_params;  
  78.         #}  
  79.   
  80.         # deny access to .htaccess files, if Apache's document root  
  81.         # concurs with nginx's one  
  82.         #  
  83.         #location ~ /\.ht {  
  84.         #    deny  all;  
  85.         #}  
  86.     }  
  87.   
  88.   
  89.     # another virtual host using mix of IP-, name-, and port-based configuration  
  90.     #  
  91.     #server {  
  92.     #    listen       8000;  
  93.     #    listen       somename:8080;  
  94.     #    server_name  somename  alias  another.alias;  
  95.   
  96.     #    location / {  
  97.     #        root   html;  
  98.     #        index  index.html index.htm;  
  99.     #    }  
  100.     #}  
  101.   
  102.   
  103.     # HTTPS server  
  104.     #  
  105.     #server {  
  106.     #    listen       443 ssl;  
  107.     #    server_name  localhost;  
  108.   
  109.     #    ssl_certificate      cert.pem;  
  110.     #    ssl_certificate_key  cert.key;  
  111.   
  112.     #    ssl_session_cache    shared:SSL:1m;  
  113.     #    ssl_session_timeout  5m;  
  114.   
  115.     #    ssl_ciphers  HIGH:!aNULL:!MD5;  
  116.     #    ssl_prefer_server_ciphers  on;  
  117.   
  118.     #    location / {  
  119.     #        root   html;  
  120.     #        index  index.html index.htm;  
  121.     #    }  
  122.     #}  
  123.   
  124. }  
  125. daemon off;  

5.随后运行nginx-dockerfile 注意那个后面的小点,代表当前位置。

[plain]  view plain  copy
  1. # docker build -t centos:nginx .  
  2. Sending build context to Docker daemon 6.144 kB  
  3. Step 1 : FROM docker.io/centos:centos6  
  4.  ---> 5dedbd63518e  
  5. Step 2 : RUN yum install -y pcre-devel wget net-tools gcc zlib zlib-devel make openssl-devel  
  6.  ---> Running in 4c25bd1ebd47  
  7. Loaded plugins: fastestmirror, ovl  
  8. Setting up Install Process  

运行完之后我们查看是否有nginx镜像

[plain]  view plain  copy
  1. # docker images   
  2. REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE  
  3. centos              nginx               155c0c66b4fc        41 seconds ago      316.4 MB  
  4. centos              http                33e4acf38103        19 minutes ago      302 MB  
  5. docker.io/centos    centos6             5dedbd63518e        9 days ago          194.3 MB  
二、安装compose,在安装docker-compose时,先安装pip,pip就相当于redhat里面的yum

以下是安装的过程

[plain]  view plain  copy
  1. # wget https://bootstrap.pypa.io/get-pip.py  
  2. --2017-09-23 14:56:25--  https://bootstrap.pypa.io/get-pip.py  
  3. 正在解析主机 bootstrap.pypa.io (bootstrap.pypa.io)... 151.101.76.175  
  4. 正在连接 bootstrap.pypa.io (bootstrap.pypa.io)|151.101.76.175|:443... 已连接。  
  5. 已发出 HTTP 请求,正在等待回应... 200 OK  
  6. 长度:1595408 (1.5M) [text/x-python]  
  7. 正在保存至: “get-pip.py”  
  8.   
  9. 29% [=================================>                             ] 477,486     29.8KB/s 剩余 45s  

python get-pip.py

[plain]  view plain  copy
  1. # python get-pip.py   
  2. Collecting pip  
  3.   Downloading pip-9.0.1-py2.py3-none-any.whl (1.3MB)  
  4.     100% |████████████████████████████████| 1.3MB 43kB/s   
  5. Collecting wheel  
  6.   Downloading wheel-0.30.0-py2.py3-none-any.whl (49kB)  
  7.     100% |████████████████████████████████| 51kB 21kB/s   
  8. Installing collected packages: pip, wheel  
  9. Successfully installed pip-9.0.1 wheel-0.30.0  

安装完成时候在安装compose

[plain]  view plain  copy
  1. # pip install docker-compose  
  2. Collecting docker-compose  
  3.   Downloading docker_compose-1.16.1-py2.py3-none-any.whl (105kB)  
  4.     100% |████████████████████████████████| 112kB 14kB/s   
  5. Collecting texttable<0.10,>=0.9.0 (from docker-compose)  
  6.   Downloading texttable-0.9.1.tar.gz  
  7. Collecting backports.ssl-match-hostname>=3.5; python_version < "3.5" (from docker-compose)  
  8.   Downloading backports.ssl_match_hostname-3.5.0.1.tar.gz  
  9. Collecting docker<3.0,>=2.5.1 (from docker-compose)  
  10.   Downloading docker-2.5.1-py2.py3-none-any.whl (111kB)  
  11.     100% |████████████████████████████████| 112kB 29kB/s   
  12. Collecting jsonschema<3,>=2.5.1 (from docker-compose)  
  13.   Downloading jsonschema-2.6.0-py2.py3-none-any.whl  
  14. Collecting cached-property<2,>=1.2.0 (from docker-compose)  
  15.   Downloading cached_property-1.3.1-py2.py3-none-any.whl  
  16. Requirement already satisfied: six<2,>=1.3.0 in /usr/lib/python2.7/site-packages (from docker-compose)  
  17. Collecting ipaddress>=1.0.16; python_version < "3.3" (from docker-compose)  
  18.   Downloading ipaddress-1.0.18-py2-none-any.whl  
  19. Collecting enum34<2,>=1.0.4; python_version < "3.4" (from docker-compose)  
  20.   Downloading enum34-1.1.6-py2-none-any.whl  
  21. Collecting websocket-client<1.0,>=0.32.0 (from docker-compose)  
  22.   Downloading websocket_client-0.44.0-py2.py3-none-any.whl (199kB)  
  23.     100% |████████████████████████████████| 204kB 29kB/s   
  24. Collecting PyYAML<4,>=3.10 (from docker-compose)  
  25.   Downloading PyYAML-3.12.tar.gz (253kB)  
  26.     100% |████████████████████████████████| 256kB 23kB/s   
  27. Collecting dockerpty<0.5,>=0.4.1 (from docker-compose)  
  28.   Downloading dockerpty-0.4.1.tar.gz  
  29. Collecting requests!=2.11.0,<2.12,>=2.6.1 (from docker-compose)  
  30.   Downloading requests-2.11.1-py2.py3-none-any.whl (514kB)  
  31.     100% |████████████████████████████████| 522kB 24kB/s   
  32. Collecting docopt<0.7,>=0.6.1 (from docker-compose)  
  33.   Downloading docopt-0.6.2.tar.gz  
  34. Collecting docker-pycreds>=0.2.1 (from docker<3.0,>=2.5.1->docker-compose)  
  35.   Downloading docker_pycreds-0.2.1-py2.py3-none-any.whl  
  36. Collecting functools32; python_version == "2.7" (from jsonschema<3,>=2.5.1->docker-compose)  
  37.   Downloading functools32-3.2.3-2.zip  
  38. Building wheels for collected packages: texttable, backports.ssl-match-hostname, PyYAML, dockerpty, docopt, functools32  
  39.   Running setup.py bdist_wheel for texttable ... done  
  40.   Stored in directory: /root/.cache/pip/wheels/9e/53/a3/e452dc385103ee8e1f0df7b3457974b580d52ce662ee5a16cc  
  41.   Running setup.py bdist_wheel for backports.ssl-match-hostname ... done  
  42.   Stored in directory: /root/.cache/pip/wheels/5d/72/36/b2a31507b613967b728edc33378a5ff2ada0f62855b93c5ae1  
  43.   Running setup.py bdist_wheel for PyYAML ... done  
  44.   Stored in directory: /root/.cache/pip/wheels/2c/f7/79/13f3a12cd723892437c0cfbde1230ab4d82947ff7b3839a4fc  
  45.   Running setup.py bdist_wheel for dockerpty ... done  
  46.   Stored in directory: /root/.cache/pip/wheels/ae/d5/14/a25cbb003bd70ffefba0fdfbd5a5c4ea4d2a11bde7736f7482  
  47.   Running setup.py bdist_wheel for docopt ... done  
  48.   Stored in directory: /root/.cache/pip/wheels/b2/16/5f/c33a2bb5f2dce71205f8e65cbfd05647d79d441282be31fd82  
  49.   Running setup.py bdist_wheel for functools32 ... done  
  50.   Stored in directory: /root/.cache/pip/wheels/3c/d0/09/cd78d0ff4d6cfecfbd730782a7815a4571cd2cd4d2ed6e69d9  
  51. Successfully built texttable backports.ssl-match-hostname PyYAML dockerpty docopt functools32  
  52. Installing collected packages: texttable, backports.ssl-match-hostname, websocket-client, ipaddress, docker-pycreds, requests, docker, functools32, jsonschema, cached-property, enum34, PyYAML, dockerpty, docopt, docker-compose  
  53.   Found existing installation: backports.ssl-match-hostname 3.4.0.2  
  54.     Uninstalling backports.ssl-match-hostname-3.4.0.2:  
  55.       Successfully uninstalled backports.ssl-match-hostname-3.4.0.2  
  56. Successfully installed PyYAML-3.12 backports.ssl-match-hostname-3.5.0.1 cached-property-1.3.1 docke  
[plain]  view plain  copy
  1. # ln -s /usr/bin/docker-compose /usr/local/bin/  

接下来介绍几个术语

[plain]  view plain  copy
  1. #build  
  2. Usage: build [options] [SERVICE...]  
  3.   
  4.   
  5. Options:  
  6. --force-rm  Always remove intermediate containers.  
  7. --no-cache  Do not use cache when building the image.  
  8. --pull      Always attempt to pull a newer version of the image.  
  9. 当修改dockerfile或者docker-compose时,运行docker-compose build 重建镜像。  生成镜像后,可使用docker-compose up启动  
  10.   
  11.   
  12. config  
  13. Usage: config [options]  
  14.   
  15.   
  16. Options:  
  17. -q, --quiet     只验证配置,不输出。 当配置正确时,不输出任何内容,当文件配置错误,输出错误信息。  
  18. --services      打印服务名,一行一个  
  19. 验证和查看compose文件配置。  
  20. create  
  21. 为服务创建容器.只是单纯的create,还需要使用start启动compose  
  22.   
  23.   
  24. Usage: create [options] [SERVICE...]  
  25.   
  26.   
  27. Options:  
  28.     --force-recreate       重新创建容器,即使他的配置和镜像没有改变,不兼容--no-recreate参数  
  29.     --no-recreate          如果容器已经存在,不需要重新创建. 不兼容--force-recreate参数  
  30.     --no-build             不创建镜像,即使缺失.  
  31.     --build                创建容器前,生成镜像.  
  32. down  
  33. Usage: down [options]  
  34.   
  35.   
  36. Options:  
  37.     --rmi type          删除镜像,类型必须是:  
  38.                         'all': 删除compose文件中定义的所以镜像.  
  39.                         'local': 删除镜像名为空的镜像  
  40.      -v, --volumes       删除卷  
  41.                         attached to containers.  
  42.     --remove-orphans    Remove containers for services not defined in the  
  43.                         Compose file  
  44. 停止和删除容器、网络、卷、镜像,这些内容是通过docker-compose up命令创建的.  默认值删除 容器 网络,可以通过指定 rmi volumes参数删除镜像和卷  
  45.   
  46.   
  47. events  
  48. Usage: events [options] [SERVICE...]  
  49.   
  50.   
  51. Options:  
  52.     --json      输出事件日志,json格式  
  53. 输出docker-compose 事件的日志,当执行docker-compose命令操作时,docker-compose even命令就会监控日志:  
  54. {  
  55.     "service": "web",  
  56.     "event": "create",  
  57.     "container": "213cf75fc39a",  
  58.     "image": "alpine:edge",  
  59.     "time": "2015-11-20T18:01:03.615550",  
  60. }  
  61. exec  
  62. Usage: exec [options] SERVICE COMMAND [ARGS...]  
  63.   
  64.   
  65. Options:  
  66. -d                分离模式,后台运行命令.  
  67. --privileged      获取特权.  
  68. --user USER       指定运行的用户.  
  69. -T                禁用分配TTY. By default `docker-compose exec`  
  70.                   分配 a TTY.  
  71. --index=index     当一个服务拥有多个容器时,可通过该参数登陆到该服务下的任何服务,例如:docker-compose exec --index=1 web /bin/bash ,web服务中包含多个容器  
  72.                   instances of a service [default: 1]  
  73. 和docker exec命令功能相同,可以通过service name登陆到容器中  
  74. e.g. docker-compose exec web sh   
  75. kill  
  76. Usage: kill [options] [SERVICE...]  
  77.   
  78.   
  79. Options:  
  80. -s SIGNAL         向容器发送信号. 默认是SIGKILL.  
  81. 通过发送 SIGKILL 信号来强制停止服务容器。支持通过参数来指定发送的信号:  
  82. $ docker-compose kill -s SIGINT  
  83. logs  
  84. Usage: logs [options] [SERVICE...]  
  85.   
  86.   
  87. Options:  
  88. --no-color          单色输出,不显示其他颜.  
  89. -f, --follow        跟踪日志输出,就是可以实时查看日志  
  90. -t, --timestamps    显示时间戳  
  91. --tail              从日志的结尾显示,--tail=200  
  92. 显示日志输出.  
  93. pause  
  94. Usage: pause [SERVICE...]  
  95. 暂停容器服务. docker-compose pause  暂停所有服务. docker-compose pause web,之后暂停web服务的容器。  
  96. unpause  
  97. Usage: unpause [SERVICE...]  
  98. 恢复容器服务. docker-compose unpause  恢复所有服务. docker-compose unpause web,之后恢复web服务的容器。  
  99. port  
  100. Usage: port [options] SERVICE PRIVATE_PORT  
  101.   
  102.   
  103. Options:  
  104. --protocol=proto  tcp or udp [default: tcp]  
  105. --index=index     index of the container if there are multiple  
  106.                   instances of a service [default: 1]  
  107. 输出服务的共有端口.  
  108. # docker-compose port web 8080   -- 8080为容器内部端口  
  109. 0.0.0.0:8884  
  110. ps  
  111. Usage: ps [options] [SERVICE...]  
  112.   
  113.   
  114. Options:  
  115. -q    只显示ID  
  116. 显示容器. 默认显示name、command、state、ports  
  117. pull  
  118. Usage: pull [options] [SERVICE...]  
  119.   
  120.   
  121. Options:  
  122. --ignore-pull-failures  忽略pull失败的镜像,继续pull其他镜像.  
  123. pull compose文件中所指明的镜像.  
  124. push  
  125. Usage: push [options] [SERVICE...]  
  126.   
  127.   
  128. Options:  
  129.     --ignore-push-failures  忽略错误.  
  130. push compose文件中所指明的镜像  
  131. restart  
  132. Usage: restart [options] [SERVICE...]  
  133.   
  134.   
  135. Options:  
  136. -t, --timeout TIMEOUT      Specify a shutdown timeout in seconds. (default: 10)  
  137. Restarts services.  
  138. rm  
  139. Usage: rm [options] [SERVICE...]  
  140.   
  141.   
  142. Options:  
  143.     -f, --force   Don't ask to confirm removal  
  144.     -v            期初加载到容器的任何匿名卷  
  145.     -a, --all     Also remove one-off containers created by  
  146.                   docker-compose run  
  147. Removes stopped service containers. 如果服务在运行,需要先docker-compose stop 停止容器  
  148. By default, anonymous volumes attached to containers will not be removed. You can override this with -v. To list all volumes, use docker volume ls.  
  149. Any data which is not in a volume will be lost.  
  150. run  
  151. Usage: run [options] [-e KEY=VAL...] SERVICE [COMMAND] [ARGS...]  
  152.   
  153.   
  154. Options:  
  155.  -d                   后台运行,输出容器名.  
  156. -e KEY=VAL            设置环境变量参数,可以使用多次  
  157. -u, --user=""         指定运行的用户  
  158. --no-deps             不启动link服务,只启动run的服务.  
  159. --rm                  运行后删除容器,后台运行模式除外(-d).  
  160. -p, --publish=[]      开放端口  
  161. --service-ports       compose文件中配置什么端口,就映射什么端口.  
  162. -T                    禁用TTY.  
  163. -w, --workdir=""      设置工作目录  
  164. 启动web服务器,并执行bash命令.  
  165. $ docker-compose run web bash  
  166. 根据compose配置文件制定的端口,映射到主机:  
  167. $ docker-compose run --service-ports web python manage.py shell  
  168. 指定端口映射到主机:  
  169. $ docker-compose run --publish 8080:80 -p 2022:22 -p 127.0.0.1:2021:21 web python manage.py shell  
  170. link db容器:  
  171. $ docker-compose run db psql -h db -U docker  
  172. 不linke容器,单独启动指定容器:  
  173. $ docker-compose run --no-deps web python manage.py shell  
  174. scale  
  175. Usage: scale [SERVICE=NUM...]  
  176. 设置服务的个数.  
  177. $ docker-compose scale web=2 worker=3  
  178. start  
  179. Usage: start [SERVICE...]  
  180. 启动服务.  
  181. stop  
  182. Usage: stop [options] [SERVICE...]  
  183.   
  184.   
  185. Options:  
  186. -t, --timeout TIMEOUT     关闭超时时间 (default: 10).  
  187. 停止容器.  
  188. up  
  189. Usage: up [options] [SERVICE...]  
  190.   
  191.   
  192. Options:  
  193.     -d                         后台运行,输出容器的名字.  
  194.                                Incompatible with --abort-on-container-exit.  
  195.     --no-color                  单色输出.  
  196.     --no-deps                  不启动link服务.  
  197.     --force-recreate           强制重新创建compose服务,即使没有任何改变。重新创建后启动容器  
  198.                                Incompatible with --no-recreate.  
  199.     --no-recreate               如果容器已经存在,不重新创建.  
  200.                                Incompatible with --force-recreate.  
  201.     --no-build                 不创建重启,即使镜像不存在.  
  202.     --build                    重新创建镜像,然后生成容器.  
  203.     --abort-on-container-exit  任何容器停止,自动停止所有容器.  
  204.                                Incompatible with  

下面创建一个web项目,一个nginx挂载三个web容器

docker-nginx目录作为工作目录,并在其中创建一个子目录nginx

[plain]  view plain  copy
  1. # pwd  
  2. /root/docker-nginx  
  3. [root@yankerp docker-nginx]# mkdir nginx  
  4. [root@yankerp docker-nginx]# ll  
  5. 总用量 0  
  6. drwxr-xr-x. 2 root root 6 9月  23 15:16 nginx  

在/root/docker-nginx/nginx下创建nginx的主配置文件nginx.conf

在/root/docker-nginx/下创建docker-compose.yml文件
下面是创建目录的内容

nginx.conf文件内容

[plain]  view plain  copy
  1. # pwd  
  2. /root/docker-nginx/nginx  
  3. [root@yankerp nginx]# vim nginx.conf  
  4. [root@yankerp nginx]# cat nginx.conf   
  5.   
  6. #user  nobody;  
  7. worker_processes  1;  
  8.   
  9. #error_log  logs/error.log;  
  10. #error_log  logs/error.log  notice;  
  11. #error_log  logs/error.log  info;  
  12.   
  13. #pid        logs/nginx.pid;  
  14.   
  15.   
  16. events {  
  17.     worker_connections  1024;  
  18. }  
  19.   
  20.   
  21. http {  
  22.     include       mime.types;  
  23.     default_type  application/octet-stream;  
  24.   
  25.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  26.     #                  '$status $body_bytes_sent "$http_referer" '  
  27.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  28.   
  29.     #access_log  logs/access.log  main;  
  30.   
  31.     sendfile        on;  
  32.     #tcp_nopush     on;  
  33.   
  34.     #keepalive_timeout  0;  
  35.     keepalive_timeout  65;  
  36.   
  37.     #gzip  on;  
  38. upstream yankerp{  
  39.                server Apache1:80 weight=1;  
  40.                server Apache2:80 weight=1;  
  41.                server Apache3:80 weight=1;  
  42.         }  
  43.     server {  
  44.         listen       80;  
  45.         server_name  localhost;  
  46.   
  47.         #charset koi8-r;  
  48.   
  49.         #access_log  logs/host.access.log  main;  
  50.   
  51.         location / {  
  52.             root   html;  
  53.             index  index.html index.htm;  
  54.             proxy_pass http://yankerp;  
  55.         }  
  56.   
  57.         #error_page  404              /404.html;  
  58.   
  59.         # redirect server error pages to the static page /50x.html  
  60.         #  
  61.         error_page   500 502 503 504  /50x.html;  
  62.         location = /50x.html {  
  63.             root   html;  
  64.         }  
  65.   
  66.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  67.         #  
  68.         #location ~ \.php$ {  
  69.         #    proxy_pass   http://127.0.0.1;  
  70.         #}  
  71.   
  72.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  73.         #  
  74.         #location ~ \.php$ {  
  75.         #    root           html;  
  76.         #    fastcgi_pass   127.0.0.1:9000;  
  77.         #    fastcgi_index  index.php;  
  78.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  79.         #    include        fastcgi_params;  
  80.         #}  
  81.   
  82.         # deny access to .htaccess files, if Apache's document root  
  83.         # concurs with nginx's one  
  84.         #  
  85.         #location ~ /\.ht {  
  86.         #    deny  all;  
  87.         #}  
  88.     }  
  89.   
  90.   
  91.     # another virtual host using mix of IP-, name-, and port-based configuration  
  92.     #  
  93.     #server {  
  94.     #    listen       8000;  
  95.     #    listen       somename:8080;  
  96.     #    server_name  somename  alias  another.alias;  
  97.   
  98.     #    location / {  
  99.     #        root   html;  
  100.     #        index  index.html index.htm;  
  101.     #    }  
  102.     #}  
  103.   
  104.   
  105.     # HTTPS server  
  106.     #  
  107.     #server {  
  108.     #    listen       443 ssl;  
  109.     #    server_name  localhost;  
  110.   
  111.     #    ssl_certificate      cert.pem;  
  112.     #    ssl_certificate_key  cert.key;  
  113.   
  114.     #    ssl_session_cache    shared:SSL:1m;  
  115.     #    ssl_session_timeout  5m;  
  116.   
  117.     #    ssl_ciphers  HIGH:!aNULL:!MD5;  
  118.     #    ssl_prefer_server_ciphers  on;  
  119.   
  120.     #    location / {  
  121.     #        root   html;  
  122.     #        index  index.html index.htm;  
  123.     #    }  
  124.     #}  
  125.   
  126. }  
  127. daemon off;  

yml文件内容

[plain]  view plain  copy
  1. [root@yankerp docker-nginx]# pwd  
  2. /root/docker-nginx  
  3. [root@yankerp docker-nginx]# ls  
  4. docker-compose.yml  nginx  
  5. [root@yankerp docker-nginx]# cat docker-compose.yml   
  6. Apache1:  
  7.   image: centos:http  
  8.   expose:  
  9.     - 80  
  10.   
  11. Apache2:  
  12.   image: centos:http  
  13.   expose:  
  14.     - 80  
  15.   
  16. Apache3:  
  17.   image: centos:http  
  18.   expose:  
  19.     - 80  
  20.   
  21. nginx:  
  22.   image: centos:nginx  
  23.   links:  
  24.     - Apache1  
  25.     - Apache2  
  26.     - Apache3  
  27.   ports:  
  28.     - "8000:80"  
配置完成后运行compose项目

在docker-nginx目录下执行docker-compose up -d启动应用

[plain]  view plain  copy
  1. # pwd  
  2. /root/docker-nginx  
  3. [root@yankerp docker-nginx]# docker-compose up -d  
  4. Creating dockernginx_Apache2_1 ...   
  5. Creating dockernginx_Apache1_1 ...   
  6. Creating dockernginx_Apache3_1 ...   
  7. Creating dockernginx_Apache2_1  
  8. Creating dockernginx_Apache1_1  
  9. Creating dockernginx_Apache2_1 ... done  
  10. Creating dockernginx_nginx_1 ...   
  11. Creating dockernginx_nginx_1 ... done  

查看容器启动情况。

[plain]  view plain  copy
  1. # docker ps  
  2. CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES  
  3. a7e7bed203a6        centos:nginx        "nginx"                  29 seconds ago      Up 9 seconds        0.0.0.0:8000->80/tcp   dockernginx_nginx_1  
  4. 8b6704eb5f6d        centos:http         "/usr/sbin/httpd -DFO"   39 seconds ago      Up 28 seconds       80/tcp                 dockernginx_Apache3_1  
  5. fa540c849b72        centos:http         "/usr/sbin/httpd -DFO"   39 seconds ago      Up 29 seconds       80/tcp                 dockernginx_Apache1_1  
  6. 50c037a51a16        centos:http         "/usr/sbin/httpd -DFO"   39 seconds ago      Up 29 seconds       80/tcp                 dockernginx_Apache2_1  


到这里有关于Docker-compose构建nginx反向代理配置就演示完毕了!!!希望对你有所帮助!!!@再见!!!!再见再见再见

猜你喜欢

转载自blog.csdn.net/kangshuo2471781030/article/details/79359856