0

以下是我的docker-compose.yml文件。使用nginx无法在容器外访问虚拟主机

我想设置反向代理。

我尝试将VIRTUAL_HOST和VIRTUAL_PORT环境设置为虚拟码头集装箱。它可以通过使用主机名和端口来访问,如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是两个独立的Docker,它们安装了Node-Red。

下面是nginx的代理日志

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; 
    #} 
} 
+0

我早就知道conf.d是什么样子,并张贴'搬运工日志nginx的-proxy'的输出来实现这一点。 –

+0

请看看。我用日志更新了问题。谢谢:) –

回答

1

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

有工具,包括GasMask(OSX)和Host File Editor(Windows)中

+0

你的意思是我必须在Ubuntu系统上手动配置/ etc/hosts文件。对? 127.0.0.1 localhost 127.0.0.1 services.local 127.0.0.1:81 service1.services.local –

+0

是的。 Docker与本地主机上的DNS查询无关。 –

+0

仍然不工作。 –