Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

多台服务器负载均衡方法 #3

Open
skydevil88 opened this issue Jun 7, 2022 · 1 comment
Open

多台服务器负载均衡方法 #3

skydevil88 opened this issue Jun 7, 2022 · 1 comment

Comments

@skydevil88
Copy link

skydevil88 commented Jun 7, 2022

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);
	}
};
@egg1234
Copy link

egg1234 commented Jul 18, 2022

@skydevil88 麻烦你如果有空的话,能否帮忙看看#4 的代码问题

非常感谢!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants