1、nginx实现负载均衡
服务器ip | 服务器分配 |
192・168・10・ 11:80 | Nginx,Tomcat |
192・168・10・ 12:80 | Tomcat |
192・168・10・ 13:80 | Tomcat |
yunsuan .conf 配置如下:
upstream load_balance_server {
#weigth参数表示权值,权值越高被分配到的几率越大 server 192.168.10.11:8080 weight=5;
server 192.168.10.12:8080 weight=1;
server 192.168.10.13:8080 weight=6;
#HTTP服务器
server {
#监听端口
listen 80;
#域名
server_name www.yunsuan.com;
#对所有请求进行负载均衡请求
location / {
root /root; #定义服务器的默认网站根目录位置
index index. html index .htm; #定义首页索引文件的名称
proxy_pass 定义的服务器列表 |
http:// load_balance_server ;#请求转向 load_balance_server
#以下是一些反向代理的配置(可选择性配置) proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr;
#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
client_max_body_size 10m; |
client_body_buffer_size 128k; |
出现权限错误,failed (13: Per mission denied) while connecting to upst ream 的解决办法,
setenforce [ Enforcing | Permissive | 1 | 0 ] // 1 开启, 0 关闭
永久关闭,需要设置文件/etc/sysconfig/selinux并重启才能生效
2、网站有多个webapp的配置
假如 www.yunsuan.com 站点有好几个webapp, finance (金融)、product (产品)、admin (用 户中心)。访问这些应用的方式通过上下文(context)来进行区分:
配置
http {
upstream product_server{
server www.yunsuan.com:8081;
upstream admin_server{
server www.yunsuan.com:8082;
}
upstream finance_server{ server www.yunsuan.com:8083;
}
server {
#默认指向product的server location / { proxy_pass http://product_server;
}
location /product/{ proxy_pass http://product_server;
}
location /admin/ { proxy_pass http://admin_server;
}
location /finance/ { proxy_pass http://finance_server;
}
}