haproxy与nginx、zabbix集成
haproxy与nginx、zabbix集成
一、业务需求由于业务需求,现在要把服务器上的部分目录暴露出去,让其它系统来调用暴露出去的文件,但是现在要求对外提供的还是80端口的http服务。
分析:
要达到上述的要求,首先我们要提供目录浏览的功能,这个我们可以使用apache或者nginx的目录浏览功能。在此,我们使用的是nginx的目录浏览功能(即nginx的目录索引功能)。
然后让haproxy反向代理到nginx,就可以实现业务的需求。
二、nginx配置
nginx的安装在此就不列出了,下面我们直接看nginx的配置文件。如下:
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/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 /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; gzip on; server { listen 8088; server_name 127.0.0.1; charset utf-8; access_log /var/log/nginx/log/host.access.log main; location /testnginx/ { root /data/; autoindex on; } } }
nginx现在我们定义的是监听8088这个端口,而且对外开放的是/data下的是testnginx这个目录。
注意:这个对外的目录名称一定要记住,这个名称我们在haproxy匹配规则中会使用到。
nginx配置完毕后,我们选择来查看下起目录浏览功能。如下:
http://http.ilanni.com:8088/testnginx/
通过上图,我们可以很明显的看出nginx的目录浏览已经完全没有问题。
三、haproxy配置
nginx配置完毕后,我们现在来配置haproxy。haproxy的配置就很简单了。
只需要根据客户端请求的url中匹配目录的名称即可。具体配置文件如下:
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
maxconn 4096
uid 188
gid 188
daemon
tune.ssl.default-dh-param 2048
defaults
log global
mode http
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.1
option redispatch
retries 3
option redispatch
maxconn 2000
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
listen admin_stats
bind 0.0.0.0:1080
mode http
option httplog
maxconn 10
stats refresh 30s
stats uri /stats
stats auth admin:admin
stats hide-version
frontend weblb
bind *:80
acl is_nginx url_beg /testnginx
acl is_http hdr_beg(host) http.ilanni.com
use_backend nginxserver if is_nginx is_http
use_backend httpserver if is_http
backend httpserver
balance source
server web1 127.0.0.1:8080 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3
backend nginxserver
balance source
server web1 127.0.0.1:8088 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3
在haproxy配置文件中,我们定义了一个is_nginx规则,该规则匹配的是以testnginx目录开始的url,然后把该规则转发到后端的nginxserver服务器组,而nginxserver服务器组就是nginx服务器。
这样就完成了nginx与haproxy集成。
注意:在这有一点一定要注意啦,haproxy匹配目录的时候,后端的服务器组一定要存在该目录,否则会报404错误的。这个是我踩过很多坑后才知道的。
四、测试集成功能
nginx与haproxy集成配置完毕后,我们选择来访问http://http.ilanni.com/testnginx/测试其功能,如下:
通过上图,我们可以很明显的看出haproxy已经完美的和nginx目录浏览功能集成了。
五、haproxy与zabbix集成
前几章节我们讲解了haproxy与nginx进行集成的功能,在这一章节,我们再来介绍下haproxy与zabbix集成的功能。
zabbix在此我们是使用的yum方式进行安装的,安装完毕后apache监听的是8099端口。访问形式如下:
http://zabbix.ilanni.com:8099/zabbix/
现在要求直接使用80端口访问zabbix,haproxy具体配置如下:
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
maxconn 4096
uid 188
gid 188
daemon
defaults
log global
mode http
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.1
option redispatch
retries 3
option redispatch
maxconn 2000
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
listen admin_stats
bind 0.0.0.0:1080
mode http
option httplog
maxconn 10
stats refresh 30s
stats uri /stats
stats auth admin:admin
stats hide-version
frontend weblb
bind *:80
acl is_lianzhou hdr_beg(host) zabbix.ilanni.com
acl is_zabbix url_beg /zabbix
use_backend zabbix if is_zabbix
backend zabbix
balance source
server web1 192.168.1.22:8099 maxconn 1024 weight 1 check inter 2000 rise 2 fall 3
haproxy配置很简单,只需要定义一个is_zabbix的url匹配规则,然后分发到后端的服务器组即可。
还是需要注意的,匹配的目录在后端服务器是存在的。
配置完毕后,访问如下:
http://zabbix.ilanni.com/zabbix/
评论暂时关闭