|
机器:日本大陆优化
系统:Ubuntu 22.04
架构:KVM
所需软件:Docker Nginx(注意含有stream模块) Certbot
安装官方Nginx(非Ubuntu源)(https://nginx.org/en/linux_packages.html#Ubuntu)
依次执行以下命令安装Nginx
```
sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \
| sudo tee /etc/apt/preferences.d/99nginx
sudo apt update
sudo apt install nginx
```
再安装Certbot
```
apt install certbot
```
使用DNS模式申请证书
```
sudo certbot certonly --manual --preferred-challenges dns
```
在对应的网站上根据提示添加好DNS记录,然后在/etc/letsencrypt/archive/(你的网站)中复制你的证书
将fullchain1.pem privkey1.pem 分别重命名为 (你的网站).crt (你的网站).key
例如你的域名是derp.example.com,则文件名应为derp.example.com.crt和derp.example.com.key
接下来选择一个目录存放你的证书,我选择放在/data/derper/certs
然后执行以下命令拉取docker镜像(https://hub.docker.com/r/fredliang/derper)
```
docker pull fredliang/derper
```
再执行以下命令(有需要修改的字段)
```
docker run -d \
--name derper \
--restart=always \
-p 0.0.0.0:3478:3478/udp \ #此UDP端口不建议修改
-p 0.0.0.0:3443:443 \ #此处3443端口可修改,对应后面的stream,这里的443不建议修改,对应下面DERP_ADDR
-v /data/derper/certs/derp.example.com.crt:/app/certs/derp.example.com.crt \ #自备证书(fullchain)
-v /data/derper/certs/derp.example.com.key:/app/certs/derp.example.com.key \ #自备证书(privkey)
-e DERP_DOMAIN=derp.example.com \ #自备域名
-e DERP_ADDR=:443 \ #derper端口,443不建议修改
-e DERP_CERT_MODE=manual \
fredliang/derper:latest
```
上面为命令解析,下面是给你复制的
```
docker run -d \
--name derper \
--restart=always \
-p 0.0.0.0:3478:3478/udp \
-p 0.0.0.0:3443:443 \
-v /data/derper/certs/derp.example.com.crt:/app/certs/derp.example.com.crt \
-v /data/derper/certs/derp.example.com.key:/app/certs/derp.example.com.key \
-e DERP_DOMAIN=derp.example.com \
-e DERP_ADDR=:443 \
-e DERP_CERT_MODE=manual \
fredliang/derper:latest
```
执行完后docker这里就不需要管了
然后修改位于/etc/nginx/的nginx.conf
接下来的教程参考来源:https://always200.com/tailscale- ... 0%E8%A6%81%EF%BC%89
将stream字段添加到http上面
```
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
stream {
# 这里就是 SNI 识别,将域名映射成一个配置名
map $ssl_preread_server_name $backend_name {
# 把derp.example.com的流量转到derper的upstream
derp.example.com derper;
# 域名都不匹配情况下的默认值
default https_web;
}
# 监听 443 并开启 ssl_preread
server {
listen 443;
listen [::]:443;
proxy_pass $backend_name;
ssl_preread on;
}
upstream derper {
server 127.0.0.1:3443; #此处端口对应上面docker配置的3443
}
upstream https_web {
server 127.0.0.1:12443; #如果需要配置其他网站,将有关443端口全部改为12443
}
}
http {
xxxxxxxxxxxxxxxxxxx
}
```
这样访问[https://derp.example.com] 就能直接显示This is a DERP Server了
添加其他网站例如www.example.com 则需要在位于/etc/nginx/sites-enabled/www.example.com.conf 中
将最前面的443改为12443(对应上面stream的https_web的端口)
```
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
后面省略
```
改后则为
```
server {
listen 12443 ssl http2;
listen [::]:12443 ssl http2;
后面省略
```
然后为了重载nginx.conf,需要重启Nginx服务,使用以下命令重启
```
systemctl restart nginx
```
如果配置无误,就会马上重启好
如果出现报错(443 is xxxx),那大概率是因为你现有网站的端口没改,导致端口占用,需要修改后再重启
最后再去Tailscale上修改ACL,增加derpmap即可
```
"derpMap": {
"OmitDefaultRegions": false
,
"Regions": {
"900": {
"RegionID": 900,
"RegionCode": "lian",
"RegionName": "LIAN",
"Nodes": [{
"Name": "tx",
"RegionID": 900,
"HostName": "derper.linshenkx.cn",
"DERPPort": 443
}
]
}
}
```
测试Derp是否可用时,将"OmitDefaultRegions"设置为true
然后在命令行使用
tailscale status
tailscale netcheck
tailscale ping [你的其他设备的Tailscale IP]
如果出现延迟 并ping通内网设备,则证明运行正常
此文毕 |
|