阿里云部署
负载均衡 服务器
SLB ECS
管理控制台
产品-实例
创建实例
按量付费-
区域(A/B 网络段)
硬件配置
操作系统
创建专有网络,区域相同、私有网络
公网、绑定弹性公网IP地址
安全组、开启协议
阿里云实验:
10.0.0.148 LB
10.0.0.149 web01
10.0.0.150 web02
1.安装nginx
获取nginx官方repo,下载nginx, scp到其他服务器
yum localinstall -y nginx
2.配置web服务器
cat www.conf
server {
listen 80;
server_name ali.oldboy.com;
location / {
root /code/ali;
index index.html;
}
}
mv default.conf default.off #关闭默认页面
3.检查语法,重启服务
nginx -t
systemctl start nginx
4.配置七层负载均衡
vim /etc/nginx/proxy_params.conf
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 138k;
负载均衡配置
vim conf.d/lb7.conf
upstream test {
server 10.0.0.149:80;
server 10.0.0.150:80;
}
server {
listen 80;
server_name ali.oldboy.com;
location / {
proxy_pass http://test;
include proxy_params;
}
}
nginx -t
systemctl start nginx
四层负载均衡配置
mkdir -p /etc/nginx/conf.c #新建代理配置文件目录
修改nginx.conf主配置文件,
vim /etc/nginx/nginx.conf
include /etc/nginx/conf.c/*.conf #http外层添加
vim /etc/nginx/conf.c/lb4.conf
stream {
upstream ssh_web01 {
server 10.0.0.149:22;
}
upstream ssh_web02 {
server 10.0.0.150:22;
}
server {
listen 5555;
proxy_pass ssh_web01;
}
server {
listen 6666;
proxy_pass ssh_web21;
}
}
购买负载均衡SLB
每个负载端口,后端服务组,单独开
动静分离
通过中间件将动态请求和静态请求进行分离,分离资源,减少延时,较少不必要消耗
好处:动静分离后,即使动态服务不可用,静态资源不会受到影响
1.静态资源服务器
cat ds_oldboy.conf
server {
listen 80;
server_name ds.oldboy.com;
root /soft/code;
index index.html;
location ~* .*\.(png|jpg|gif)$ {
root /soft/code/image;
}
}
mkdir /soft/code/images -p
2.动态资源服务器
yum install -y tomcat
systemctl start tomcat
测试页面放入Tomcat的root目录
vi java_test.jsp
<%@ page language=”java” import=”java.util.*” pageEncoding=”utf-8″%>
<html>
<head>
<title>JSP TEST PAGE </title>
</head>
<body>
<%
Random a = new Random();
out.print(a.nextInt(100)+1);//若要生成1-100的随机数则改为a.nextInt(100)+1即可
%>
</body>
</html>
3.配置负载均衡实现访问jsp和png
cat ds-proxy.conf
upstream static {
server 10.0.0.7:80;
}
upstream java {
server 10.0.0.8:8080;
}
server {
listen 80;
server_name ds.oldboy.com;
location / {
root /soft/code;
index index.html;
}
location ~ .*\.(png|jpg|gir)$ {
proxy_pass http://static;
include proxy_params;
}
location ~ .*\.jsp$ {
proxy_pass http://java;
include proxy_params;
}
}
systemctl restart nginx
4.在负载均衡上整合动态和静态资源
cat /soft/code/index.html
<html lang=”en”>
<head>
<meta charset=”UTF-8″ />
<title> 测试ajax和跨域访问</title>
<script src=”http://libs.baidu.com/jquery/2.1.4/jquery.min.js”></script>
</head>
<script type=”text/javascript”>
$(document).ready(function(){
$.ajax({
type: “GET”,
url: “http://ds.oldboy.com/java_test.jsp”,
success: function(data) {
$(“#get_data”).html(data)
},
error: function() {
alter(“fail!!, 请刷新再试!”);
}
});
});
</script>
<body>
<h1>测试动静分离</h1>
<img src=”http://ds.oldboy.com/nginx.org”>
<div id=”get_data”></div>
</body>
</html>
关闭后端静态或动态资源,互相不受影响