117

How do I allow any device, e.g., iPhone, to connect over a WLAN to my Mac's localhost server?

On my Mac, I'm running a "Hello World" HTTP Node.js server that serves a page, which Safari opens successfully, at http://localhost:1337. And, running ipconfig getifaddr en1 in Terminal outputs 192.168.1.9.

But, Safari, on both iPhone & Mac, displays "Safari can't connect to the server" when it tries to open http://192.168.1.9:1337.

I don't think this should involve port forwarding because I only want the HTTP server to be available privately, not publicly.

Related:

Community
  • 1
  • 1
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
  • 1
    question answered here http://stackoverflow.com/questions/9304058/how-to-view-localhost-on-my-ipod-touch – Andreas Norman Aug 07 '13 at 15:20
  • I was surprised not to see an answer like this one, which worked for me: http://stackoverflow.com/a/41857012/470749 – Ryan Jan 25 '17 at 16:44

14 Answers14

69

I had the same problem. I turned off my WI-FI on my Mac and then turned it on again, which solved the problem. Click Settings > Turn WI-FI Off.

I tested it by going to Safari on my iPhone and entering my host name or IP address. For example: http://<name>.local or http://10.0.1.5

johnnieb
  • 3,982
  • 4
  • 29
  • 32
  • strangely enough, this worked for me! Thanks a lot. I can't understand why. – Björn Grossmann Oct 10 '14 at 22:44
  • 5
    plus one for `.local` - No more static IPs for testing! – Adam Carter Nov 24 '15 at 18:30
  • I spent a whole day trying to figure this out. Toggling my Mac's WiFi on/off fixed it. Weird. – oif_vet Dec 14 '15 at 18:38
  • Toggling Mac wifi also fixed it for me. I wonder if this is a Mac problem or a Router problem, or a nodejs problem. Any ideas? – neoneye Feb 15 '16 at 18:44
  • strange thing… worked for me. thanks! – trnc Apr 06 '16 at 11:49
  • What is even stranger is that turning wi-fi off through top nav bar didn't work (right clicking on wi-fi icon and then selecting Turn Wi-Fi Off), but doing this through Settings did work!!! – smohadjer Aug 19 '16 at 11:40
  • Does not really work for me. I restarted wifi like 20 times =( But right after i connect using a cable everything worked =) – Zhen Liu Oct 30 '16 at 18:08
  • Thank you! And make sure you don't have your iphone connected to your mac when you try this :S – vaskort Dec 03 '17 at 14:56
  • 5
    http://.local worked for me. To expand on the answer the of your mac can be found in **System Preferences > Sharing**. Underneath your **Computer Name:** you can find **Computers on your local network can access your computer at: .local** – John Lay Dec 15 '17 at 12:57
  • Ip didn't work for me, but computer name did. Thank you! – meerkat Sep 14 '19 at 15:38
  • How strange. I have to turn off Wifi and turn on again, then it worked. Thanks a lot. – Son Tr. Apr 28 '20 at 14:11
  • Remember to specify the port . If you're running a server at port `8000`, the URL should be e.g. `http://10.0.1.5:8000` – Ben Butterworth Aug 28 '21 at 21:16
  • Thanks!!! I've used this method for external device testing for ages, only today did it stop working. I never thought to turn the wifi off and on again on my mac (face palm). – Harry Mar 17 '22 at 21:22
63

Have your server listen on 0.0.0.0 instead of localhost.

ma11hew28
  • 121,420
  • 116
  • 450
  • 651
  • 2
    Ahhhh thank you this worked for me – aks. Oct 16 '16 at 22:07
  • 2
    For anyone having issues with the other answers - give this a try! – Trent Dec 28 '16 at 21:07
  • 6
    Alternatively, you can specify '0.0.0.0' to bind directly to local ip. Will prevent issues if you switch networks and are not assigned the same ip. – Cole Murray Jan 22 '17 at 23:04
  • 2
    Sorry but how do you "have your server listen on" exactly? – gmarais May 21 '20 at 08:47
  • @ColeMurray, sorry for the delayed response. Thank you for your suggestion. I just updated my answer. – ma11hew28 May 21 '20 at 13:17
  • 2
    @gmarais, that depends how you're building your server. In this [example Node.js application](https://nodejs.dev/learn#an-example-nodejs-application) you'd change `127.0.0.1` to `0.0.0.0`. – ma11hew28 May 21 '20 at 13:24
  • Thanks @ma11hew28. I am hosting a website using Django/Python. Also, I am noob, but have since learned that you can add ip addresses to achieve your "have your server listen.." advice. Thanks again – gmarais May 21 '20 at 13:42
  • 1
    You're welcome, @gmarais. :-) – ma11hew28 May 21 '20 at 14:13
34

I was only able to access my iphone using http://name.local:1337. You have to add the ".local" You can find your computer name under System Preferences/sharing/"Computer Name".

