workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

🐛 BUG: Wrangler redirects to https://www.localhost:8787

Open isaac-mcfadyen opened this issue 3 years ago • 8 comments

What version of Wrangler are you using?

0.0.0-893830aa

What operating system are you using?

macOS

Describe the Bug

When running wrangler dev (no args) Wrangler says it's running on http://0.0.0.0:8787 but upon requesting any path it returns a 301 redirect to https://www.localhost:8787.

I'm definitely not returning this redirect, this is Wrangler.

This does not happen using --local.

isaac-mcfadyen avatar Sep 11 '22 19:09 isaac-mcfadyen

Looks like the same issue in Wrangler v1 https://github.com/cloudflare/wrangler/issues/1784 And possibly the same issue here https://community.cloudflare.com/t/wrangler-dev-gives-me-a-301-redirect-to-my-sites-url/350919

caizixian avatar Sep 12 '22 12:09 caizixian

I have gotten this problem from versions 2.0.28, 2.1.1 and 2.1.4. I first had this on the 13th and since them am unable to run wrangler dev on any projects,

Running wrangler dev on my workers will direct all requests to the apex of my domain (bubblez.app.) This doesn't show up anywhere within my code so it can only be Wrangler doing the redirect.

addidotlol avatar Sep 17 '22 12:09 addidotlol

@isaac-mcfadyen To help us diagnose this further, would you be able to provide some more information about your setup? In particular:

  • Your wrangler.toml file, if you're able to share it. If you're not, just the routes section would be helpful.
  • Does your origin server return a 301 redirect to www? (i.e. if your origin server is example.com, would accessing example.com redirect to www.example.com?
  • The output of curl -I http://0.0.0.0:8787 (or whatever port Wrangler says it's running on) It would also be helpful if you could use the latest version of Wrangler (2.1.5), rather than 0.0.0-893830aa

penalosa avatar Sep 20 '22 15:09 penalosa

Sure!

  • Routes are: routes = { pattern = "subdomain.example.com/*", zone_name = "example.com" }. Workers.dev is disabled but there's nothing else affecting the routes.

  • My origin server does return a 301 from apex to www, but I'm also backing that with a Page Rule that does the same thing so that if I move servers the behavior remains:

Page Rule:
URL: example.com/*
Forwarding URL (Status Code: 301 - Permanent Redirect, Url: https://www.example.com/$1)
  • Output of curl -I http://0.0.0.0:8787:
HTTP/1.1 301 Moved Permanently
date: Tue, 20 Sep 2022 15:26:36 GMT
location: https://www.localhost:8787/
cache-control: max-age=3600
expires: Tue, 20 Sep 2022 16:26:36 GMT
x-robots-tag: noindex
server: cloudflare
cf-ray: 74db94958d8e5491-YYZ
alt-svc: h3=":443"; ma=86400, h3-29=":443"; ma=86400
Connection: keep-alive
Keep-Alive: timeout=5

isaac-mcfadyen avatar Sep 20 '22 15:09 isaac-mcfadyen

Ah okay—the issue is that you're trying to run your worker on subdomain.example.com/*, but wrangler dev runs locally, and so requesting http://0.0.0.0:8787 is looking for worker routes to run on your bare domain example.com. Since none exist, it's falling back to your origin (which returns a 301 redirect to www). In essence, it looks like what you want is a way to request the worker running on subdomain.example.com/* when you hit http://0.0.0.0:8787 (instead of the default, which is to request the worker running on example.com/*). There are two ways to achieve this:

  • In the CLI: Run wrangler dev --host subdomain.example.com, which will proxy http://0.0.0.0:8787 to subdomain.example.com rather than example.com

  • In wrangler.toml: Add the following section to wrangler.toml:

    [dev]
    host = "subdomain.example.com"
    

In this case, Wrangler will also autodetect the right configuration (with no extra changes to wrangler.toml or extra CLI args), if you swap out zone_name for zone_id (and set the value to your zone ID) in your route configuration.

penalosa avatar Sep 21 '22 13:09 penalosa

Thanks for the workaround!

I'd question why this is closed: if a route is on a subdomain and there's no Worker on the main domain then things should just "work".

In fact, the Wrangler.toml configuration example says this: # Host to forward requests to, defaults to the host of the first route of project

However that's not what's happening here: my first route is a subdomain but Wrangler is not picking up on this and things break. IMO users shouldn't have to look through Github issues to find the solution to a relatively common configuration.

isaac-mcfadyen avatar Sep 21 '22 13:09 isaac-mcfadyen

Host to forward requests to, defaults to the host of the first route of project

This is the behaviour, but only if zone_id is used rather than zone_name (i.e. routes = [{ pattern = "subdomain.example.com/*", zone_id = "12345..." }]. However, that's not super intuitive, and so I'll re-open this for discussion.

penalosa avatar Sep 21 '22 14:09 penalosa

After discussing this with the team, we're treating this as a bug, and will have a fix up shortly to handle zone_name the same as zone_id

penalosa avatar Sep 21 '22 15:09 penalosa

I'm testing on a cloned production website and I got this issue. I've found that even though example.com works fine (no 301 redirects), my page my-page.com redirects with 301.

The difference between these are that example.com has http protocol (which seems to be default for wrangler) and my page has https.

With that said, adding --local-protocol=https to my command fixed the issue. Will try to look for a prettier way, but so far this patch works.

aleksandras-va avatar Apr 20 '23 08:04 aleksandras-va