本文由ilanniweb提供友情赞助,首发于烂泥行天下nginx
想要得到更多的文章,能够关注个人微信ilanniweb。web
昨天介绍了haproxy的手机匹配规则,今天再来介绍下haproxy与nginx、zabbix的集成。接下来我会详细介绍haproxy与nginx目录浏览功能的集成,与zabbix集成我会把haproxy配置贴出来。redis
因为业务需求,如今要把服务器上的部分目录暴露出去,让其它系统来调用暴露出去的文件,可是如今要求对外提供的仍是80端口的http服务。apache
分析:后端
要达到上述的要求,首先咱们要提供目录浏览的功能,这个咱们可使用apache或者nginx的目录浏览功能。在此,咱们使用的是nginx的目录浏览功能(即nginx的目录索引功能)。服务器
而后让haproxy反向代理到nginx,就能够实现业务的需求。微信
nginx的安装在此就不列出了,下面咱们直接看nginx的配置文件。以下:app
user nginx;frontend
worker_processes 1;ide
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的目录浏览已经彻底没有问题。
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与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/
经过上图,咱们能够很明显的看出haproxy与zabbix已经完美集成。