使用nginx无法在容器外部访问虚拟主机

时间:2017-09-01 12:48:43

标签: docker nginx docker-compose jwilder-nginx-proxy nginx-reverse-proxy

下面是我的docker-compose.yml文件。

我正在尝试设置反向代理。

我尝试将VIRTUAL_HOST和VIRTUAL_PORT环境设置为虚拟docker容器。可以使用主机名和端口http://services.local:81

访问它

请建议如何使用浏览器访问这些服务。

我想访问http://service1.services.localhttp://services.local/service1等服务。http://services.local是我的默认主机

注意: - 我正在使用jwilder/nginx-proxy

搬运工-compose.yml

version: '2'

networks:
  prodnetwork:
    driver: bridge

services:
  nginx-proxy:
    image: jwilder/nginx-proxy
    container_name: nginx-proxy
    ports:
      - "80:80" 
    networks:
      - prodnetwork 
    volumes:
      - "/var/run/docker.sock:/tmp/docker.sock:ro"
      - "./nginx.tmpl:/app/nginx.tmpl:ro"
      - /etc/nginx/conf.d
    environment:
      - DEFAULT_HOST=services.local,localhost

  dockergen:
    image: jwilder/docker-gen
    command: -notify-sighup nginx -watch /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf
    networks:
      - prodnetwork
    volumes_from:
      - nginx-proxy
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - ./nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl

  service1:
    build: vgw1
    container_name: service1
    networks:
      - prodnetwork 
    ports:
      - "81:80" 
    environment:
      - VIRTUAL_HOST=service1.services.local
    expose:
      - "81"

  service2:
    build: vgw2
    container_name: service2
    networks:
      - prodnetwork 
    ports:
      - "82:80" 
    environment:
      - VIRTUAL_HOST=service2.services.local
    expose:
      - "82"

这里vgw1和vgw2是安装了Node-Red的两个独立的docker。

以下是 nginx-proxy log

WARNING: /etc/nginx/dhparam/dhparam.pem was not found. A pre-generated dhparam.pem will be used for now while a new one
is being generated in the background.  Once the new dhparam.pem is in place, nginx will be reloaded.
forego     | starting dockergen.1 on port 5000
forego     | starting nginx.1 on port 5100
dockergen.1 | 2017/09/01 13:16:59 Generated '/etc/nginx/conf.d/default.conf' from 4 containers
dockergen.1 | 2017/09/01 13:16:59 Running 'nginx -s reload'
dockergen.1 | 2017/09/01 13:17:09 Error running notify command: nginx -s reload, exit status 1
dockergen.1 | 2017/09/01 13:17:09 Watching docker events
dockergen.1 | 2017/09/01 13:17:09 Contents of /etc/nginx/conf.d/default.conf did not change. Skipping notification 'nginx -s reload'
nginx.1    | 172.18.0.1 - - [01/Sep/2017:13:17:51 +0000] "GET /service1 HTTP/1.1" 404 571 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36" "-"
nginx.1    | 2017/09/01 13:17:51 [error] 37#37: *1 open() "/usr/share/nginx/html/service1" failed (2: No such file or directory), client: 172.18.0.1, server: localhost, request: "GET /service1 HTTP/1.1", host: "services.local"
nginx.1    | 172.18.0.1 - - [01/Sep/2017:13:18:03 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36" "-"
nginx.1    | 172.18.0.1 - - [01/Sep/2017:13:18:06 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36" "-"
nginx.1    | 2017/09/01 13:18:49 [error] 37#37: *1 "/usr/share/nginx/html/service1/index.html" is not found (2: No such file or directory), client: 172.18.0.1, server: localhost, request: "GET /service1/ HTTP/1.1", host: "services.local"
nginx.1    | 172.18.0.1 - - [01/Sep/2017:13:18:49 +0000] "GET /service1/ HTTP/1.1" 404 571 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36" "-"
2017/09/01 13:19:21 [warn] 44#44: server name "services.local/service1" has suspicious symbols in /etc/nginx/conf.d/default.conf:74
nginx: [warn] server name "services.local/service1" has suspicious symbols in /etc/nginx/conf.d/default.conf:74
2017/09/01 13:19:21 [emerg] 44#44: host not found in upstream "services.local" in /etc/nginx/conf.d/default.conf:78
nginx: [emerg] host not found in upstream "services.local" in /etc/nginx/conf.d/default.conf:78
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
dhparam generation complete, reloading nginx
nginx.1    | 2017/09/01 13:23:27 [error] 37#37: *6 "/usr/share/nginx/html/service1/index.html" is not found (2: No such file or directory), client: 172.18.0.1, server: localhost, request: "GET /service1/ HTTP/1.1", host: "services.local"
nginx.1    | 172.18.0.1 - - [01/Sep/2017:13:23:27 +0000] "GET /service1/ HTTP/1.1" 404 571 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36" "-"
Custom dhparam.pem file found, generation skipped
forego     | starting dockergen.1 on port 5000
forego     | starting nginx.1 on port 5100
dockergen.1 | 2017/09/04 07:18:19 Generated '/etc/nginx/conf.d/default.conf' from 4 containers
dockergen.1 | 2017/09/04 07:18:19 Running 'nginx -s reload'
dockergen.1 | 2017/09/04 07:18:19 Watching docker events
dockergen.1 | 2017/09/04 07:18:19 Contents of /etc/nginx/conf.d/default.conf did not change. Skipping notification 'nginx -s reload'
nginx.1    | localhost 172.18.0.1 - - [04/Sep/2017:07:26:42 +0000] "GET /favicon.ico HTTP/1.1" 503 615 "http://localhost/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36"

/nginx/conf.d/default.conf

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

1 个答案:

答案 0 :(得分:1)

为了更改浏览器映射的路径,您将不得不更改etc/hosts文件中的配置。这实际上是在告诉您的浏览器在本地计算机上查找您指定的URL的DNS记录。

有一些工具可以实现这一目标,包括https://registry.ng.bluemix.net/v2(osX)和GasMask(Windows)