烂泥:haproxy与nginx、zabbix集成

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介:

本文由ilanniweb提供友情赞助,首发于烂泥行天下

想要获得更多的文章,可以关注我的微信ilanniweb。

昨天介绍了haproxy的手机匹配规则,今天再来介绍下haproxy与nginx、zabbix的集成。接下来我会详细介绍haproxy与nginx目录浏览功能的集成,与zabbix集成我会把haproxy配置贴出来。

一、业务需求

由于业务需求,现在要把服务器上的部分目录暴露出去,让其它系统来调用暴露出去的文件,但是现在要求对外提供的还是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;

}

}

}

clip_image001

nginx现在我们定义的是监听8088这个端口,而且对外开放的是/data下的是testnginx这个目录。

注意:这个对外的目录名称一定要记住,这个名称我们在haproxy匹配规则中会使用到。

nginx配置完毕后,我们选择来查看下起目录浏览功能。如下:

http://http.ilanni.com:8088/testnginx/

clip_image002

通过上图,我们可以很明显的看出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

clip_image003

在haproxy配置文件中,我们定义了一个is_nginx规则,该规则匹配的是以testnginx目录开始的url,然后把该规则转发到后端的nginxserver服务器组,而nginxserver服务器组就是nginx服务器。

这样就完成了nginx与haproxy集成。

注意:在这有一点一定要注意啦,haproxy匹配目录的时候,后端的服务器组一定要存在该目录,否则会报404错误的。这个是我踩过很多坑后才知道的。

四、测试集成功能

nginx与haproxy集成配置完毕后,我们选择来访问http://http.ilanni.com/testnginx/测试其功能,如下:

clip_image004

通过上图,我们可以很明显的看出haproxy已经完美的和nginx目录浏览功能集成了。

五、haproxy与zabbix集成

前几章节我们讲解了haproxy与nginx进行集成的功能,在这一章节,我们再来介绍下haproxy与zabbix集成的功能。

zabbix在此我们是使用的yum方式进行安装的,安装完毕后apache监听的是8099端口。访问形式如下:

http://zabbix.ilanni.com:8099/zabbix/

clip_image005

现在要求直接使用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/

clip_image006

通过上图,我们可以很明显的看出haproxy与zabbix已经完美集成。


本文转自 烂泥行天下 51CTO博客,原文链接:http://blog.51cto.com/ilanni/1710621

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore     ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库 ECS 实例和一台目标数据库 RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
7月前
|
监控 应用服务中间件 BI
如何使用 Zabbix 监控 Nginx?
如何使用 Zabbix 监控 Nginx?
159 6
|
监控 应用服务中间件 nginx
ZABBIX4.0监控nginx1.16
ZABBIX4.0监控nginx1.16
105 0
ZABBIX4.0监控nginx1.16
|
监控 应用服务中间件 nginx
使用zabbix监控nginx服务(十五)
使用zabbix监控nginx服务 1.开启nginx状态监控
272 0
使用zabbix监控nginx服务(十五)
|
监控 应用服务中间件 nginx
zabbix3.2监控nginx(二)
上篇文章中监控了nginx status,这篇文章来介绍如何添加模板设置告警(客户端配置见上一篇文章) 1、配置-模板-创建模板 名称随意,添加到相应的群组中,最后点击添加 2、创建模板监控项 继续再添加个监控项 我这里演示只添加了两个,其他同理 3、设置触发器 名称我这里设置为(自行定义...
1008 0
|
监控 应用服务中间件 nginx
zabbix3.2监控nginx(一)
以下内容在zabbix客户端配置 1、查看当前nginx的status模块 nginx -V 查看nginx是否将--with-http_stub_status_module编译进来 可以看到我这里已经将--with-http_stub_status_module编译进来了 2、启用nginx的s...
611 0
|
监控 应用服务中间件 Apache
|
监控 应用服务中间件 PHP
|
监控 网络协议 测试技术
|
监控 应用服务中间件 nginx
|
监控 应用服务中间件 nginx

推荐镜像

更多