1 Introduction
Because Tailscale does not have an official relay server in China (for reasons you know), although it can basically still be used (using overseas relays), the displayed latency is very high. Moreover, deep down, public relay servers feel uncomfortable (like sharing underwear?). What should you do if you want low latency and a dedicated relay server? The only way is to build it yourself. If your home broadband has a public IP address, or you have an existing cloud host, then you can have your own DERP server.
2 Install the latest version of Go
Since DERP is developed in Go, you need to install the Go environment first. First, download the corresponding download package according to your system. The official Go website link is:https://go.dev/dl/

The official website has installation packages for different systems. Since my lightweight server is Debian 11:

So download the go1.21.2.linux-amd64.tar.gz package in the red box, upload it to the root directory, enter the root directory, and then run the following command (requires logging in as root or running the command via sudo):
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.21.2.linux-amd64.tar.gz
Userm -rf /usr/local/gois to delete previous versions of Go. If you haven't installed Go before, you don't need the rm command. Then the tar command extracts the go1.21.2.linux-amd64.tar.gz archive to the /usr/local/go directory, as follows:

Add the Go path to the PATH environment variable
/usr/local/go/bin , so that you can run Go commands:
export PATH=$PATH:/usr/local/go/bin
Note: This method only temporarily adds the environment variable to the current environment. If you want to add it automatically on boot, you need to edit the~/.bashrcfile, add the above environment variable to the last line of the file, and save and exit.
Open the command prompt and type the following command to confirm if Go is installed:
go version
Confirm that the command correctly prints the installed version of Go:

3 Install DERP server
Run the following command to install the latest DERP server to $HOME/go/bin:
go install tailscale.com/cmd/derper@main
But encountered an error (only occurs on domestic VPS):

Directly accessing the URL in the image above yields the following result:

217.160.113 should be the IP address of Google CDN, which means it is inaccessible (for reasons you know). At this time, you need to temporarily add a goproxy:
export GOPROXY=https://goproxy.io
Or:
export GOPROXY=https://goproxy.cn
Run the command again, success:

Before running the binary, you need a domain name pointing to the server. With the domain name and the binary, you can run the DERP server on your domain.
At this point, if you have a cloud server with unoccupied port 443 and port 80, you can directly run the following command to start derper:
sudo /root/go/bin/derper -c=/root/derper.conf -hostname=ab.cd.ef -a=:443 -stun
-c: Specifies the fixed private key of this DERP server (optional, not required; if not fixed, a temporary one will be generated), which is a base64-encoded string starting with “privkey:”. The content of the derper.conf file is as follows:
{
"PrivateKey": "privkey:xxxxxxxxxxxxxxxx"
}
"PrivateKey": "privkey:xxxxxxxxxxxxxxxx"
}
-hostname: A valid public domain name, derper will automatically apply for a Let's Encrypt certificate for this domain name
-a: Specifies the TCP port that derper listens on, default is 443 (if you want to use port 443, the domain name needs to be ICP-filed), other non-443 ports can be specified (domain name does not need ICP filing)
-stun: UDP port for the STUN protocol, used during health checks, default is 3478
This will start the DERP server with port 443 (or the listening port you specified). Then, you can add the DERP server to your Tailscale network as described in step 3.
Regarding the port 443 issue: If you want to use port 443, the domain name must be ICP-filed (for domestic VPS). Otherwise, the cloud provider will obtain your domain name based on the SNI information in the TLS handshake and compare it with the ICP filing database. If it is not in their database, they will reset your TLS handshake, so I recommend not using port 443.
Having port 443 and an ICP-filed domain name is just the prerequisite. If the cloud host has already deployed a site using Nginx (or possibly Apache, which is even more troublesome) that occupies port 443, you need to use the stream module in Nginx for Layer 4 reverse proxy (TLS works at the transport layer) so that DERP's 443 can coexist with other sites. If Nginx was not installed with stream-related modules loaded, you would need to recompile it... Anyway, it's a hassle. I'm lazy, so I decided to skip it. If it were a brand new cloud server, it could be planned from the start, but since there are already a bunch of applications running on the cloud host, I didn't bother. If you have this need (mainly to use Let's Encrypt to automatically renew certificates so you don't have to manually renew free certificates with only a 1-year validity every year), you can research it yourself. In this article, I will deploy using a non-443 port.
This approach is based on the premise that DERP itself performs the TLS handshake, which is also how the official documentation generally does it. However, in reality, this method is not mandatory. You can completely perform the TLS handshake through a reverse proxy at the frontend, while the DERP server itself only listens on the HTTP port. This method is actually much simpler, but this article will still focus on the method where the DERP server performs the TLS handshake itself, as this method does not rely on a reverse proxy and has stronger independence.
sudo /root/go/bin/derper -c=/root/derper.conf -hostname=ab.cd.ef -a=:45678 -http-port -1 -stun -certmode=manual -certdir=xxx
The command format when not using port 443 and port 80 is as follows:
sudo /root/go/bin/derper -c=/root/derper.conf -hostname=ab.cd.ef -a=:45678 -http-port -1 -stun -certmode=manual -certdir=xxx
-a=:45678 Modify the listening port to 45678 (please modify this according to your own environment)
-http-port -1 Do not use HTTP port 80
-certmode manual Indicates manually specifying the certificate
acme.sh --install-cert -d '*.tangwudi.com' \
--cert-file /path/to/certfile/cert.pem \
--key-file /path/to/keyfile/key.pem \
--fullchain-file /path/to/fullchain/fullchain.pem
Therefore, the certificate corresponding to example.com.crt mentioned above must be the certificate corresponding to ”–fullchain-file”, rather than the ”–cert-file” certificate. Everyone must pay attention to this. Previous versions could use ”–cert-file”, but the new version of DERP has this requirement, which confused me for quite a while.
-verify-clients If you want to ensure that you are the only one who can access this DERP server, and you have also installed the tailscale client on the cloud host where the DERP server is installed and successfully logged into your account, just add this parameter. If you want to share it with other friends besides yourself, you cannot add this parameter. However, if it is a cloud host with a fixed public IP, you still need to be careful.
After the command runs successfully, you can access it using https://ab.cd.ef:45678. If successful, it will display as follows:

