4

I have a Microsoft Azure Function App 2.0 preview written in C# which binds to http://localhost:7071.

enter image description here

How do I get it to bind to 0.0.0.0 instead of localhost so I can access it from another machine on my local network?

Looking at these Microsoft Docs I can see how to specify the port in the local.settings.json file "Host": { "LocalHttpPort": 7071, "CORS": "*" }

But not which Network Interface to use.

Coming from a Rails background I would do this with: rails s -b 0.0.0.0

How do I do this in the .net / Azure Function world?

worldofchris
  • 493
  • 6
  • 19
  • You should be able to access it from the other machine by IP address - https://stackoverflow.com/questions/19482164/how-can-i-access-localhost-from-another-computer-in-the-same-network – stuartd Mar 28 '18 at 16:26
  • Thanks I’ve tried this. Even tried using the IP address to access it from the local machine but it’s not worked. I have Windows Firewall switched off too. – worldofchris Mar 28 '18 at 17:24
  • There's always [ngrok](https://ngrok.com/) – stuartd Mar 28 '18 at 17:29
  • If anyone came after http://0.0.0.0:7071 did not work just use http://localhost:7071 – dejjub-AIS Jan 09 '20 at 09:45

1 Answers1

6

UPDATE —

You don't need this sorcery anymore as the functions host now listens on 0.0.0.0:7071 by default:

$ func -v
2.0.1-beta.33

$ func | grep Runtime
Function Runtime Version: 2.0.11933.0

$ func host start | grep -i listen
Listening on: http://0.0.0.0:7071

NOW DEPRECATED —

If you're on Windows,

C:\>netsh interface portproxy add v4tov4 listenport=8081 ^
        listenaddress=0.0.0.0 connectport=7071 connectaddress=127.0.0.1


C:\>netsh interface portproxy show all

Listen on ipv4:             Connect to ipv4:
Address         Port        Address         Port
--------------- ----------  --------------- ----------
0.0.0.0         8081        127.0.0.1       7071


C:\>netstat -an  | findstr 8081
  TCP    0.0.0.0:8081           0.0.0.0:0              LISTENING

C:\>netstat -an  | findstr 7071
  TCP    127.0.0.1:7071         0.0.0.0:0              LISTENING

To test it out:

C:\>curl.exe http://192.168.111.4:8081/api/HttpTrigger -i
HTTP/1.1 200 OK
Date: Wed, 28 Mar 2018 18:25:31 GMT
Content-Type: application/json; charset=utf-8
Server: Kestrel
Transfer-Encoding: chunked

"Oh hai."

To get rid of portproxy:

C:\>netsh interface portproxy delete v4tov4 listenport=8081 listenaddress=0.0.0.0

On Linux, iptables your way through it, on a Mac... i don't know, nginx your way out? Okay, there seems to be a pfctl tool, probably inherited from BSDs?

Although Kestrel is the server, setting the environment variable ASPNETCORE_URLS="http://*:7071" has no effect. The function host is probably bootstrapped with localhost in code, which may be a hint that you're not supposed to expose it naked and you should reverse proxy into it.

evilSnobu
  • 24,582
  • 8
  • 41
  • 71