0

I try to connect my service run on windows wsl2 with docker nginx as proxy, but the request always timeout. Here my configuration & docker compose file.

Docker-compose:

version: '3'
services:
    proxy:
       image: nginx:alpine
       ports:
         - "8999:80"
       extra_hosts:
         - "host.docker.internal:host-gateway"
       volumes:
         - "./api.conf:/etc/nginx/nginx.conf"

api.conf

events {
    worker_connections 1024;
}
http {
    server{
        listen 80;

        location /v1/api1/ {
            rewrite ^/v1/api1/(.*)$ /v1/$1 break;
            proxy_pass http://host.docker.internal:3000/;
        }

        location /v1/api2/ {
            rewrite ^/v1/api2/(.*)$ /v1/$1 break;
            proxy_pass http://host.docker.internal:4000/;
        }
}

Error message

[error] 33#33: *2 upstream timed out (110: Operation timed out) while connecting to upstream, client: 172.19.0.1, server: , request: "POST /v1/account/buyer/auth/login HTTP/1.1", upstream: "http://192.168.65.1:3000/v1/data", host: "localhost:8999"

The service is fine when I access in normal way, using localhost, for example

http://localhost:3000/v1/data

I try to using proxy and make api call into only 1 endpoint.

  • Where is the `host-gateway`? The `extra_hosts` directive is for adding some host alias to `/etc/hosts` file. But inside container there is no other clue for `host-gateway`. It's not also FQDN, so nginx process could not find the final destination `host-gateway`. – J. Song Dec 05 '21 at 01:28
  • @J.Song , for `host-gateway` I follow this [link](https://stackoverflow.com/questions/31324981/how-to-access-host-port-from-docker-container) to map `host.docker.internal`, my docker version is 20.10.0, so I think it will work on WSL2. – Hieronimus Budisantosa Dec 05 '21 at 02:00
  • Ok, I think it works for me. Could share the results of `netstat -nptl` from your host not inside docker? – J. Song Dec 05 '21 at 02:15
  • Ok, thank you, here screenshot from my WSL2 host [![ss.png](https://i.postimg.cc/bYZ9d6wW/ss.png)](https://postimg.cc/c6NnbBYB), @J.Song – Hieronimus Budisantosa Dec 05 '21 at 02:41
  • Ok, I think I found the reason. Your api services are connected only ipv6 address. Set `net.ipv6.bindv6only` value as 0 by this command `sysctl net.ipv6.bindv6only=0` . And restart all your api services. REFER this page https://stackoverflow.com/questions/29957143/make-docker-use-ipv4-for-port-binding. YOU MUST check your api services are listening on ipv4. – J. Song Dec 05 '21 at 02:54

0 Answers0