Troubleshooting “Localhost Refused to Connect” Within Minutes
"Hi, I am having this really annoying issue where I cannot get any app that I spin up to connect to the localhost port it is trying to run on. I have killed all ports before trying and the problem persists"
The localhost refused to connect error indicates the failure of the web browser to connect to the local server. It is a common network issue that significantly hampers the development and testing of locally-hosted web applications.
But there's nothing to worry about! Let us walk you through the most common causes of the "localhost refused to connect" error followed by various solutions to resolve it.
Quick Tips to Fix
Before proceeding, apply the following quick workarounds:
1. Use HTTP instead of HTTPS.
2. Clear browser cache.
3. Restart your computer.
4. Make sure your browser and server are updated.
5. Use another browser to try to connect to the local server.
If these solutions don't work for you, worry not. Below, you'll find advanced troubleshooting tips to fix the problem.
Advanced Solutions to Fix Localhost Refused to Connect
Solutions | Key Notes |
---|---|
The Apache server is active and running smoothly. | |
Verify that the server is configured to listen on the correct IP address and port. | |
Clear the DNS cache and lookup data. | |
Temporarily stop the firewall on your machine to stop blocking of a particular port. | |
Enable connection to sites without SSL certificates. |
Solution 1. Check Apache Server Status
The first thing you should verify the server process is running smoothly. If the Apache web server is not active, the user cannot access localhost. The command script is slightly different for Windows, macOS, and Linux machines, as demonstrated below:
For Windows:
- Press the Ctrl + Shift + Esc keys simultaneously and go to Task Manager.
- Here, type in Apache to confirm the service is running.
For Debian or Ubuntu
$ sudo systemctl status apache2
Note: Change "Apache2" with the exact name of your Apache service, if it's different.
For CentOS/RHEL/Fedora
# systemctl status httpd
If the result shows that your service is stopped, use the below commands to restart the settings:
For Debian and Ubuntu:
sudo systemctl restart apache2
For CentOS/RHEL/Fedora:
# systemctl restart httpd
If the output gives an error after restarting the command, try reinstalling the Apache services.
Solution 2. Inspect Port Configuration
Next, make sure you're accessing the correct port and IP address. as some services operate on specific ports, like port 80 for HTTP and port 443 for HTTPS.
Also, if some other service, like Skype, is currently operating on that port, it can create a conflict, resulting in "localhost 8080 refused to connect error." Below are the steps to verify and change the port settings in XAMPP on Windows, macOS, and Linux:
1. Launch XAMPP from the Start menu and click Netstat.
2. It shows all the ports on your machine. Confirm if the port 80 is free or not.
3. If it's already in use, choose a free port of your choice, like 8080.
To customize the settings to ensure a connection to the right port number:
1. Open XAMPP and click Stop next to Apache.
2. Now, go to File Explorer and find the httpd.conf file in:
C:\xampp\apache\conf for Windows
bin/apache for Linux
Applications/XAMPP/xamppfiles/etc/httpd.conf. for macOS
3. Open the file using Notepad and look for the text:
Listen 80
ServerName localhost:80
If another application is using it, replace it with any available port, like 8080.
4. After that, save the file and exit. It might require the Administrator approval to apply the changes.
5. Finally, open the XAMPP Control Panel and confirm if you can access http://localhost:8080 or https://127.0.0.1:8080/.
And that's how end the conflicting processes to resolve the "Docker localhost connection refused" error.
Solution 3: Flush DNS Cache
Your machine uses the DNS check to ensure the hostname is resolved to the appropriate IP address. Over time, it accumulates the DNS cache which can get corrupt or overloaded, causing the "SSH localhost connection refused" problem.
To fix this, try clearing the DNS cache and lookup data to confirm it isn't interfering with the local host connection. To do that:
For Windows:
Open Command Prompt and run the command:
ipconfig /flushdns
For Linux, run the below command:
sudo /etc/init.d/nscd restart
For macOS, run the below command in the Terminal:
$ sudo killall -HUP mDNSResponder
Solution 4: Disable Firewall Temporarily
The firewall configuration on your machine can block the traffic on a specified port, leading to the "net err_connection_refused localhost" error. To confirm this, temporarily disable the firewall settings:
For Windows:
- Head to Control Panel > System Security > Windows Defender Firewall.
- Select Turn Windows Defender Firewall on or off, and disable the Firewall option on the next screen.
For Ubuntu/Debian, run the command:
# sudo ufw disable
Now, confirm the status via the cmdlet:
# sudo ufw status
For CentOS/RHEL/Fedora, run the command
# sudo systemctl stop firewalld
Here again, check the status via the command:
# sudo systemctl status firewalld
For macOS:
- Head to System Preferences in the Apple Menu and select Security and Privacy > Firewall.
- Make sure the Firewall option is turned off.
Solution 5: Tweak Browser Settings
One reason for "This site can't be reached. localhost refused to connect" is misconfigured browser settings. It can occur that your browser is automatically redirecting the HTTP addresses to HTTPS. If that particular doesn't have a valid SSL certificate, it leads to "err_connection_refused localhost."
To fix this issue on Chrome:
1. Navigate to "chrome://net-internals/#hsts" from your browser.
2. Under the Delete domain security policies, type localhost.
3. Click Delete and restart the browser.
Understanding the Localhost Refused to Connect Error
The "Local host connection refused" is an error message that pops up after a failed attempt to access a webpage or app hosted on your local machine. Depending on the reason causing the issue, it can appear in several types, such as:
- ERR_CONNECTION_REFUSED
- This site can't be reached localhost refused to connect
- Docker localhost refused to connect (when you're using the Docker toolkit on Windows)
What Causes This Issue
Understanding the potential reasons for "Local host connection refused" is the first step in resolving the issue. Based on our research, the key factors contributing to this issue include:
1. Inactive Service: If the local server you're trying to connect is not currently active, it cannot accept the incoming connection requests.
2. Incorrect URL or Port Number: Be sure to use the right port number, which by default is port 80 for HTTP.
3. Firewall Restrictions: Security restrictions or firewall settings on the user's machine might block the incoming connections. It's important to ensure the firewall is not blocking the target ports.
4. Corrupt Browser Cache: Corrupt browser cookies can lead to "localhost refused to connect on Chrome." It's best to clear the browser cookies regularly.
5. Network Issues: Users need to ensure a seamless network connection and resolve any issues, especially with DNS Resolution.
6. Service Conflicts: If other services are using the same port, it can lead to net err_connection_refused localhost. Try to diagnose and terminate the conflicting processes.
7. Software Update Issues: Outdated server software or your browser can also cause the "localhost refused to connect" issue.
Bonus Tip: Efficiently Control Unattended Devices in Large Number
If you want an instant remote support application to manage personal or business tasks efficiently, try AirDroid Remote Support. It is perfect for providing remote IT support, instructions, or customer service, as well as managing a large number of devices from anywhere.
The admin has the authority to assign various levels of access management on the devices for different users. Moreover, you can force a black screen on the controlled device when performing a task to ensure complete privacy from passersby.
As it comes with a free 14-day trial, you can easily test the efficiency of its stellar remote access and management features.
FAQs
Q1: What is an alternative to localhost?
One good alternative to localhost is using your own computer's name and IP address as the host. Get the IP address of your machine y executing the ipconfig command in the Command prompt
Q2: Apache server is running but I still encounter the localhost refused to connect error. Why?
If the Apache server is running, make sure you're accessing the right port number and IP address. Also, check the browser's settings to confirm it's not redirecting the HTTP traffic to HTTPS.
Final Remarks
This article has outlined detailed instructions for resolving the "Localhost refused to connect" error. It's crucial to understand the underlying issues resulting in this error, before applying the solutions. Hopefully, it helps you effectively diagnose and fix the connection problem to resume the work without any hindrance.
In addition, we talked about a perfect remote support application to boost collaboration, productivity, and efficiency of your administrative tasks.
Thanks
Leave a Reply.