skip to Main Content

I have a setup that works successfully in Linux and MacOS, in which I run a docker nginx container to route all of my different services running locally.

The issue is that this same setup is throwing nginx Bad Gateway errors when running the docker container inside of Window’s WSL2, presumably because I’m missing some additional routing configuration between Windows and WSL2.

A simplified version of my setup:

docker-compose.yml

  nginx:
    image: nginx:alpine
    container_name: nginx
    volumes:
      - ./config/nginx.conf:/etc/nginx/nginx.conf
    ports:
      - 80:80
      - 443:443
    networks:
      - backend

/config/nginx.conf

    server {
        listen 80;
        server_name test.localhost;
        location / {
            set test.localhost host.docker.internal:3001;
            proxy_pass http://test.localhost;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $remote_addr;
        }
    }

    server {
        listen 80;
        server_name test2.localhost;
        location / {
            set test2.localhost host.docker.internal:3002;
            proxy_pass http://test2.localhost;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $remote_addr;
        }
    }

windows hosts file

127.0.0.1 test.localhost
127.0.0.1 test2.localhost

WSL2 Debian /etc/hosts file

127.0.0.1 test.localhost
127.0.0.1 test2.localhost

Both services are running inside WSL2 at ports 3001 and 3002.

Browsing to localhost:3001 and localhost:3002 provides the expected result, but if I go to test.localhost or test2.localhost I get 502 Bad Gateway errors from nginx.

Any idea on what I may be missing or guidance will be greatly appreciated.

2

Answers


  1. Maybe you could try with the workaround below, seen here.

    127.0.0.1 test.localhost
    ::1 test.localhost localhost
    
    Login or Signup to reply.
  2. You can use WSL2HOST which will automatically update your Windows hosts file with the WSL2 VM’s IP address.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search