Virtual host is not accessible outside container using nginx

2.3k views Asked by At

Below is my docker-compose.yml file.

I am trying to set reverse proxy.

I tried setting VIRTUAL_HOST and VIRTUAL_PORT Environment to virtual docker containers. it is accessible using Host name and port like http://services.local:81

Please suggest how can I access these services using browser.

I want to access services like http://service1.services.local or http://services.local/service1 Here http://services.local is my default host

Note:- I am using jwilder/nginx-proxy

docker-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"

Here vgw1 and vgw2 are two separate dockers which are having Node-Red installed.

Below is 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

There are 1 answers

3
Andrew Graham-Yooll On

In order to change the route that your browser maps too, you are going to have to change the configuration in your etc/hosts file. This is virtually telling your browser to look up the DNS records on your local machine for the url that you specify.

There are tools to accomplish this with including GasMask (osX) and Host File Editor (Windows)