Can't access nginx sites on wsl from windows browser

Viewed 2413

I installed wsl2 => then ubuntu => then nginx => php.fpm (version I need) Then added mysite.local.conf to my nginx config but can't access the site on wsl from windows

nginx config code:

server {
        listen 80 default_server;
        server_name mysite.local *.mysite.local;

        root /mnt/d/projects/arash/original/Web/frontend/web;
        

        access_log /mnt/d/projects/arash/original/logs/arash-access.log;
        error_log /mnt/d/projects/arash/original/logs/arash-error.log;


        location ~* ^/backend$ {
                rewrite ^ http://backend.mysite.local permanent; #301 redirect
        }

        location ~* ^/setting$ {
                rewrite ^ http://setting.mysite.local permanent; #301 redirect
        }

        if ($host ~* ^(arash\.local)) {
        rewrite ^ http://www.$host$uri permanent; #301 redirect
        }

        location ~ /\. {
                deny all;
        }

        location / {
                index index.php index.html index.htm;
                try_files $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
                fastcgi_read_timeout 1h;
                fastcgi_pass unix:/run/php/php7.1-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
        }

        location ~* ^.+\.(rss|atom|jpg|jpeg|gif|png|ico|rtf|js|css|svg|woff)$ {
                expires 7d;
        }

        charset utf8;
}

server {
        listen 80;
        server_name backend.mysite.local;

        access_log /mnt/d/projects/arash/original/logs/arash-access.log;
        error_log /mnt/d/projects/arash/original/logs/arash-error.log;

        root /mnt/d/projects/arash/original/Web/backend;

    location / {
                index index.php index.html index.htm;
                try_files   $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
                fastcgi_read_timeout 1h;
                fastcgi_pass unix:/run/php/php7.1-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
        }

}

server {
        listen 80;
        server_name setting.mysite.local;

        access_log /mnt/d/projects/arash/original/logs/arash-access.log;
        error_log /mnt/d/projects/arash/original/logs/arash-error.log;

        root /mnt/d/projects/arash/original/Web/setting/web;

        location / {
                index index.php index.html index.htm;
                try_files   $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
                fastcgi_read_timeout 1h;
                fastcgi_pass unix:/run/php/php7.1-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
        }

}

then ran ln -s /etc/nginx/sites-available/mysite.local.conf /etc/nginx/sites-enabled/

then got my wsl IP using ifconfig 172.28.11.207 and added 172.28.11.207 mysite.local to hosts of windows then tried to reach mysite.local from windows browser shows nothing

I also set 127.0.0.1 mysite.local but no chance either

I used ipconfig /flushdns after each change in host

The config of fast startup on windows is off The config of hibernate on windows is off

then I tried https://docs.microsoft.com/en-us/windows/wsl/wsl-config#wsl-2-settings this and added .wslconfig to my user with this code:

[wsl2]
localhostForwarding=true

still nothing

when I tried to run curl mysite.local I got curl: (7) Failed to connect to mysite.local port 80: Connection refused

then I set wsl to use version 1 with wsl --set-version Ubuntu-20.04 1 and run again curl mysite.local now I get curl: (52) Empty reply from server

1 Answers

Port may already be in used by another service, like IIS. I faced an nginx service start issue, because the port was already in use by IIS. After turning off the IIS sevice, the nginx service ran without any fails.

Related