Publish a Service with Cloudflare Tunnel
Publish an application hosted on Debian or Ubuntu without opening any inbound port using Cloudflare Tunnel, protect an admin interface with Cloudflare Access, then update and back up.
On this page
Your values
Enter them once and the commands in this guide update.
This guide explains how to publish an application hosted on your server without opening any inbound port. The cloudflared program establishes an outbound connection to Cloudflare. Visitors arrive at https://app.your-domain.com, and Cloudflare handles the certificate and forwards requests through this tunnel.
By the end of the guide, the application keeps listening locally, the tunnel starts with the server, and you know how to restrict a sensitive interface to identified people.
Order a Server
This guide applies to servers that you administer yourself:
Prerequisites
- A server running Debian 12 or 13, or Ubuntu 22.04 or 24.04
- SSH access with a user that has
sudo - A Cloudflare account
- An active domain on that account
- An application that listens locally, for example on
127.0.0.1:3000
Add the Domain to Cloudflare
If the domain is not on Cloudflare yet:
- Add it from the dashboard, then choose a plan.
- Check the imported DNS records. Pay particular attention to the MX records and those used for email, because the automatic scan can miss some.
- Disable DNSSEC at your registrar if it is active.
- Replace the domain's name servers with the ones Cloudflare gives you. For a domain managed at HostMyServers, see the DNS Server guide.
Ports Used
| Port | Direction | Purpose | Action |
|---|---|---|---|
| 7844/tcp and 7844/udp | Outbound | Tunnel connection to Cloudflare | Allow if outbound traffic is filtered |
| 443/tcp | Outbound | Secondary functions, such as checking for updates | Optional |
| 3000/tcp | Local | Example application | Do not open |
| None | Inbound | The tunnel needs no inbound port | Nothing to open |
Step 1: Check the Application
The application must respond on the loopback interface:
curl -I http://127.0.0.1:3000
sudo ss -ltnp | grep 3000The listening address must be 127.0.0.1. If the application runs in Docker, publish its port as 127.0.0.1:3000:3000. A 3000:3000 publication would remain reachable from the Internet, because Docker inserts its own firewall rules.
Step 2: Install cloudflared
Add Cloudflare's official repository, then install the package:
sudo mkdir -p --mode=0755 /usr/share/keyrings
curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-main.gpg >/dev/null
echo 'deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared any main' | sudo tee /etc/apt/sources.list.d/cloudflared.list
sudo apt-get update
sudo apt-get install -y cloudflared
cloudflared --versionStep 3: Create the Tunnel
Authenticate cloudflared. The command prints a URL to open in your browser, where you choose your domain:
sudo cloudflared tunnel loginThe account certificate is saved to /root/.cloudflared/cert.pem. Then create the tunnel:
sudo cloudflared tunnel create hms-tunnel
sudo cloudflared tunnel listNote the identifier displayed, called TUNNEL_UUID here. Copy its credentials file to the folder used by the service:
sudo install -d -m 700 /etc/cloudflared
sudo install -m 600 /root/.cloudflared/TUNNEL_UUID.json /etc/cloudflared/TUNNEL_UUID.jsonStep 4: Configure the Tunnel
sudo nano /etc/cloudflared/config.ymltunnel: TUNNEL_UUID
credentials-file: /etc/cloudflared/TUNNEL_UUID.json
ingress:
- hostname: app.your-domain.com
service: http://127.0.0.1:3000
- service: http_status:404Rules are read in order. The last one, with no hostname, is mandatory: it answers 404 to any request that matches no service.
Check the file and the routing of a URL:
sudo cloudflared tunnel --config /etc/cloudflared/config.yml ingress validate
sudo cloudflared tunnel --config /etc/cloudflared/config.yml ingress rule https://app.your-domain.comStep 5: Protect an Admin Interface
Skip this step only if the service is meant for the public and has its own authentication. For an admin interface, set up the protection before publishing the name: without an Access application, the address is open to the whole Internet.
In the Cloudflare dashboard:
- Go to Zero Trust > Access controls > Applications.
- Select Create new application, then Self-hosted and private.
- Select Add public hostname and enter
app.your-domain.com. - Under Access policies, create a policy with the Allow action and an Include rule of type Emails, containing your address.
- Choose the login method. If you want to receive a code by email, add One-time PIN under Zero Trust > Integrations > Identity providers.
- Select Create.
Labels may change: if they differ, follow the Cloudflare Access documentation.
You can also have cloudflared verify the Access token. The Application Audience (AUD) Tag is found under Additional settings for the application. In config.yml, complete the relevant rule:
ingress:
- hostname: app.your-domain.com
service: http://127.0.0.1:3000
originRequest:
access:
required: true
teamName: TEAM_NAME
audTag:
- AUD_TAG
- service: http_status:404TEAM_NAME is the name of your Cloudflare One team, the part that comes before .cloudflareaccess.com. Then run the validation from step 4 again.
Step 6: Publish the Domain Name
Create the DNS record that points to the tunnel:
sudo cloudflared tunnel route dns hms-tunnel app.your-domain.comIf you manage several domains, check in the dashboard that the CNAME record was created in the right zone. It must point to TUNNEL_UUID.cfargotunnel.com.
Step 7: Run the Tunnel as a Service
sudo cloudflared service install
systemctl status cloudflared --no-pagerThe command creates the cloudflared service, enables it at boot and starts it with /etc/cloudflared/config.yml.
Check the Result
sudo journalctl -u cloudflared --no-pager -n 50 | grep 'Registered tunnel connection'
sudo cloudflared tunnel info hms-tunnel
curl -I https://app.your-domain.comThe logs must show registered connections, and the URL must respond. If you created an Access application, open the address in a private browsing window: the login page must appear before the application.
From your computer, direct access must fail:
curl -m 5 -I http://YOUR_SERVER_IP:3000Close Access That Is No Longer Needed
If the application was previously published on the web ports and nothing uses them anymore, remove their UFW rules:
sudo ufw status numbered
sudo ufw delete allow 80/tcp
sudo ufw delete allow 443/tcpThe tunnel works with a closed inbound policy. Be sure to keep your SSH rule, unless you go through a private network such as Tailscale.
The cert.pem file lets you manage all of the account's tunnels, whereas the service only needs the tunnel's credentials file. If you do not manage your tunnels from this server, delete it:
sudo shred -u /root/.cloudflared/cert.pemYou will need to run sudo cloudflared tunnel login again before any management command, such as tunnel route dns or tunnel info. Deleting this file does not revoke the token it contains: to do that, delete the Cloudflare Tunnel API Token or Argo Tunnel API Token under My Profile > API Tokens.
Publish Another Service
Add a rule before the final rule:
ingress:
- hostname: app.your-domain.com
service: http://127.0.0.1:3000
- hostname: docs.your-domain.com
service: http://127.0.0.1:8080
- service: http_status:404Validate, create the DNS name, then restart the service:
sudo cloudflared tunnel --config /etc/cloudflared/config.yml ingress validate
sudo cloudflared tunnel route dns hms-tunnel docs.your-domain.com
sudo systemctl restart cloudflaredIf cert.pem has been deleted, you can also create a CNAME record in Cloudflare DNS pointing to TUNNEL_UUID.cfargotunnel.com.
Alternative: Manage the Tunnel from the Dashboard
Cloudflare also lets you control the tunnel remotely, without config.yml:
- Go to Networking > Tunnels, then select Create a tunnel.
- Name the tunnel, choose your system and run the suggested command on the server. It contains a secret token.
- In the Routes tab, select Add route, then Published application.
- Enter the subdomain, the domain and the service URL, for example
http://127.0.0.1:3000.
A machine can run only one cloudflared service. Add your applications to the existing tunnel rather than installing a second one.
Limitations to Know
- The application sees requests arriving from
cloudflared. The visitor's real address is in theCF-Connecting-IPheader. - The request size limit depends on your Cloudflare plan: 100 MB on the Free and Pro plans.
- Cloudflare's terms restrict the delivery of videos and large files on the Free, Pro and Business plans.
- A multi-level subdomain, such as
a.b.your-domain.com, requires an advanced certificate. - This guide covers web applications. SSH or other TCP protocols also require configuration on the client side.
Update
A cloudflared installed from a package is updated with APT, then the service must be restarted. Traffic in progress is briefly interrupted:
sudo apt-get update
sudo apt-get install --only-upgrade -y cloudflared
sudo systemctl restart cloudflared
cloudflared --versionBack Up and Restore
The /etc/cloudflared folder contains the configuration and the tunnel's credentials file:
sudo tar -czf /root/cloudflared-$(date +%F).tar.gz -C /etc cloudflared
sudo chmod 600 /root/cloudflared-$(date +%F).tar.gzThis credentials file does not expire and allows the tunnel to be run: keep the archive encrypted and off the server.
To restore on a server where cloudflared is installed, replacing YYYY-MM-DD with its date:
sudo tar -xzf /root/cloudflared-YYYY-MM-DD.tar.gz -C /etc
sudo cloudflared service installStop the tunnel on the old server first if you do not want both to receive traffic.
Troubleshooting
Error 1033
Cloudflare finds no active instance of the tunnel. Check the service and its logs:
systemctl status cloudflared --no-pager
sudo journalctl -u cloudflared --no-pager -n 100If the service is running, check that the CNAME points to the right TUNNEL_UUID.cfargotunnel.com.
502 Error and Unable to reach the origin service
The tunnel is connected, but cloudflared cannot reach the application. Check that it is running, then compare its port and protocol with the service line:
curl -I http://127.0.0.1:3000ERR_TOO_MANY_REDIRECTS
The application redirects HTTP to HTTPS while the rule uses http://. Disable this redirect in the application, or use https:// in service with a valid certificate.
x509: certificate signed by unknown authority or certificate is valid for
With an HTTPS application, cloudflared does not recognize its certificate. Specify the authority with caPool, or the expected name with originServerName, in originRequest. Use noTLSVerify only for the duration of a diagnosis.
remote error: tls: handshake failure
The public name is not covered by a certificate, often because it is a multi-level subdomain. Use a single level or order an advanced certificate.
Tunnel credentials file ... doesn't exist or is not a file
The credentials-file path is wrong. Check that it matches the file present in /etc/cloudflared:
sudo ls -l /etc/cloudflaredAn A, AAAA, or CNAME record with that host already exists
The name is already used in your DNS zone. Delete the old record in Cloudflare after checking that it is no longer needed, or choose another name. To deliberately replace the record:
sudo cloudflared tunnel route dns --overwrite-dns hms-tunnel app.your-domain.comcloudflared service is already installed
A service already exists on the machine. Add your application to the existing tunnel, or uninstall the service before starting over:
sudo cloudflared service uninstallpossible conflicting configuration
Two configuration files exist, often /root/.cloudflared/config.yml and /etc/cloudflared/config.yml. Keep the one in /etc/cloudflared, then specify it explicitly:
sudo cloudflared --config /etc/cloudflared/config.yml service installThe Tunnel Does Not Connect
cloudflared uses QUIC over UDP and falls back to HTTP/2 over TCP if UDP does not get through. Outbound port 7844 must therefore be open. Test it from the server:
sudo apt-get install -y netcat-openbsd
nc -vz -w 3 region1.v2.argotunnel.com 7844
nc -uvz -w 3 region1.v2.argotunnel.com 7844The UDP test can succeed even if packets are filtered: the TCP test is the more telling one. If your local rules are correct and the port stays blocked, contact support from your client area.
failed to sufficiently increase receive buffer size
This message concerns QUIC's UDP buffer. It is generally harmless and requires no action.