[英]Cloudflare serving different pages to different ip lists under same domain
Here's what I'm trying to do这就是我想要做的
I want a maintenance page up on mysite.com, but I want to still serve my app to our internal organization我想在 mysite.com 上建立一个维护页面,但我仍想将我的应用程序提供给我们的内部组织
The way I'm able to achieve serving mysite.com to my organization is through zone lock down, but I am unsure how to approach the outside world since they don't get anything我能够实现为我的组织服务 mysite.com 的方式是通过区域锁定,但我不确定如何与外界接触,因为他们什么也得不到
Take a look at this.看看这个。 I'm using this to achieve the exact same thing you mentioned.
我正在使用它来实现您提到的完全相同的事情。 I've also provided some extras options for anyone else who might need them.
我还为可能需要它们的其他人提供了一些额外选项。
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>
`;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.