使用Docker-Compose和nginx-proxy

时间:2016-06-21 17:58:25

标签: django nginx docker docker-compose

我是nginx的新手,我正在尝试使用 Docker-Compose 设置链接的DOcker容器。

基于教程here和jwilder的文档here,我试图在我的Django容器上部署Nginx代理。 docker-compose.yml的结构如下:

  build: .
  command:  bash -c "sleep 5 && python -u reroute/manage.py runserver 0.0.0.0:8081"
  volumes:
    - .:/code
  environment:
    - PYTHONUNBUFFERED=0
    - VIRTUAL_HOST=site1.com
    - VIRTUAL_PORT=8081
  ports:
    - "8081:8081"
  links:
    - db

db:
  image: postgres:latest
  environment:
    POSTGRES_PASSWORD: xxxxxx
    POSTGRES_USER: xxxxxx
  ports:
    - "5432"
  volumes:
    - ./backups:/home/backups

nginx:
  image: jwilder/nginx-proxy
  restart: always
  volumes:
    - /var/run/docker.sock:/tmp/docker.sock
  ports:
    - "80:80"

Nginx的.conf文件中的配置如下所示:

server {
    server_name _; # This is just an invalid value which will never trigger on a real hostname.
    listen 80;
    access_log /var/log/nginx/access.log vhost;
    return 503;
}
upstream urls.mapquest.com {
                ## Can be connect with "bridge" network
            # googleadtrafficredirect_web_1
            server 172.17.0.4:8081;
}
server {
    server_name urls.mapquest.com;
    listen 80 ;
    access_log /var/log/nginx/access.log vhost;
    location / {
        proxy_pass http://urls.mapquest.com;
    }
}

我希望nginx代理重定向到应用程序本身。我在Ubuntu上编辑了我的hosts文件,指向我指定的新域。但是,当我在本地部署应用程序时,我收到502错误网关错误,并查看日志中的Nginx容器显示我收到连接拒绝错误:

nginx.1    | 2016/06/21 17:39:00 [error] 24#24: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: urls.mapquest.com, request: "GET / HTTP/1.1", upstream: "http://172.17.0.4:8081/", host: "site1.com"

有人能指出我的问题是正确的方向吗?

1 个答案:

答案 0 :(得分:1)

事实证明应用程序本身存在问题。我在更改远程版本之前在本地测试了docker-compose容器,并忘记启用我的应用程序工作所需的某个配置。一旦我更正了,应用程序正确加载并且nginx成功重新路由。