上一篇介绍了docker 安装和docker-compose配置
使用docker pull nginx:1.12.0
编写docker-compose.yml
nginx:
image: 17daebd00e2c ports: - 80:80 volumes: - /home/conf/nginx.conf:/etc/nginx/nginx.conf links: - nginx1 - nginx2 privileged: truenginx1:
image: 17daebd00e2c volumes: - /home/www1:/usr/share/nginx/html privileged: truenginx2:
image: 17daebd00e2c volumes: - /home/www2:/usr/share/nginx/html privileged: true
编写nginx.conf
在http模块中添加
upstream pro {
ip_hash;//根据ip访问,测试时可以注释 server nginx1:80; server nginx2:80; }server模块中添加
location / {
proxy_pass http://pro; #proxy_redirect off; #如果是非80端口,配置为Host $host:端口号,目的是将代理服务器收到的用户的信息传到真实服务器上 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 300; proxy_send_timeout 300; proxy_read_timeout 300; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; add_header Access-Control-Allow-Origin *;}
最后在www1和www2目录中添加html文件
直接访问html地址即可
nginx1和nginx2只可以在docker容器中访问