WSL2 Nginx - Works on localhost but throws 403 Forbidden on 127.0.0.1 or custom host name

Viewed 1013

I've been trying to setup nginx on my WSL2 running Ubuntu 20.04. I got pretty much the entire site setup. It's working when I visit http://localhost. But I want it to be on http://app.test. So I added it to the host and tried, it throws 403 Forbidden error. I've not changed anything in nginx.conf. This is what I have so far:

Server Block

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html/app.test;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }
}

Permissions

The /var/www/html/app.test folder belongs to www-data:www-data user and group.

Hosts

I've added the following in both my Windows Hosts and on Ubuntu Hosts

127.0.0.1 app.test

Findings

  1. I checked the access.log. I've included 2 of the logs below. The first one is a non-working one (when visiting app.test) and the other is a working one (when visiting localhost).
127.0.0.1 - - [05/Nov/2020:00:07:09 +0400] "GET / HTTP/1.0" 403 564 "-" "Mozilla/5.0 (Windows NT 10.0;..."

::1 - - [05/Nov/2020:00:01:13 +0400] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0;...
  1. I tried running a curl command from Windows Terminal, I get 200 OK for http://localhost but 403 Forbidden for http://127.0.0.1

Thanks!

1 Answers

This is the solution for me.

Just add:

C:\Windows\System32\drivers\etc\hosts

127.0.0.1 app.test 
::1 app.test

WSLUbuntu:/etc/hosts

127.0.0.1 app.test
Related