Using caddy with ngrok: cannot connect to localhost

I’ve started to use Caddy with the ngrok.io tunnel server service (https://ngrok.io/) that allows a site running on localhost:8080 to be accessible from a custom URL.

I create the tunnel using:

ngrok http localhost:8080

The Caddy server is running on localhost and port 8080. These are the pertinent lines from my Caddyfile:

localhost:8080

However, when I try to connect using the URL given to me by the ngrok service, I get the following reply from Caddy when I try to access the web root:

HTTP/1.1 404 Not Found
Content-Type: text/plain; charset=utf-8
Server: Caddy
X-Content-Type-Options: nosniff
Date: Tue, 28 Mar 2017 17:24:11 GMT
Content-Length: 21
No such site at :8080

What is going on here, and why can’t I access localhost using the webroot?

By the way, the same return code from the server (“No such site at :8080”) is also returned when I use the localtunnel.me service.

I am running Linux on my development machine (Linux Laura 4.4.0-67-generic #88-Ubuntu SMP Wed Mar 8 16:34:45 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux).

Probably because the Host header of the request isn’t “localhost”. Remove that from your site definition in the Caddyfile and try again.

2 Likes

Matt, thank you for your response.

In my Caddyfile, I tried to replace the line

localhost:8080

with

[ip address]:8080

where [ip address] is my current IP address, but this did not work.

Could you clarify how I can remove the “localhost” from the site definition? I have also tried 127.0.0.1, but the same problem is apparent. That is, in my Caddyfile:

127.0.0.1:8080

This is the only line that I have in my file for the site definition. Is something else required?

try with just

:8080 

this will respond to any requests on port 8080

3 Likes

Great - thanks so much Toby and Matt for the quick response. This works well!

Thanks again!

1 Like

Yeah, like literally remove it, don’t put any host in there…

1 Like

Yes, this works well - thanks Matt!

Oops, I saw after the fact that Toby already answered perfectly! Stupid internet connection.

To shed some light on why that happened:

When a client accesses Caddy via yourapp.ngrok.io, Caddy will look for server configuration for yourapp.ngrok.io, which it doesn’t have (because it was configured for localhost, then your IP address).

By changing the Caddyfile to :8080, Caddy will serve that configuration to any host, localhost, your IP, example.com, or ngrok.io, because it is essentially a wildcard host. In this case, using yourapp.ngrok.io:8080 would also work, but then you would get 404s if you tried to access it locally in the browser via localhost:8080.

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.