2020-04-20 22:16:15
在现实的应用环境中,往往根据业务请求的不同将相关的请求指定到不同的后端服务器中,例如客户是静态资源的请求,haproxy就将请求转发给静态服务器,如果是动态的请求就转发给静态服务器,haproxy实现动静分离是通过acl匹配规则来实现这一目的。
(1)在192.168.180.4上配置static服务器
[root@Monitor conf]# vim /data/index.html
<h1>192.168.180.4---static</h1>
[root@Monitor conf]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
user appuser appuser;
error_log /data/nginx/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
access_log /data/nginx/access.log;
keepalive_timeout 65;
gzip on;
server_tokens off;
server {
listen 80;
server_name 192.168.180.4;
access_log /data/nginx/nginx.access.log;
index index.php index.html index.htm;
# root /data/www/;
root /data/;
}
}
[root@Monitor conf]# /usr/local/nginx/sbin/nginx -s reload
保存后直接加载nginx ,在浏览器上查看该页面
(2)在192.168.180.9安装配置php服务器。
[root@localhost www]# vim /www/html/www/index.php
<h1>this is 192.168.180.9---dynamic for php page</h1>
[root@localhost www]# cat /usr/local/nginx/conf/nginx.conf
worker_processes 1;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
location ~ .php$ {
root /www/html/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
重新加载nginx ,在浏览器中查看如下界面:
(3)在192.168.180.2服务器中安装配置jsp测试界面
[root@ittestserver1 m]# vim 1.jsp
this is test jsp page
[root@ittestserver1 conf]# /usr/local/tomcat/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/jdk1.7.0_79
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
查看测试界面
(4)接下来是最重要的配置haproxy服务器
[root@localhost haproxy]# vim /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
log 127.0.0.1 local2 info ###[err warning info debug]
chroot /usr/local/haproxy
pidfile /var/run/haproxy.pid ###haproxy的pid存放路径,启动进程的用户必须有权限访问此文件
maxconn 4000 ###最大连接数,默认4000
user haproxy
group haproxy
daemon ###创建1个进程进入deamon模式运行。此参数要求将运行模式设置为"daemon"
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http ###默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
log global ###采用全局定义的日志
option dontlognull ###不记录健康检查的日志信息
option httpclose ###每次请求完毕后主动关闭http通道
option httplog ###日志类别http日志格式
option forwardfor ###如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
option redispatch ###serverId对应的服务器挂掉后,强制定向到其他健康的服务器
timeout connect 10000 #default 10 second timeout if a backend is not found
timeout client 300000 ###客户端连接超时
timeout server 300000 ###服务器连接超时
maxconn 60000 ###最大连接数
retries 3 ###3次连接失败就认为服务不可用,也可以通过后面设置
####################################################################
listen stats
bind 0.0.0.0:1080 #监听端口
stats refresh 30s #统计页面自动刷新时间
stats uri /stats #统计页面url
stats realm Haproxy Manager #统计页面密码框上提示文本
stats auth admin:admin #统计页面用户名和密码设置
stats hide-version #隐藏统计页面上HAProxy的版本信息
stats enable ###启用管理界面
stats admin if TRUE ##如果登录成功就可以管理在线服务器
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
#frontend www # *表示haproxy监听所有地址,监听的端口为80
bind 0.0.0.0:80
# bind *:8080
#######定义访问控制,表示url以.css .js .html .php结尾的分别调度到哪台服务器上访问
# acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js .html
acl url_dynamic_php path_end -i .php
acl url_dynamic_jsp path_end -i .jsp
#######usr_backend表示使用backend服务,if表示如果满足url_static这个条件就调度到这台服务器上
use_backend static if url_static ###满足策略要求,则响应策略定义的backend静态页面
use_backend dynamic_php if url_dynamic_php ###满足策略要求,则响应策略定义的backend静态页面
se_backend dynamic_jsp if url_dynamic_jsp ###满足策略要求,则响应策略定义的backend静态页面
# default_backend dynamic ###不满足则响应backend的默认动态页面
# default_backend dynamic ###不满足则响应backend的默认动态页面
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static ###定义后端静态请求响应
balance roundrobin ###负载均衡模式轮询
server static 192.168.180.4:80 check ###后端服务器定义
#server static 192.168.180.9:80 check ###后端服务器定义
backend dynamic_php #####定义后端动态请求响应
balance roundrobin
server phpsrv1 192.168.180.9:80 check maxconn 2000
# server websrv1 dd192.168.180.9:80 check maxconn 2000
#server websrv2 192.168.180.4:80 check maxconn 2000
# server websrv2 192.168.180.2:443 check maxconn 2000
backend dynamic_jsp #####定义后端动态请求响应
balance roundrobin
server jspsrv1 192.168.180.2:8081 check maxconn 2000
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
errorfile 403 /etc/haproxy/errorfiles/403.http
errorfile 500 /etc/haproxy/errorfiles/500.http
errorfile 502 /etc/haproxy/errorfiles/502.http
errorfile 503 /etc/haproxy/errorfiles/503.http
[root@localhost haproxy]# systemctl restart haproxy.service
(1)测试static页面并查看haproxy的访问日志;
[root@localhost ~]# tail -f /var/log/haproxy.log
Jul 20 18:07:22 localhost haproxy[6436]: 192.168.181.231:53672 [20/Jul/2017:18:07:22.371] main static/static 0/0/0/1/1 304 167 - - ---- 0/0/0/0/0 0/0 "GET /index.html HTTP/1.1"
(2)访问php页面
[root@localhost ~]# tail -f /var/log/haproxy.log
Jul 20 18:08:36 localhost haproxy[6436]: 192.168.181.231:53834 [20/Jul/2017:18:08:36.261] main dynamic_php/phpsrv1 0/0/1/0/2 200 2332 - - ---- 0/0/0/0/0 0/0 "GET /index.php?=PHPE9568F35-D428-11d2-A769-00AA001ACF42 HTTP/1.1"
(3)访问jsp页面
[root@localhost ~]# tail -f /var/log/haproxy.log
Jul 20 18:09:58 localhost haproxy[6436]: 192.168.181.231:54015 [20/Jul/2017:18:09:57.999] main dynamic_jsp/jspsrv1 0/0/1/2/3 200 188 - - ---- 0/0/0/0/0 0/0 "GET /1.jsp HTTP/1.1"
(4)查看haproxy监控页面
总之:haproxy可以利用acl规则匹配url做相应的请求跳转,比如动静分离,域名跳转等等应用需求,haproxy是一款性能很强大的四层以及七层代理server。HAProxy运行在 当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中,同时可以保护你的web服务器不被暴露到网络上。
配置HAproxy在http请求头添加后端用户真实IP
07-05
如何解决 Tomcat 不能自动刷新静态页面的问题
03-03
安装配置HAproxy实现RabbitMQ的负载均衡
02-19
HAproxy常用配置介绍,ACL详解
04-15
CentOS 7配置并更换为本地或网络yum源
07-17
Debian 7 (Wheezy)安装配置Apache mod_fastcgi PHP-FPM
07-20
Docker安装配置tomcat jdk
06-21
Jenkins运行多个步骤的配置
06-21
Linux 的系统服务及其配置
07-07
Nginx HTTP2配置教程
06-23
Nginx ngx_http_limit_conn ngx_http_limit_conn模块(请求限制和连接数限制)使用指南
03-20
TR260 G2服务器内存DIMM安装配置和内存插的槽分布
06-23
Ubuntu 16.04配置Nginx使用GeoIP
07-12
Ubuntu系统配置samba实现文件夹共享
06-22
Vista磁盘管理中无法使用压缩卷分离132G空间,如何解决?
05-26
centos 7 yum安装配置apache 2.4
07-15
centos7.3下配置LAMP部署WordPress博客
07-04
centos7安装配置squid正向代理
06-23
linux 配置nfs挂载共享目录
07-13
使用nginx Limit Requests模块限制单个IP请求速率
03-09
dx9.0c(游戏必备驱动)2021 免费版
104.1M
下载flash cs4(动画制作软件)V10.0.0.544 破解版
164.2M
下载macromedia flash(动画设计软件)v8.0 中文版
107 MB
下载regsvr32.exe(动态链接库和ActiveX控件)2021 免费版
0.01MB
下载setpoint(驱动程序综合软件)v6.70.55 中文版
82.9M
下载usb3.0驱动(硬件驱动安装工具) v6.6 绿色版
8.67MB
下载vc2005(动态链接库)V0.3.2 中文版
6.63MB
下载virtual drive manager(虚拟驱动器管理工具) V1.32 绿色版
0.21MB
下载三维动画制作软件下载
76.78 MB
下载电脑动态桌面壁纸下载
1.03 MB
下载视频GIF转换(视频转换动态图片工具)v2.0.0.1免费版
6.97MB
下载SpleeterGUI下载
626.8M
下载adobe flash cs6下载
0 bytes
下载bcdautofix下载
393KB
下载gif movie gear下载
2M
下载h3c模拟器下载
59.1M
下载ulead gif animator下载
6.4M
下载