負載均衡—nginx反向代理

說明

Nginx的反向代理是使用場景最多,配合分發策略,可以實現高可用和高性能的。但是nginx默認沒有一個比較完善的後端檢測機制,這點我們需要藉助第三方插件。

後端健康檢查

  • nginx_upstream_check_module-master.zip

 

安裝過程

創建文件夾

Shell>mkdir/var/cache/nginx

獲取安裝包,我們這裏使用nginx-1.8.1的穩定版本

Shell># wget http://nginx.org/download/nginx-1.8.1.tar.gz

Shell># wgethttps://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master

安裝patch程序

Shell># yum–y install patch

解壓縮nginx軟件包

Shell># tarxf nginx-1.8.0.tar.gz

解壓縮補丁包

Shell># unzipnginx_upstream_check_module-master.zip

打補丁

Shell># cdnginx-1.8.1

Shell># patch-p1 < ../ nginx_upstream_check_module-master /check_1.7.5+.patch

編譯軟件, 編譯nginx,參數如下(按照rpm包的習慣編譯)

./configure \

--prefix=/etc/nginx \

--sbin-path=/usr/sbin/nginx \

--conf-path=/etc/nginx/nginx.conf \

--error-log-path=/var/log/nginx/error.log \

--http-log-path=/var/log/nginx/access.log \

--pid-path=/var/run/nginx.pid \

--lock-path=/var/run/nginx.lock \

--http-client-body-temp-path=/var/cache/nginx/client_temp \

--http-proxy-temp-path=/var/cache/nginx/proxy_temp \

--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \

--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \

--http-scgi-temp-path=/var/cache/nginx/scgi_temp \

--user=nginx \

--group=nginx \

--with-http_ssl_module \

--with-http_realip_module \

--with-http_gunzip_module \

--with-http_gzip_static_module \

--with-http_stub_status_module \

--with-file-aio \

--with-http_spdy_module \

--add-module=/root/ nginx_upstream_check_module-master

安裝

Shell >make&& make install

 

配置文件示例

user  nginx;

worker_processes  1;

events  {

       use epoll

       worker_connections  1024;

       multi_accept on;

}

http  {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {

        listen       80;

        server_name  localhost;

        location / {

            root   /var/www/html/;

            index  index.html index.htm;

            proxy_set_header Host $host;

            proxy_set_header X-Real-IP  $remote_addr;

            proxy_set_header X-Forward-For  $remote_addr;

            proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;

            proxy_headers_hash_max_size  51200;

            proxy_headers_hash_bucket_size  6400;

            proxy_set_header x-for  $remote_addr;

            proxy_set_header x-server $host;

            proxy_set_header x-agent  $http_user_agent;

            proxy_pass http://cluster1;

            break;

        }

    }

       upstream cluster1  {

        server 10.10.2.31:8080;

        server 10.10.2.32:8080;

        check interval=3000 rise=2 fall=5  timeout=1000;

        check_http_send "GET  /HTTP/1.0\r\n\r\n";

        check_http_expect_alive http_2xx  http_3xx;

       }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章