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

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
Set up WSL2 according to this Microsoft WSL2 tutorial
Set up LAMP stack according to this Digital Ocean tutorial
Set up Symfony according to this Symfony tutorial
Run the following bash script on startup to start my services and set the host to the virtual WSL IP in my xdebug.ini file
#!/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! :)

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.

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.

Related

How to get WSL2 to route traffic through ProxyCap ssh tunnel

I'm trying to get WSL2 to use the tunnel present in my windows environment but I'm struggling with getting it to work properly.
In order to access our company servers I've set up ProxyCap to open a ssh connection to a proxy server. The only thing I did was point it to our jump server and then set up the following rule.
Now this works flawlessly in Windows and I'm able to ping a machine inside our company network by doing a simple ping x.x.x.x:pppp. However once I start my WSL2 environment and try pinging the same ip and port the only thing I get back is ping: ping x.x.x.x:pppp: Name or service not known.
My initial guess is that this is due to the network in WSL2 being different from the network in Windows. Is there any way to make WSL2 utilize the proxy tunnel running in my Windows environment?
I've also tried running sshuttle inside WSL2 to see if I could access the server that way and not have to go through Windows (I'm using the same command I use on my Linux machine where it works) but I get the same problem this way.

Access a localhost running in Windows from inside WSL2? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed last year.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I am running a local AEM server in my Windows machine. The server is running on localhost:4502. I am using Ubuntu distro running in WSL2 for my development. I want to access the localhost:4502 running in the Windows machine in my WSL2 Ubuntu.
Is there any way to do that or is it not possible ?
Short answer for most recent Windows versions
mDNS has been a feature of WSL2 for a while now. Concatenating your WSL2 hostname (or the equivalent command/function in your programming/language environment) with ".local" should get you access.
For example, from Bash, try:
ping "$(hostname).local"
For instance, if your hostname is "MyComputer", then the mDNS should be MyComputer.local.
If ICMP is blocked (as it seems to be on new Windows 11 installs), or if you want to test the connection to the actual port, then use netcat. It's available by default in the WSL Ubuntu installation, but may need to be installed in other distributions like openSUSE:
nc -zv "$(hostname).local" <portnumber>
Why localhost doesn't work
WSL2 is running with a virtual network (vNIC) that is created by the Windows Virtual Machine Platform (a subset of Hyper-V). Inside WSL2, localhost is the address of the vNIC.
What you need
WSL2 also sets up a virtual router on the Windows host to allow connectivity to both the outside world as well as the Windows host. You can see this via:
ip route
This is the address you need to use for the Windows host.
You could, of course, parse it from the route (or, as in an earlier answer, from /etc/resolv.conf), but WSL sets up a convenience mDNS (the .local domain) using the Windows "computer name", which is also used as the hostname of the WSL instance.
So concatenating $(hostname) (or it's equivalent in your programming/language environment) with ".local" should get you access.
Other considerations:
mDNS is reliant on the Windows host to resolve the name. If you have changed your /etc/resolv.conf under WSL, then this will likely not work.
Remember to open any necessary firewall ports. WSL2 is considered a separate network from that of the Windows host. Windows will consider network connections from WSL2 to be coming from an external source. (Credit to #RamilGilfanov for a comment pointing this out)
The first time a connection is made from WSL2 to a particular port, Windows Defender (if that is your firewall) will typically display a dialog asking if you want to grant access. However, in my experience, this dialog often gets buried under the main window due to timing of mouse-clicks, keyboard, etc., so it's easy to miss.
Remember to have your Windows service accept connections from remote hosts.
Many servers are configured by default to bind to localhost/127.0.0.1. Because WSL2 appears to Windows as a remote network, you'll typically need to update your configuration to bind to 0.0.0.0 or a specific address.
Note that, since the address for WSL2 changes after each reboot, it can be difficult to update your configuration each time. If at all possible, use 0.0.0.0 unless there are security concerns. Since WSL is designed for development rather than production, this shouldn't be an issue.
I was also looking for some solution to do this but currently, there is no such option available. Check out this GitHub issue:
https://github.com/microsoft/WSL/issues/4619
One solution can be this:
If you have the IP of windows(host) then it will do the job but the only problem is that IP will change every time. But, WSL2 stores your windows(host) IP in /etc/resolv.conf file. So we can modify our etc/hosts to map winhost to the IP dynamically.
Add the following lines at the end of ~/.bashrc file. This will grep the IP and modify the etc/hosts when you boot the WSL.
export winhost=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')
if [ ! -n "$(grep -P "[[:space:]]winhost" /etc/hosts)" ]; then
printf "%s\t%s\n" "$winhost" "winhost" | sudo tee -a "/etc/hosts"
fi
then run the following command to reload the changes.
$ source ~/.bashrc
now you can use winhost instead of localhost in WSL2(client) to access the servers running windows(host). In your case, it will be winhost:4502 but this will work for any other use cases as well like accessing Postgres/MySQL server running on windows, etc.
NOTE: Always remember to configure your firewall on windows to allow those ports so that WSL2 can access otherwise your connection may get blocked by firewall.
You need add ipv6 rule in hosts file.
Like:
127.0.0.1 example.com
::1 example.com localhost
I had a similar problem and found the solution at this link:
https://github.com/microsoft/WSL/issues/5728
This is simple, you just need to do 2 things. set ip address to localhost in hosts file and allow windows firewall to allow incoming connections.
Here are steps by step instructions:
Get IP address, open cmd type ipconfig /all, note down ip address 172.22.16.1
Open notepad as administrator, File->open , browse to C:\Windows\System32\drivers\etc\hosts , add line 172.22.16.1 localhost, save and close.
Last option is to allow Firewall rules to accept public connections, for this open control panel > System and Security > Windows Defender Firewall > Advanced Settings > Inbound Rules > 'New Rule`
Rule Type : choose Port , click next
Protocol and Ports : type *Specific local port: 4502 , click next
Action : Allow the connection , click next
Profile : check Public , click next
Name : type AEM server , click finish
close other windows, again open cmd , type wsl --shutdown for shutdown ubuntu subsystem.
open ubuntu again.
testing connection type curl localhost:4502 in command prompt. if it returns something means you are good to go.
The easy way:
in ubuntu command line type: ip route
default via 172.23.96.1 dev eth0
172.23.96.0/20 dev eth0 proto kernel scope link src 172.23.97.122
In your app, config the host to: 172.23.97.122
Now you can access using Windows Browser
This really belongs on another forum, but I suspect you could do this.
Step 1. Run your AEM server on your actual IP address (or all addresses) instead of binding to just localhost.
Step 2. Your Ubuntu WSL client instance won't share localhost with the host machine, but it will share the local IP address your machine actually has (e.g. 10.0.0.2). Just have your client connect to that IP address instead of localhost.
I don't know anything about AEM, but you might need to secure it if you are exposing it to a larger network. Block port 4502 on the upstream firewall or use a Windows Firewall rule to only allow incoming on port 4502 from your own IP address. This certainly appears possible.
Windows 10 considers localhost as ::1
Ubuntu considers localhost as 127.0.0.1
So solution is to create a mapping
create a file called .wslconfig in C:\Users<your_username>.wslconfig
add the following to it.
[wsl2]
localhostForwarding=true
PS: I dont know if it mess up anything else but it helped me run django servers which were broken after upgrading to wsl2

How to access vhosts via IPad?

I'm using XAMPP, on Mavericks, to simulate a localhost. I set jobeet.local, for example, as my vhosts. I tried to access it on my computer, it worked as well.
Now, I want to test my website on an IPad. So, I did the following steps:
Run XAMPP server on port 80, as usual
Connect the IPad to my wifi network.
Setup Http Proxy on Ipad, as picture showed below but change the port to 80
Now, I tested to access 127.0.0.1 via IPad. It worked! However, jobeet.local doesn't work.
I have searched and tried many ways to achieve this issue but they don't work.
Could you help me figure it out?
Thanks in advance.
You might have to update the hosts file.
The hosts file is a text file that maps hostnames to IP addresses.
Upon typing a url address on the browser, the system is checking if there is a relevant entry on the hosts file and gets the corresponding IP address, else it resolves the IP via the active connection’s DNS servers.
The hosts file can be edited to block certain hostnames (like ad-serving/malicious hosts), or used for web development purposes, i.e. to redirect domains to local addresses.
Editing the hosts file
Editing the hosts file in Mac OS X – Leopard, is a pretty easy task, especially if you are familiar with the terminal.
Step 1 – Open the Terminal.app
Either by start typing Terminal on the Spotlight, or by going into Applications -> Utilities -> Terminal.
Step 2 – Open the hosts file
Open the hosts by typing on the Terminal that you have just opened:
$ sudo nano /private/etc/hosts
Type your user password when prompted.
Step 3 – Edit the hosts file
The hosts file contains some comments (lines starting with the # symbol), as well as some default hostname mappings (e.g. 127.0.0.1 – localhost).
Simply append your new mappings underneath the default ones. Or edit one of the default values if you know what you are doing!
You can navigate the file using the arrow keys.
Step 4 – Save the hosts file
When done editing the hosts file, press control-o to save the file.
Press enter on the filename prompt, and control-x to exit the editor.
Step 5 – Flush the DNS cache
On Leopard you can issue a simple Terminal command to flush the DNS cache, and have your host file changes to take immediate effect:
$ dscacheutil -flushcache
You can now test your new mapping on the browser!
Hope this works for you!
I had to reinstall my setup today and made a step by step at that occasion:
I use a combination of Squidman and Mamp Pro (I assume it's similar to XAMPP)—hope this help anyone in their quest; happy to read feedback or get advice to make this better...
Squidman http://squidman.net/squidman/index.html
Mamp Pro http://www.mamp.info/en/mamp-pro/
on Squidman
- Preferences > General > Http port:
something different from the one MAMP is using (if map uses 80, then put 8080)
Preferences > Clients > Provide proxy service for:
insert the IP address or the subnet we will be catering for
Start Squidman
on MAMP:
setup the hostname, as well as the the website attached
insert the (local) IP address of the local machine
insert the port of Apache on the local machine
start Mamp; check that website is running correctly on local machine
on iPad/iPhone/mobile device
select the same wireless network as the laptop
in Wifi network > Preferences/Information: set a Manual HTTP Proxy
server IP is the (local) IP address of the Apache server, also running Squidman; port is the port used for Squidman
(Extra)
stuff to test: move dev server onto virtual machine (VirtualBox), to use with Node, custom PHP build, etc.
automate the setting: write pref for Squidman, Mamp/hostname, restart the Apache server, send configuration of proxy to mobile via iMessage or email.
I recommend using http://xip.io/. For example (taken from the website):
10.0.0.1.xip.io resolves to 10.0.0.1
www.10.0.0.1.xip.io resolves to 10.0.0.1
mysite.10.0.0.1.xip.io resolves to 10.0.0.1
foo.bar.10.0.0.1.xip.io resolves to 10.0.0.1
It does the job and you don't have to set anything up. I'm only pushing it because I am overly excited that I have wasted time trying over-engineered solutions.
On a mac you can use Squid on Windows that's Fiddler, however I'm having troubles with Fiddler and iPhone at the moment...

xampp not responding to external request

i am trying to run xampp on the internal network but it is not responding
could anyone help me with it
Here are things you can check:
Is it running at all? You might have some other application running on port 80 (default if you haven't changed anything)
Can you access it on the same machine it's running? Open your browser and go to http://localhost/ - if it works, then it's a communication issue between the machines
Can you see anything in the logs?
Do you have any firewall running on the machine running XAMPP? If so, turn it off temporarily and see if it works - you should configure it properly and configure it afterwards
You should check the FAQs:
depending on the OS you are running it on.

localhost host not working on XAMPP

I am facing same problem to the thread. I've gone through all the suggestions provided in the solution. I've searched all possible solution but nothing gonna workout.
"http://stackoverflow.com/questions/817745/localhost-not-working-on-xampp-both-service-apache-mysql-are-fine"
I've ensured :
1. Xampp is successfully started (in log) but it is not receiving any request(checked access.log).
also ensured that it is running on port 80 in config file.
confirmed status of application using netstat
C:\Documents and Settings\user>netstat -aon | grep 80
TCP 0.0.0.0:80 0.0.0.0:0 LISTENING 3524
Checked host file in /windows/system32/drivers/etc/hosts and has only one entry
127.0.0.1 localhost
skype teamviewer or other application are closed and configured not to use port 80.
ensured that windows firewall allows http process to use port 80.
pinged localhost, 127.0.0.1 and my ip (192.168.1.2). localhost resolves to 127.0.0.1
tried different urls http:// localhost http:// localhost:80 http:// 127.0.0.1 http:// laptop-name but nothing works. browser just waits for response (spaces after http to allow links in this post)
ensured that there is no loop in index.php(it is untouched) and also has index.html. also tried diffent paths
nothing gonnna work out. I am irritated and I dont want to format my machine. please help....
when this happens to me it is usually Skype taking over the default localhost address
Check on command prompt
netstat -a -b
And close any other application like skype etc using 80 port.
Try stopping IIS. It worked for me.
Select Start, then type IIS in the search box
select Internet Information Services Manager
In the manager on the right under "Manage server"select "stop"
I was having an issue with Last.fm that is very similar to yours, and I found that it was a software conflict.
The Last FM scrobbler application would not finish installing on my new build because it couldn't login or authenticate my username. I tried the regular scrobbler application and then tried the beta desktop program on Windows 7 64 bit. I suspected Skype or TeamViewer, but even after stopping or exiting those programs, neither versions of the installation would log in to the services.
The beta desktop application actually authenticated on 127.0.0.1 (random port) through my internet browser, but my browser would time out trying to send data over the port.
I checked and edited my HOSTS file in Windows, but that didn't fix the issue either.
Pinging 127.0.0.1 worked, but pinging any variation of the port would time out.
Finally, I decided to uninstall the Windows 7 Firewall Control program made by Sphinx Software. Mind you, I had set the firewall to be disabled at start up (clean start-up - all startup programs disabled through MSConfig).
I made sure that the firewall program was not running at all in my services. However, after uninstalling the software, even without restarting my computer, 127.0.0.1 opened up on all ports and my Last.fm scrobbler finally connected and finished installation.
So, it may be a hidden setting that can only be undone by uninstalling programs (even programs that aren't running).
For me localhost was not the correct address.
Type into cmd "hostname" and use that as the url.
CMD -> hostname
If that doesn't work try just the ip address of the server (or computer)
I stored my website folder in xamp/htdocs (windows machine). Then I started apache on the XAMP control panel. My pages rendered successfully when I called http://localhost/mysite/index.html.
I hope this is helpful.

Resources