4

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://learn.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

Arash Rabiee
  • 1,019
  • 2
  • 16
  • 31
  • 1
    wsl2 gets a new IP each time you start a dist. `localhost` always works, but not using `127.0.0.1`. You either need to check the what IP it gets each time you start it and update your hosts file in windows with that, or just stick with `localhost` – M. Eriksson May 12 '21 at 19:37
  • the problem is that it's not even run once – Arash Rabiee May 12 '21 at 22:23
  • 1
    Localhost forwarding can break down in certain situations. Before going any further in troubleshooting, exit your WSL instances, run `wsl --shutdown` from PowerShell or CMD, and then restart the WSL instance and services. See [here](https://stackoverflow.com/a/63455770/11810933) for more information. – NotTheDr01ds May 13 '21 at 02:20
  • @NotTheDr01ds I did as you suggested, and also things recommended on the link but no progress – Arash Rabiee May 14 '21 at 10:20

1 Answers1

2

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.

Lux Logica
  • 1,429
  • 1
  • 14
  • 30
  • 2
    Welcome back to Stack Overflow! Please phrase this as an explained conditional answer, in order to avoid the impression of asking a clarification question instead of answering (for which a comment should be used instead of an answer, compare https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). For example like "If your problem is ... then the solution is to .... because .... ." – hakre Nov 01 '21 at 23:29