Nginx running in WSL2 (Ubuntu 20.04) does not serve HTML page to Windows 10 host on another port than port 80

Viewed 11122

I am new to WSL2 but so far it works really nice. I have a simple HTML Page i want to serve with Nginx, but i want it to access with a web browser on the host. The default nginx webpage works(!), so i started to mimic the default nginx html page (/var/www/html/index.html).

I have created:
/var/www/test.dev/index.html
/etc/nginx/sites-available/test.dev (+Symlink in sites-enabled/)

Nginx config:

server {
    listen 9000;
    listen [::]:9000;

    server_name test.dev;

    root /var/www/test.dev;
    index index.html;

    location / {
            try_files $uri $uri/ =404;
    }
}

So the only big difference to the default config is the port 9000.

I reloaded/restarted nginx and i tried to curl my configs:

$ curl https://localhost

$ curl https://localhost:9000

both requests were successful.

But now i want to access the pages on my Windows host with a web browser. The first one (default) works and i can see the default Nginx HTML page. The second one does not work: site can't be reached.

So my questions:
1. Why is that? Do i have to make some changes to the Windows Firewall settings?
2. I like to have a virtual host name like example.com instead of localhost:9000
I've edited /etc/hosts... it works with curl but again not in the host Browser

1 Answers

In your nginx config you have:

  listen 9000;
  server_name test.dev;

So your domain should be resolved with: http://test.dev:9000

But you should also add test.dev to your Windows hosts file %windir%\system32\drivers\etc\hosts

127.0.0.1 test.dev
Related