简体   繁体   English

Cloudflare 为同一域下的不同 ip 列表提供不同的页面

[英]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.

相关问题 Cloudflare Pages 根据提交使用不同的 sphinx 版本,其中一个失败 - Cloudflare Pages uses different sphinx version depending on commits and one is failing Cloudflare 将域转换为 Cloudflare public IP 而不是我的服务器 public IP - Cloudflare translates domain to Cloudflare public IP not my server public IP 通过 cloudflare worker 使用相同的 URL 提供不同的缓存版本 - Serve different cache versions using the same URL through cloudflare worker Cloudflare:将 url 覆盖到不同的路径 - Cloudflare: Override url to different path 将同一裸域反向代理到不同主机 - Reverse proxy same naked domain to different hosts 通过ip地址的网站可以永久快速地通过cloudflare上的域进行加载 - Website by ip address loads quickly, by domain on cloudflare, forever Cloudflare 页面 - Cloudflare Workers - Cloudflare Pages - Cloudflare Workers 自定义域github页面使用cloudflare后返回404错误 - custom domain github pages returns 404 error after using cloudflare 我们可以从同一台机器创建多个具有不同域的 cloudflare argo 隧道吗? - Can we create multiple cloudflare argo tunnel with different domains from same machine? Cloudflare Dashboard 和 Cloudflare Web Analytics 显示非常不同的访问次数 - Cloudflare Dashboard and Cloudflare Web Analytics show very different number of visits
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM