Cloudflare 域名管理
前提
把域名托管到cloudflare
DNS
cloudflare开启dns代理,仅支持http/https协议,会自动cdn,国内使用会很慢
ssh等其它协议不能配置代理
workers 绕过443端口
因为某些端口号被禁止访问了,所以用cloudflare的workers配合dns解析实现反向代理,可绕过gfw及备案使用443端口
反向代理
优点:无需单独申请ssl证书,cloudflae会自动添加ssl证书。国外服务器访问速度能加快
缺点:国内服务器会很慢
cloudflare支持的端口号
HTTP端口:80、8080、8880、2052、2082、2086、2095
HTTPS端口:443、2053、2083、2087、2096、8443
例:
服务器ip: 11.22.33.44
域名:www.xiaoyue.pro
端口:8080
nginx配置
server {
listen 8080;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name www.xiaoyue.pro;
location / {
try_files $uri $uri/ =404;
}
}
workers配置
addEventListener('fetch', event => {
event.respondWith(handleProxyRequest(event.request))
})
async function handleProxyRequest(request) {
const url = new URL(request.url);
const host = request.headers.get("Host");
// 提取子域名,假设域名格式为subdomain.domain.tld
const subdomain = host.split('.')[0];
// 构建目标URL,这里直接使用子域名作为转发的依据
const targetUrl = `http://${subdomain}.xiaoyue.pro:8080`;
// 注意:如果目标服务使用不同的顶级域或者路径不同,你可能需要根据实际情况修改URL构建逻辑
const proxyUrl = new URL(url.pathname + url.search, targetUrl);
const proxyRequest = new Request(proxyUrl, {
method: request.method,
headers: request.headers,
body: request.method !== 'GET' ? request.body : undefined
});
return fetch(proxyRequest);
}
iframe嵌套
用iframe实现类似隐式转发的功能
特点:使用iframe嵌套页面,地址栏不显示端口号,但是实际请求都从原服务器请求
优点:国内外使用速度不受影响
缺点:嵌套https时需要服务器证书
workers配置
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
// 设置目标网页的URL
const targetUrl = 'https://xiaoyue.pro:2083'; // 你希望隐式转发的目标URL
const htmlContent = `
<!DOCTYPE html>
<html style="height: 100%;">
<head>
<title>download</title>
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: hidden; /* 防止出现滚动条 */
}
iframe {
display: block; /* 防止iframe周围有空隙 */
width: 100vw; /* 视口宽度的100% */
height: 100vh; /* 视口高度的100% */
border: none; /* 移除边框 */
}
</style>
</head>
<body>
<iframe src="${targetUrl}" allowfullscreen></iframe>
</body>
</html>
`;
return new Response(htmlContent, {
headers: {
'Content-Type': 'text/html'
}
});
}