我希望能够访问包含Web应用程序的同一个主机上的多个容器。
当我想访问主机(通过IP地址)或容器(例如通过host_ip_adress /容器1),我得到了来自nginx的503错误。 我想要的是通过ip_addrress_host / container1访问我的container1。
我在网上find的解决scheme是设置一个nginx-proxy前端服务器(来源: https : //blog.florianlopes.io/host-multiple-websites-on-single-host-docker/ )
我的docker合成文件: docker-compose.yml
version: '2' services: nginx-proxy: image: jwilder/nginx-proxy ports: - "80:80" volumes: - /var/run/docker.sock:/tmp/docker.sock site_a: image: php:7.0-apache expose: - "80" environment: - VIRTUAL_HOST=container1 volumes: - ./php:/var/www/html site_b: image: php:7.0-apache expose: - "80" environment: - VIRTUAL_HOST=container2 volumes: - ./php:/var/www/html
我用这个命令运行它:
docker-compose up
我在/ etc / hosts文件中的条目:
127.0.1.1 container1 127.0.0.1 container2
我从外面提出请求时看到的日志:
nginx-proxy_1 | nginx.1 | 192.168.12.28 192.168.12.82 - - [25/Oct/2017:09:46:42 +0000] "GET /container1 HTTP/1.1" 503 615 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" nginx-proxy_1 | nginx.1 | 192.168.12.28 192.168.12.82 - - [25/Oct/2017:09:46:42 +0000] "GET /favicon.ico HTTP/1.1" 503 615 "http://192.168.12.28/container1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
谢谢你的帮助,对不起我的英文不好! 🙂
编辑:
这是我在开始时的nginx-proxy容器的日志:
forego | starting dockergen.1 on port 5000 forego | starting nginx.1 on port 5100 dockergen.1 | 2017/10/25 14:01:53 Generated '/etc/nginx/conf.d/default.conf' from 3 containers dockergen.1 | 2017/10/25 14:01:53 Running 'nginx -s reload' nginx.1 | 2017/10/25 14:01:54 [warn] 30#30: server name "192.168.12.28/container1" has suspicious symbols in /etc/nginx/conf.d/default.conf:60 nginx.1 | 2017/10/25 14:01:54 [warn] 30#30: server name "192.168.12.28/container2" has suspicious symbols in /etc/nginx/conf.d/default.conf:74 dockergen.1 | 2017/10/25 14:01:54 Watching docker events dockergen.1 | 2017/10/25 14:01:54 Contents of /etc/nginx/conf.d/default.conf did not change. Skipping notification 'nginx -s reload'
编辑2:我试图“定制”nginx代理与PawełTatarczuk给出的configuration文件( https://serverfault.com/a/880384/441157 )
现在,当我做一个请求,如http://192.168.12.28/container1我有这个日志:
nginx-proxy_1 | nginx.1 | 2017/10/26 08:46:19 [error] 41#41: *1 open() "/etc/nginx/html/container1" failed (2: No such file or directory), client: 192.168.12.82, server: 192.168.12.28, request: "GET /container1 HTTP/1.1", host: "192.168.12.28"
编辑3:添加? 重写
nginx-proxy_1 | nginx.1 | 2017/10/26 09:11:00 [error] 31#31: *1 container1 could not be resolved (2: Server failure), client: 192.168.12.82, server: 192.168.12.28, request: "GET /container1 HTTP/1.1", host: "192.168.12.28"
我想要的是通过ip_addrress_host / container1访问我的container1。
你确定要访问容器与例如http://127.0.0.1/container1 ? 那么jwilder/nginx-proxy不是最好的方法。
您的代理正在端口80本地监听,它将代理请求到container1和container2但不会代理path/container1和/container2 。
curl
curl -H "Host: container1" localhost
浏览器
打开http:// container1
你可以附加一个自定义的configuration来处理代理path,使ip_address/container-name工作:
volumes : ./custom.conf:/etc/nginx/conf.d/custom.conf : ./custom.conf:/etc/nginx/conf.d/custom.conf 在docker-compose.yml旁边创buildcustom.conf :
server { server_name 192.168.XY; listen 80; location ~ ^/([^/]+)(/.*)? { proxy_pass http://$1$2; } }
链接容器
nginx-proxy: ... links: - site_a:container1 - site_b:container2
这只是开始,你应该改善这个以符合你的需求。 它应该使用http://192.168.XY/container1或http://192.168.XY/container2 。
请注意,有一个重写,所以http://192.168.XY/container1/some/path被代理到http:// container1 / some / path 。 我假设你不希望带有/container1前缀的请求到你的目标conatiner。