Docker Compose,nginx配置无法将“ varnish”解析为主机名

时间:2018-07-02 15:43:49

标签: docker nginx

我有以下设置:

version: '2'

services:
  web:
    build: ./nginx
    links:
      - "php"
    ports:
      - "8080:80"
      - "443:443"
    volumes:
      - ./build:/var/www/magento/build/
      - ./vagrant/tools:/var/www/tools

  php:
    image: php:7.2-fpm
    volumes:
      - ./build:/var/www/magento/build/
      - ./vagrant/etc/php/7.1/fpm/conf.d/90-custom.ini:/etc/php-fpm/fpm/conf.d/90-custom.ini
      - ./vagrant/etc/php/7.1/cli/conf.d/90-custom.ini:/etc/php-fpm/cli/conf.d/90-custom.ini
      - ./vagrant/etc/php/7.1/fpm/pool.d/www_ps.conf:/etc/php-fpm/fpm/pool.d/www_ps.conf
    links:
      - "cache"
      - "mysql"

  solr:
    build: ./solr
    ports:
      - "8983:8080"
    links:
      - "mysql"
    volumes:
      - solr0-data:/var/solr/core0/data/

  varnish:
    build: ./varnish
    ports:
      - "80:80"
    links:
      - "web"
      - "php"

  mysql:
    image: mariadb:latest
    ports:
        - "3306:3306"
    environment:
          - MYSQL_ROOT_PASSWORD=xxx
          - MYSQL_DATABASE=xxx
    volumes:
      - mysql:/var/lib/mysql

  cache:
      build: ./redis
      ports:
        - "6379:6379"

  session:
      build: ./redis
      ports:
        - "6389:6379"


volumes:
  solr0-data:
  mysql:

在我的nginx-config文件中,我使用:

# Varnish
upstream varnish_com  {
    keepalive 100;
    server varnish;
}

在同一文件中的用法:

server {
    listen          443 http2;

    server_name     www.cs.test;
    ssl_certificate /etc/ssl/nginx.pem;
    ssl_certificate_key /etc/ssl/nginx.key;
    ssl_session_cache shared:SSL:20m;
    ssl_session_timeout 180m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers MD5:ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH;

    rewrite ^/(nl|fr|es|it|en)/(.*)$ /$2 last;

    location / {

       ...
        proxy_pass              http://varnish_com;
    }
}

控制台上的输出:

web_1      | 2018/07/02 15:39:31 [emerg] 1#1: host not found in upstream "varnish" in /etc/nginx/sites-enabled/com.localhost:5
web_1      | nginx: [emerg] host not found in upstream "varnish" in /etc/nginx/sites-enabled/com.localhost:5
xxx_web_1 exited with code 1

为什么nginx不能将“清漆”解析为主机? AFAIK Docker自动别名化主机名,以避免IP繁忙。但是,nginx似乎无法完全将“ varnish”作为主机名,因此无法将http://varnish_com解析为http://[IP的VARNISH CONTAINER]。 :(

1 个答案:

答案 0 :(得分:0)

更新您的docker-compose文件以使用以下清漆:

varnish:
    hostname: varnish_com
    build: ./varnish

基本上,我已经添加了主机名。现在,您的Docker容器将不会获得一些随机名称。希望它应该解决提到的问题。