本文由ilanniweb提供友情贊助,首發于爛泥行天下
想要獲得更多的文章,可以關注我的微信ilanniweb。
昨天介紹了haPRoxy的手機匹配規則,今天再來介紹下haproxy與nginx、zabbix的集成。接下來我會詳細介紹haproxy與nginx目錄瀏覽功能的集成,與zabbix集成我會把haproxy配置貼出來。
由于業務需求,現在要把服務器上的部分目錄暴露出去,讓其它系統來調用暴露出去的文件,但是現在要求對外提供的還是80端口的http服務。
分析:
要達到上述的要求,首先我們要提供目錄瀏覽的功能,這個我們可以使用apache或者nginx的目錄瀏覽功能。在此,我們使用的是nginx的目錄瀏覽功能(即nginx的目錄索引功能)。
然后讓haproxy反向代理到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的目錄瀏覽已經完全沒有問題。
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已經完美集成。
新聞熱點
疑難解答