4 Configure DERP to start on boot
If you want to set DERP to start on boot, you can add it as a service by following these steps:
Add the service file corresponding to DERP
vim /etc/systemd/system/derper.service
Paste the following content into it and save:
[Unit]
Description=Tailscale DERP Relay Server
After=network.target
[Service]
ExecStart=/root/go/bin/derper \
-c=/root/derper.conf \
-hostname=example.com \
-a=:45678 \
-http-port=-1 \
-stun \
-certmode=manual \
-certdir=xxx
Restart=always
User=root
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
Then start the service
systemctl daemon-reexec
systemctl daemon-reload
systemctl enable derper
systemctl start derper
Note: To maintain compatibility with Tailscale client updates, it is best to regularly update the DERP server by re-running the previous installation command: go install tailscale.com/cmd/derper@main。
5 Add the DERP server to your Tailscale network
Each region has a unique Region ID. Region ID values 900-999 are reserved for user-specified custom regions and will not be used by Tailscale itself.
For example, the following configuration will enable a custom DERP server with the domain name example.com:
{
// ... other parts of ACL/Policy JSON
"derpMap": {
"Regions": {
"900": {
"RegionID": 900,
"RegionCode": "myderp",
"Nodes": [
{
"Name": "1",
"RegionID": 900,
"HostName": "example.com",
"DERPPort": 45678,
}
]
}
}
}
}
In the code above, it is actually mainly the content in the red box in the image below. Copy it into the Access Controls of the official tailscale admin, making sure it is before the last ”}”:

Pay attention to the format when modifying, especially the ”,” at the end of the line.
In the above configuration, the only things that really must be modified are ”HostName” and ”DERPPort” (if there is only this one RegionID). The former is your domain name, and the latter is the listening port specified by the DERP server earlier. There is also a default stun udp 3478. Both of these ports need to be opened on your cloud host's firewall.
Run the command on a terminal (Linux or Win) where tailscale is installed to check the DERP usage status:
tailscale netcheck
Displayed as follows:

What is displayed here is the ”RegionCode” name you defined in the code above. In my case, because there are 2 DERP servers, one is a lightweight server on Tencent Cloud, and the other is a Mac mini on my home broadband. However, the detection here actually only checks the udp 3478 port. Even if this shows as available, it might not actually be usable. Ultimately, the HTTPS port listened to by DERP must be available (as mentioned earlier in the article, it can be opened normally using a browserhttps://域名:自定义端口)。
You can also check it in the Machine Details on the official Tailscale website, as shown below, where you can see the Region ID used (defined in the code above):

If there are multiple DERP servers, you can add them as multiple Regions for backup. For example, I added 2 regions with IDs 900 and 901:

Then it will display in Machine Details as follows:

Additionally, here are some commonly used tailscale terminal commands:
tailscale status #查看tailscale终端之间的连接状态:在线或者离线,直接连接direct或是通过relay以及通过哪个relay

tailscale ping #可以直接看到是否是通过DERP以及通过哪个DERP服务器连接
Actually, we can expand on this: how to monitor the health of the DERP server? The simplest way is to monitorhttps://DERP域名:指定端口links, for details you can refer to my other article:Docker Series: Building a Real-time Application Health Monitoring and Alerting System Based on Uptime and Bark
Note: My DERP at home is built using macOS, and the configuration process is very similar to Debian. It is mapped behind a soft router, so building a DERP can also be done in the local network through public IP mapping on the egress device (just map udp 3748 and the specified TCP listening port in, and the mapped internal and external ports must be consistent). It does not necessarily have to be built directly on a cloud host with a public IP. For dynamic domain names, it needs to be used in conjunction with acme.sh or similar tools that support automatic certificate updates.