cf-page-func-proxy icon indicating copy to clipboard operation
cf-page-func-proxy copied to clipboard

多台服务器负载均衡方法

Open skydevil88 opened this issue 3 years ago • 1 comments

export default {
	async fetch(request, env) {
		const cars = [
			"app1.example.com", 
			"app2.example.com", 
			"app3.example.com", 
			"app4.example.com", 
			"app5.example.com"
		];
		let host = cars[Math.floor(Math.random() * cars.length)];	//随机选择VPS
		//let host = cars[new Date().getDate() % cars.length];		//每天自动更换VPS
		
		let url = new URL(request.url);
		if (url.pathname.startsWith('/')) {
			url.hostname = host;
			let new_request = new Request(url,request);
			return fetch(new_request);
		}
		return env.ASSETS.fetch(request);
	}
};

skydevil88 avatar Jun 07 '22 05:06 skydevil88

@skydevil88 麻烦你如果有空的话,能否帮忙看看https://github.com/xyTom/cf-page-func-proxy/issues/4 的代码问题

非常感谢!

egg1234 avatar Jul 18 '22 02:07 egg1234