Localhost refused to connect on WSL2 when accessed via https://localhost:8000/ but works when using internal WSL IP adress
Asked Answered
S

4

21
What I'm Trying to Achieve

To access localhost from my local machine during the development of a Symfony web app.

My Environment
  • WSL2 running on Windows 10
  • Linux, Apache2, MySQL, PHP-7.4 stack (with Xdebug3 intalled)
  • Debian 10
  • Symfony 5.4 (although not sure on if relevant to this problem)
Steps I've Taken
#!/bin/sh
REMOTEIP=`cat /etc/resolv.conf | grep nameserver | sed 's/nameserver\s//'` 
sed -i -E "s/client_host=[0-9\.]+/client_host=$REMOTEIP/g" /etc/php/7.4/mods-available/xdebug.ini
service php7.4-fpm start
service apache2 start
service mysql start  
  • Run my Symfony project on the development server using symfony serve -d (Symfony then tells me "The Web server is using PHP FPM 7.4.23 https://127.0.0.1:8000")
  • Go to https://localhost:8000/ in Chrome where the app is running
What I Expect to Happen
  • My Symfony web app to be running on https://localhost:8000/ when I visit the URL in my Chrome browser
What Actually Happens
  • I get "This site can't be reached localhost refused to connect." in the Chrome browser
What I've Tried
  • This used to happen less frequently and I would give my laptop a restart, repeat the process above, and I could connect via https://localhost:8000/. However, it refuses to connect more regularly now (like 8/10 times I start up for the day)
  • Connecting to https://127.0.0.1:8000 yields the same result.
  • Connecting to the site using the internal WSL IP address, found using hostname -I and replacing localhost with this IP (still on port 8000). This is an adequate workaround to use my app, however I am unable to interact with my database via MySQL Workbench without having to set up a new connection, therefore a fix where I can use localhost would be very helpful!
  • (Based off comments) Only ran symfony serve -d without starting apache and PHP services separately - still sometimes allows connections to localhost but sometimes doesn't work.
Conclusion

The behaviour is odd as it works sometimes but other times it doesn't when the exact same steps are carried out. I am unsure where else to look for answers and I can't seem to find anything online with this same problem. Please let me know if any config files, etc would be helpful. Thank you so much for your help! :)

Samoyedic answered 11/11, 2021 at 10:44 Comment(8)
"The behaviour is odd as it works sometimes but other times doesn't" With PHP-FPM and Apache2 why are you also using symfony server? Sounds like an IP or port conflict is occurring someplace. For example with XDebug running on localhost:8000 by default. I strongly suggest virtualizing your dev environment with Vagrant+VirtualBox or Docker, as a virtual machine, with the desired distro image. There are way too many nuanced issues with using WSL in comparison.Catlike
Either use Apache, or use symfony serve- or do you have any specific reason for configuring Apache when you didn't use it after all?Doityourself
Okay thanks @WillB. I'll look into that and update here if there is any success with the problem.Samoyedic
@NicoHaase oh okay that must've been an issue with my understanding I thought I had to start the apache2 services independently from the Symfony server, but that makes sense why I don't need to do that. Should this be as simple as not running service apache2 start and only running symfony serve -d? As I have tried stopping the apache service but I still get the same issue, however I'll also test this out with a restart.Samoyedic
Simply choose one. If you want to use Apache on the production system, and you already have Apache installed on your development system, it looks fine to me to not use symfony serve, but ApacheDoityourself
Nice one @NicoHaase! I have just tried a restart and only ran symfony serve -d and started mysql and it works now :) Like I mentioned though it was quite flukey before, I will try it again each day in the following week and update here whether that is a definite fix. You can create and answer at that point as well if you would like as well so I can accept it.Samoyedic
If using Symfony server, you also do not need start PHP-FPM, which by default listens on localhost:9000, and may cause issues with other services that also uses port 9000.Catlike
I can confirm that annoyingly the problem still exists. The past few day I simply ran symfony serve -d or symfony server:start without also individually starting apache or PHP, for the first 2 days I connected to localhost:8000 with no problem. However, this morning I did this same and I got "This site can't be reached localhost refused to connect." again. I think it must be a Windows/WSL issue.Samoyedic
R
33

When it's working normally, as you are clearly aware, the "localhost forwarding" feature of WSL2 means that you can access services running inside WSL2 using the "localhost" address of the Windows host.

Sometimes, however, that feature breaks down. This is known to happen when you either:

  • Hibernate
  • Have the Windows "Fast Startup" feature enabled (and it is the default). Fast Startup is a pseudo-hibernation which triggers the same problem.

Typically the best solution is to disable Hibernation and Fast Startup. However, if you do need these features, you can reset the WSL localhost feature by:

  • Exiting any WSL instances
  • Issuing wsl --shutdown
  • Restarting your instance

It's my experience that localhost forwarding will work after that. However, if it doesn't, thanks to @lwohlhart in the comments for mentioning that another thing to try is disabling IPv6 on WSL2, since (I believe) there's a possibility that the application is listening on IPv6 while the Windows->WSL2 connection localhost connection is being attempted on IPv6.

You can disable IPv6 on WSL2 per this Github comment by creating or editing .wslconfig in your Windows user profile directory with the following:

[wsl2]
kernelCommandLine=ipv6.disable=1

A wsl --shutdown and restart will be necessary to complete the changes.

If you find that this works, it may be possible to solve the issue by making sure to either use the IPv4 (127.0.0.1) or IPv6 (::1) address specifically in place of localhost on the Windows side, or by configuring the service to listen on both addresses.

Rizal answered 17/11, 2021 at 13:7 Comment(8)
Ahhh thank you so much @Rizal ! This has worked :) This makes a lot of sense as well and was the kind of solution I was looking for as I knew it was something to do with Windows and WSL. I did wsl --shutdown so I didn't disable Fast Startup (although I might consider it as I think I'd rather everything properly shuts down when I shut down my laptop). Many thanks again.Samoyedic
This was a very clear and well explained answer as well, hopefully this will help others with the same problem as well.Samoyedic
not work for me on WSL2Resilience
Just want to add that one thing that helped me out was to disable ipv6 on my wsl instances (github.com/microsoft/WSL/issues/9211#issuecomment-1331395957) in~/.wslconfig otherwise the application running is wsl would apparently try to repond to the tcp connection request via the ipv6 version of the resolved localhost ip. This seemed to cause all sorts of connection build up troubles.Emplace
@Emplace Good point! Added to the answer. Would you review and make sure that my understanding of your situation is correct? Thanks!Rizal
Thanks for considering my comment. I'd love to validate it, however I'm currently unsure if I just fooled myself. My brilliant 'fix' with disabled ipv6 worked for half a day yesterday. Now I'm getting a very flaky behaviour with localhost forwarding working initially after restarting wsl and at some point it stops... I'll get back to you if I have something deterministic to report :-)Emplace
@Emplace unfortunately, disabling IPv6 seems to prevent any ports from being forwarded for me eitherNeurilemma
I wish I had found this answer a long time ago, I've been struggling with this problem for so longEnvious
R
4

You may try "Mirrored Mode" of WSL2

As explained in https://superuser.com/a/1732513

To enable Mirrored mode:

Add the following to your <windows_user_profile>/.wslconfig:

[wsl2]
networkingMode=mirrored

Exit your WSL distribution

Run wsl --shutdown from command prompt then restart WSL

This helped me in the scenario of running my .net core API server on windows which is being access by NextJs Route Handler (running on wsl)

Ranice answered 20/4 at 12:59 Comment(0)
T
-1

Try to run command netstat -nltp. It shows active addresses and ports. Your nginx process should be run at 0.0.0.0:8000. 0.0.0.0 means the nginx process is available from anywhere.

If your nginx process is ran by any specific ip address, you should access it by that ip address, e.g http://192.168.4.2:8000.

Tarsus answered 11/11, 2021 at 11:3 Comment(3)
Hi, I'm not sure if you saw but as I put in my question, I'm actually running Apache not nginx. Is this answer still applicable?Samoyedic
Please share more details. What makes you think the OP uses nginx anywhere?Doityourself
Sorry, I mean web service.Tarsus
C
-1

New Answer

I changed ENV HOSTNAME localhost to ENV HOSTNAME '0.0.0.0' in my Dockerfile to make it work in my Next.js application. You can see the code here.

Original Answer

This really happened & idk why it worked but it worked. Something to do with WSL.

I had to pass a port to make it work like:

docker run -d -p 8000:8000 node-app

If you don't pass it, it gives localhost can't be reached error like this:

localhost doesnt work with docker

Cuisse answered 15/2 at 5:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.