workers-sdk
workers-sdk copied to clipboard
🐛 BUG: Wrangler redirects to https://www.localhost:8787
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.
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
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.
@isaac-mcfadyen To help us diagnose this further, would you be able to provide some more information about your setup? In particular:
- Your
wrangler.tomlfile, if you're able to share it. If you're not, just theroutessection would be helpful. - Does your origin server return a 301 redirect to
www? (i.e. if your origin server isexample.com, would accessingexample.comredirect towww.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 than0.0.0-893830aa
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
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 proxyhttp://0.0.0.0:8787tosubdomain.example.comrather thanexample.com -
In
wrangler.toml: Add the following section towrangler.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.
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.
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.
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
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.