DERP¶
A DERP (Designated Encrypted Relay for Packets) server is mainly used to relay traffic between two nodes in case a direct connection can't be established. Headscale provides an embedded DERP server to ensure seamless connectivity between nodes.
Configuration¶
DERP related settings are configured within the derp section of the configuration file. The following sections only use a few of the available settings, check the example configuration for all available configuration options.
Enable embedded DERP¶
Headscale ships with an embedded DERP server which allows to run your own self-hosted DERP server easily. The embedded DERP server is disabled by default and needs to be enabled. In addition, you should configure the public IPv4 and public IPv6 address of your Headscale server for improved connection stability:
Keep in mind that additional ports are needed to run a DERP server. Besides relaying traffic, it also uses STUN (udp/3478) to help clients discover their public IP addresses and perform NAT traversal. Check DERP server connectivity to see if everything works.
Remove Tailscale's DERP servers¶
Once enabled, Headscale's embedded DERP is added to the list of free-to-use DERP servers offered by Tailscale Inc. To only use Headscale's embedded DERP server, disable the loading of the default DERP map:
Single point of failure
Removing Tailscale's DERP servers means that there is now just a single DERP server available for clients. This is a single point of failure and could hamper connectivity.
Check DERP server connectivity with your embedded DERP server before removing Tailscale's DERP servers.
Customize DERP map¶
The DERP map offered to clients can be customized with a dedicated YAML-configuration file. The file extension picks the format: .yaml or .yml, or the JSON format Tailscale serves DERP maps in as .json or .hujson. Both JSON extensions are read as HuJSON, so comments and trailing commas are allowed. This allows to modify previously loaded DERP maps fetched via URL or to offer your own, custom DERP servers to nodes. A file with a key that has no field in DERPMap fails to load. So does a region whose regionid differs from its key, a node whose regionid differs from its region, or a region without nodes: clients count latency by regionid.
A derp.urls or derp.paths source that fails to load, for example a URL that is down or a file with an error, is logged as a warning and skipped. Headscale starts with the DERP regions from the other sources, the embedded DERP server and the policy's derpMap, and refuses to start only when no region remains. A refresh with a failing source keeps the previous DERP map. A skipped file also skips what it removes: a region it sets to null comes back.
The free-to-use DERP servers are organized into regions via a region ID. You can explicitly disable a specific region by setting its region ID to null. The following sample derp.yaml disables the New York DERP region (which has the region ID 1):
Use the following configuration to serve the default DERP map (excluding New York) to nodes:
The following sample derp.yaml references two custom regions (custom-east with ID 900 and custom-west with ID 901) with one custom DERP server in each region. Each DERP server offers DERP relay via HTTPS on tcp/443, support for captive portal checks via HTTP on tcp/80 and STUN on udp/3478. See the definitions of DERPMap, DERPRegion and DERPNode for all available options.
regions:
900:
regionid: 900
regioncode: custom-east
regionname: My region (east)
nodes:
- name: 900a
regionid: 900
hostname: derp900a.example.com
ipv4: 198.51.100.1
ipv6: 2001:db8::1
canport80: true
901:
regionid: 901
regioncode: custom-west
regionname: My Region (west)
nodes:
- name: 901a
regionid: 901
hostname: derp901a.example.com
ipv4: 198.51.100.2
ipv6: 2001:db8::2
canport80: true
Use the following configuration to only serve the two DERP servers from the above derp.yaml:
Each client picks its home DERP region by latency. A region score multiplies the latency a client measures for a region before it compares regions: a score below 1 makes a region more attractive, a score above 1 less attractive. Regions without a score count as 1, scores of 0 or below are ignored. See DERPHomeParams.
The following sample derp.yaml prefers the embedded DERP server (region ID 999) and avoids New York (region ID 1). The file may hold scores alone, the regions come from other sources:
The same in a .json or .hujson file is {"HomeParams": {"RegionScore": {"999": 0.5, "1": 2}}}.
A score biases the choice, it does not pin it: a client keeps its home region unless another one is clearly faster after scaling. Connected clients keep a score that is removed from the file until they reconnect; set it to 1 instead to reset it.
Independent of the custom DERP map, you may choose to enable the embedded DERP server and have it automatically added to the custom DERP map.
Customize the DERP map in the policy¶
The policy may hold a derpMap, as on Tailscale. It changes the DERP map from the configuration above, and connected nodes get the change as soon as the policy is updated. Headscale checks it like Tailscale does:
- A region added in the policy needs a region ID from 900 to 999 and at least one node. Its
RegionID, and theRegionIDof each node, must match the ID it is listed under. A policy region replaces a configured region with the same ID, such as the embedded DERP server's. - A node's
HostNameandCertNamemust not betailscale.comor one of its subdomains. Tailscale also rejects the IP addresses of its own DERP servers; Headscale does not, as that needs Tailscale's current list of servers. - A region set to
nullis removed. This works for any region ID. OmitDefaultRegions: trueremoves every region from the configuration, the embedded DERP server's included, and needs at least one region added in the policy.HomeParamsis accepted but ignored, as on Tailscale. Set region scores in aderp.pathsfile instead.
{
"derpMap": {
"Regions": {
"1": null,
"900": {
"RegionID": 900,
"RegionCode": "custom",
"RegionName": "My region",
"Nodes": [{"Name": "900a", "RegionID": 900, "HostName": "derp900.example.com"}]
}
}
}
}
Verify clients¶
Access to DERP serves can be restricted to nodes that are members of your Tailnet. Relay access is denied for unknown clients.
Tailscale's derper provides two parameters to configure client verification:
- Use the
-verify-client-urlparameter of thederperand point it towards the/verifyendpoint of your Headscale server (e.ghttps://headscale.example.com/verify). The DERP server will query your Headscale instance as soon as a client connects with it to ask whether access should be allowed or denied. Access is allowed if Headscale knows about the connecting client and denied otherwise. - The parameter
-verify-client-url-fail-opencontrols what should happen when the DERP server can't reach the Headscale instance. By default, it will allow access if Headscale is unreachable.
Check DERP server connectivity¶
Any Tailscale client may be used to introspect the DERP map and to check for connectivity issues with DERP servers.
- Display DERP map:
tailscale debug derp-map - Check connectivity with the embedded DERP1:
tailscale debug derp headscale
Additional DERP related metrics and information is available via the metrics and debug endpoint.
Limitations¶
- The embedded DERP server can't be used for Tailscale's captive portal checks as it doesn't support the
/generate_204endpoint via HTTP on port tcp/80. - There are no speed or throughput optimisations, the main purpose is to assist in node connectivity.
-
This assumes that the default region code of the configuration file is used. ↩