Search code examples
windows-subsystem-for-linuxhosts

WSL hosts file with custom domain


I create a basic express app

const express = require('express');

const app = express();

app.get("*", (req, res) => {;
  res.contentType('html');
  res.send("HELLO FROM WSL");
});
const port = 80
app.listen(port);

Then I add following entrie in c:\windows\system32\drivers\etc\hosts

127.0.0.1        custom.local

Then I shutdown wsl wsl --shutdown and re-open to start my express app.

If I check hosts file from WSL (cat /etc/hosts), I got following result

# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateHosts = false
127.0.0.1       localhost
127.0.1.1       LAPTOP-ZECKA.localdomain        LAPTOP-ZECKA
127.0.0.1       custom.local

Then I go to http://custom.local trough chome in windows. But it's doesn't display my express app. (If i run express on windows instead of wsl it's work well).

What's wrong on my hosts file ?


Solution

  • Finally I found a solution on github: https://github.com/microsoft/WSL/issues/5728#issuecomment-917295590

    Instead declare domain like that

    127.0.0.1        custom.local
    

    I do as follow:

    127.0.0.1   custom.local
    ::1         custom.local localhost