haproxy與nginx、zabbix整合

來源:互聯網
上載者:User

haproxy與nginx、zabbix整合
一、業務需求 由於業務需求,現在要把伺服器上的部分目錄暴露出去,讓其它系統來調用暴露出去的檔案,但是現在要求對外提供的還是80連接埠的http服務。 分析: 要達到上述的要求,首先我們要提供瀏覽目錄的功能,這個我們可以使用apache或者nginx的瀏覽目錄功能。在此,我們使用的是nginx的瀏覽目錄功能(即nginx的目錄索引功能)。 然後讓haproxy反向 Proxy到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/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.