moeamaya
  • 349
  • 4
  • 5
  • I tried using the IP address with and without firewall and many other solutions suggested and this is the only one that worked. I am running a localhost site on port 3000 with express using node js. – lostAtSeaJoshua Apr 06 '17 at 17:13
  • You can also find your "name" by typing `whoami` into the console. – Jadam Mar 26 '18 at 17:21
  • 2
    @Jadam This technique doesn't work for me. I get two different names. For a local server situation, the "Computer Name" is the one that works. – moeamaya Apr 06 '18 at 22:37
  • I can access the default localhost site using this method, but does anyone know how to access virtualhost domain names via the device? Ex: `http://vhost-servername.local:1337/` – TheKarateKid Apr 23 '18 at 18:40
  • 1
    For me, I never got anywhere using name.local, however using the actual IP address worked. I would say troubleshoot using specific actual IP addresses as opposed to the convenience of something like name.local which just adds another layer of complication. – Kalnode Jul 14 '18 at 18:20
  • 1
    This should be the accepted answer. It requires no additional setup or modification to current Node.js server file. @ma11hew28 – BenS Jul 18 '18 at 12:01
17

Try enabling Internet Sharing:
Open System Preferences -> Sharing. Check Internet Sharing to turn it on, it will prompt you to confirm your action, select ok. If your iPhone is connected using USB, the iPhone USB is checked at the "sharing your connection" list on the right side.
After this, try accessing your local server using your macs ip on wifi.

matthias_h
  • 11,356
  • 9
  • 22
  • 40
maqbulh
  • 179
  • 1
  • 2
17

My problem was the same, but the solution had to do with changing a firewall setting. It turned out that node was set to block incoming traffic. I knew something was up because I used to be able to get to my localhost server from my phone.

Go to System Preferences -> Security & Privacy -> Firewall -> Firewall Options, and then scroll down until you find node and make sure node's setting is set to Allow incoming connections.

After changing the setting, I could reach the localhost server (port 3000) running on my Mac by going to http://192.168.1.11:3000

David Rajcok
  • 186
  • 1
  • 5
7

I solve a similar problem.

  • connected Mac and iPhone to the same Wi-Fi
  • change the iPhone Wi-Fi setting, set http proxy to manual and change the Server to you Mac ip address and setting the Port. My Port is 80.

image one

image two

  • you can input http://<Mac ip>:<your customer server port> in iPhone's safari
Avag Sargsyan
  • 2,437
  • 3
  • 28
  • 41
Sinter
  • 71
  • 1
  • 3
6

MacOS Sierra users can find their auto-generated vanity URL by going to System Preferences > Sharing and checking beneath the Computer Name text input. To access it, enter this URL, plus your port number (e.g. your-name.local:8000), on your iPhone over the same Wi-Fi connection as your computer.

brogrammer
  • 804
  • 1
  • 12
  • 22
5

make sure you phone and mac machine both connected to the same wifi and you good to go your http://<machine-name>.local

Muhaimin
  • 1,643
  • 2
  • 24
  • 48
5
  1. Connect your iPhone to your Mac via USB.

  2. Go to Network Utility (cmd+space and type "network utility")

  3. Go to the "Info" tab

  4. Click on the drop down menu that says "Wi-Fi" and select "iPhone USB" as shown here:

    Photo for Step 4

  5. You'll find an IP address like "xxx.xxx.xx.xx" or similar. Open Safari browser on your iPhone and enter IP_address:port_number

    Example: 169.254.72.86:3000

[NOTE: If the IP address field is blank, make sure your iPhone is connected via USB, quit Network Utility, open it again and check for the IP address.]

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
arjunkm
  • 471
  • 6
  • 12
3

I suggest to use the name of the computer, e.g.http://mymac:1337/. Works for me perfect without any configuration required and I don't have to care about changing IP addresses due DHCP.

stiebitzhofer
  • 314
  • 2
  • 12
3

As posted I followed moeamaya's answer but needed to modify it just a bit to see my work in the Sites directory.

http://[name].local.~[username]/

[name] is as stated already (System Preferences/sharing/"Computer Name")

[username] is found at:

/etc/apache2/users/username.conf

hope this helps!

naiJenn
  • 91
  • 1
  • 7
2

Tried everything on this page, but http://<name>.local:<PORT> only worked on my iPhone after I quit and restarted Safari...

physicsAI
  • 73
  • 2
  • 6
1

Always use the IP that is shown in your Network settings. It changes when you change location and you use another wireless connection.

For example in my case now it is: 10.0.0.5

Edgaras
  • 11
  • 3
  • You can try, but it won't work in all circumstances. You've made an assumption that I don't see in the question, about the network configuration. – benc Jan 12 '19 at 02:10
  • Well, this assumption helped me to solve my similar problem yesterday. I could not find the IP to connect. Terminal commands did not work. What worked, was the IP in Network settings. (I am running apache local server on mac) – Edgaras Jan 13 '19 at 06:01
-2

also, make sure the server is listening for ur mobile device! for example, by default jekyll only answers requests made by the host machine. this solved my problem:

Connect to a locally built Jekyll Server using mobile devices in the LAN

Tom
  • 1