Page rule redirect with port

After checking out the forum and doing some research i figured out that it’s not possible to set up dns with port i.e something ip:port…

So i used a page rule which i have mentioned below:

http://admin.domain.com/*
Forwaring URL - Permanent Redirect 301
to http://admin.domain.com:3000

It seems not to be working while http://admin.domain.com:3000 works as i have already set up the dns.

Not sure if i was able to explain it properly but if anyone gets my point any further assistance will be really appreciated. :slight_smile:

Review the documentation on what ports are supported by Cloudflare.

Is port 3000 the only port in use on your origin?

Is admin.example.com :orange: or :grey: in the DNS settings?

1 Like

According to the documentation it seems like 3000 is not supported. But i just want it to get redirected to :3000, is it not possible?
Is port 3000 the only port in use on your origin? - Sorry, but didn’t get this part.
Is admin.example.com :orange: or :grey: in the DNS settings? It’s :grey:

No, it is not possible unless you are on an Enterprise plan. If that hostname only works on port 3000, you can not use Cloudflare Page Rules because Cloudflare will not proxy Port 3000 traffic.

EDIT: Hang on…it might be possible to use Workers to pull from Port 3000 on your server. Digging around now…

For page rules to work the host needs to be :orange:. If you make it :orange: now, the redirect page rule will work, but you will not be able to reach port 3000.

Do you need the redirect just as a convenience (so you don’t have to type :3000 every time) or something else?

Are you unable to change the listening port?

Do you have other applications running on admin.example.com, or is it always admin.example.com:3000?

@sdayman I’ve never used Portzilla, but is it an option here?

1 Like

Portzilla is the type of function I was thinking of. @matteo gave me this Worker script once, but it’s been a long time since I tried it:

async function changePort(request) {
  var newURL = new URL(request.url)
  newURL.port = '2096'

  return fetch(newURL, request)
}

EDIT: p.s. Portzilla has a known issue about routing from the root directory. Matteo’s might work better, but they’re both worth a try.

Indeed, it’s just to make it more convenient and easy to remember so that peoples don’t have to come up and check what exactly to type to go to the admin panel.
I’m using npm for the port 3000 , checking out if it’s possible to change the port then i can assign a port that’s supported by cloudflare.
Nah, nothing else running in admin.example.com, infact it’s not even reachable without 3000.

Checked out Portzilla just few mins before (comes with a free package aswell) but while setting it up it showed it will be installed as a whole thoughtout my domain so didn’t take a step forward.

Will try it if i’m not able to change the port.
Really appreciate it for such quick reply.

1 Like

Mine seems to work just fine for all subdirectories and roots, but one might not work. I still have that code running and I haven’t switched to Portzilla, for example.

1 Like

Changing to a standard port is the best option, as you can then get the protection offered by being :orange:

Failing that, you could use a page rule on another host that is :orange:, something like example.com/admin* ->
http://admin.example.com:3000/

Also you should strongly consider HTTPS, especially for something with ‘admin’ in the name!

1 Like

In my case I can’t move the port (it’s some random 54xxx port assigned to me), but doing the above I get HTTPS to the origin if supported and HTTPS to the client via the standard 443 port. You could even handle it on a specific subdirectory with Workers based on the origin’s port (if you don’t want to have a web server on the server itself).

I got it working on port 8080 which is supported by cloudflare.
So many options above i’m confused now :smiley:
So now the best and easiest will be just to port forward it right?

The quickest and cheapest way is to simply change the port the service runs on (ideally using one of the HTTPS ones both Cloudflare and server side), failing that (or in case you have many services) run a web proxy on the origin, which connects locally (even in plain HTTP) to the various services on subdirectories (e.g. Caddy, nginx, Apache, etc.) and proxies them via port 443 with HTTPS to Cloudflare from which you are connecting to.

Failing all on the server you can either use Workers or the Portzilla app mentioned above to do the same albeit with a bit of a cost to run Workers.

I changed the port. I got the service running on 8080.
Then in created a new A record = admin with the IPv4 address followed by SRV record admin.example.com with the port 8080.

Finally in Page rule setup -
http://admin.example.com/*
Forwaring URL - Permanent Redirect 301
to http://admin.example.com:8080

But it seems not to be working.

First of all you should check without redirects if the actual admin page is reachable inputting the direct address with the port specified. What error does that give out? Is the record actually proxied (as :orange:)?

Yeah, the actual admin page i.e http://admin.example.com:8080 is accessible with :orange: . I’hve turned off the page rule for now.

Edit: No it’s now not accessible http://admin.example.com:8080 - shows Error 521 Web server is down

Then the service it’s not actually running at port most likely. By doing direct-io:8080 works?