cloudflare - Cloudflare 为同一域下的不同 ip 列表提供不同的页面
问题描述
这就是我想要做的
我想在 mysite.com 上建立一个维护页面,但我仍想将我的应用程序提供给我们的内部组织
我能够为我的组织提供服务 mysite.com 的方式是通过区域锁定,但我不确定如何与外部世界接触,因为他们什么也得不到
解决方案
看看这个。我正在使用它来实现您提到的完全相同的事情。我还为可能需要它们的其他人提供了一些额外选项。
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
// IPs allowed to bypass the maintenance page
const white_list = [
'1.1.1.1', // MyIP1
'2.2.2.2', // MyIP2
'3.3.3.3' // MyIP3
];
// Custom IP for flexibility of having a custom response
// E.g. using automation to verify that the page is always under maintenance
// Obviously it applies only if you're have scheduled maintenance. In your case, you can safely ignore this.
const custom_whitelist = "4.4.4.4" // MyCustomIP
async function handleRequest(request) {
//return fetch(request) //Uncomment this and comment below to allow everyone to access (to disable the maintenance page)
return maintenance(request)
}
async function maintenance(request) {
let modifiedHeaders = new Headers()
modifiedHeaders.set('Content-Type', 'text/html')
modifiedHeaders.append('Pragma', 'no-cache')
//Check requester IP and compare with whitelist, allow access if match.
if (white_list.indexOf(request.headers.get("cf-connecting-ip")) > -1) {
return fetch(request)
}
//Else, return maintenance page if IP is not whitelisted
else {
var current_ip = ""
current_ip = request.headers.get("cf-connecting-ip")
// Customise headers or other response properties for a custom IP.
// E.g. if using a powershell script to check if 100+ domains are properly set to maintenance,
// you would want the status to remain 200 instead of 503, which would result in a false negative.
// Can be removed if not needed.
if (current_ip == custom_whitelist){
return new Response(maintenanceContent, {
headers: modifiedHeaders
})
}
else {
// Return modified response. HTTP Code 503 is used for maintenance pages.
return new Response(maintenanceContent, {
status: 503,
headers: modifiedHeaders
})
}
}
}
// Place your maintenance page content in HTML below.
let maintenanceContent = `
<!doctype html>
<html lang="en">
<head>
</head>
<body>
</body>
</html>
`;
推荐阅读
- c# - WPF 和 WinForms WebBrowser 控件不处理 HTTP 302 重定向
- javascript - 如何在ionic中使用html5 onerror函数
- sql - SQL 行总问题
- jooq - 如何将本机 SQL 函数公开为谓词
- pdf - Edge 一直默认为某些用户的 pdf 阅读器
- git - 为什么我的 Git 分支在可视化中总是显示为一行?
- c# - C# WebClient - 我怎样才能找到一个类的“src”?
- regex - 搜索段并在末尾附加值
- html - 在具有 innerHTML 内容的 div 中有一个日期
- javascript - 我的 innerHTML 说未定义,我想让 innerHTML 输入