安裝nginx,配置負載均衡和添加狀態監測的nginx_upstream_check_module模塊

1.安裝所需依賴

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

###2.安裝pcre, nginx_upstream_check_modulejavascript

PCRE 做用是讓 Ngnix 支持 Rewrite 功能。 ####**2.1 下載pcre **css

wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

####**2.2 解壓pcre **html

tar zxvf pcre-8.35.tar.gz

####2.2 編譯和安裝java

cd pcre-8.35
./configure 
make && make install

####**3.1 下載nginx_upstream_check_module ** nginx_upstream_check_module 的項目在github 上,我是安裝了git以後clone下來node

yum install -y git
git clone https://github.com/yaoweibin/nginx_upstream_check_module.git

####**4.1 下載nginx ** 試了下最新穩定版1.10.1,卻配置不成功,不知道爲何,試了1.9.9版本也不行。最後仍是換回了本身熟悉的1.8.1版本。有知道問題的請留言給我吧,謝謝。python

wget http://nginx.org/download/nginx-1.8.1.tar.gz
tar zxvf nginx-1.8.1.tar.gz
cd nginx-1.8.1

PCRE ,nginx_upstream_check_module ,nginx 我是放在同一目錄下。linux

在nginx目錄先patch 一下nginx

patch -p1 < ../nginx_upstream_check_module/check_1.7.5+.patch

patch選擇哪些版本,github上有說明 ####**4.2 配置和安裝nginx **c++

cd nginx-1.8.1
./configure  --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=../pcre-8.35 --add-module=../nginx_upstream_check_module/
make
make install

設置nginx命令git

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx

最後在/usr/local/nginx/conf/nginx.conf配置負載均衡和監測功能

worker_processes  1;

error_log  logs/error.log warn;

pid        logs/nginx.pid;

events {
    worker_connections  1024;
    multi_accept on;
    use epoll;

}


http {
    include       /usr/local/nginx/conf/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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;
    gzip_disable "msie6";
    gzip_proxied any;
    gzip_min_length 1000;
    gzip_comp_level 6;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

     upstream juice-api{
      server 192.168.1.121:8080;
      server 192.168.1.132:8080;
      server 192.168.1.120:8080;

      check interval=5000 rise=1 fall=3 timeout=4000;

      #check interval=3000 rise=2 fall=5 timeout=1000 type=http;
      #check_http_send "HEAD / HTTP/1.0\r\n\r\n";
      #check_http_expect_alive http_2xx http_3xx;

     }

    #安全防護模塊,觸發條件,全部訪問ip 限制每秒80個請求
    limit_req_zone $binary_remote_addr zone=one:10m rate=80r/s;

    server {
        listen       8000;
        server_name  localhost;

        charset utf-8;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://juice-api;
            proxy_redirect off;
            proxy_set_header  Host  $host;
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
            limit_req zone=one burst=5 nodelay;
        }

        location /status {
            check_status;
            access_log   off;
            allow 192.168.1.93;
            deny all;
        }
     }
 }

配置太多就不粘貼了

最後啓動,查看監測 效果圖

參考資料: https://github.com/yaoweibin/nginx_upstream_check_module

http://tengine.taobao.org/document_cn/http_upstream_check_cn.html

http://www.runoob.com/linux/nginx-install-setup.html

相關文章
相關標籤/搜索