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

仍然不工作。 –

相關問題