負載均衡配置

參考原文:   http://blog.csdn.net/liaomin416100569/article/details/72897641


配置參照文檔路徑:進入到tengine.taobao.org官網,點擊文檔--->nginx文檔-->文檔


nginx.xml  位於/usr/local/nginx/conf (這裏使用notepad++操作)


worker_processes  1;     #設置工作的進程數(可設置爲可用cpu的數量)默認值:1  命令:more /porc/cpuinfo  查看cup個數


#error_log  logs/error.log;
#error_log  logs/error.log  notice; #error.log文件位於/usr/local/nginx/logs
#error_log  logs/error.log  info;       #錯誤日誌的實際級別是error


#pid        logs/nginx.pid;



events {
    worker_connections  1024;    #1秒鐘允許連接的個數
}


# load modules compiled as Dynamic Shared Object (DSO)
#
#dso {          #可以導入c語言寫的so文件
#    load ngx_http_fastcgi_module.so;
#    load ngx_http_rewrite_module.so;
#}


http {
    include       mime.types;    #mime類型
    default_type  application/octet-stream;  #默認mime類型


    #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; #該日誌文件和error.log位於相同目錄下
#用於網站流量與性能分析指標:PV/UV/PR/IP/QPS/併發數/吞吐量/響應時間


    sendfile        on;
    #tcp_nopush     on;


    #keepalive_timeout  0;
    keepalive_timeout  65;   #長連接的超時時間:客戶端和服務端之間的連接可以保持的時間,超過這個時間沒有客戶端任何操作連接斷開


    #gzip  on;


#配置負載均衡

upstream backend {


#負載均衡,詳細查看:http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_upstream_module.html#upstream

# 配置後端tomcat服務器的ip和端口 

server 192.168.80.130:8080;  
server 192.168.80.1:8080;

#session共享:保證同一個客戶端訪問的都是同一個後端服務器
session_sticky;


#健康檢查模塊:查看服務器的狀態 
#詳細查看:http://tengine.taobao.org//http_upstream_check_cn.html

#interval檢測後端服務器的時間3000  

#rise=2表示連續兩次連接後端服務器表示存活  

#fall=5表示連續5次連接後端服務器失敗表示後端tomcat掛了  

#timeout=1000 表示連接超時時間  

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

#發送一個HEAD請求   

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

#返回 2和3開頭的響應碼錶示成功   

check_http_expect_alive http_2xx http_3xx;  


}



    server {
        listen       80;
        server_name  localhost;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


location / {
            root   html;
          index  index.html index.htm;
proxy_pass http://backend;   #綁定負載均衡的配置

#當用戶訪問 /admin/ 的路徑 阻止訪問
#查看內置變量:http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_core_module.html#variables
#查看下面的用法:http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_rewrite_module.html

if ( $uri ~* ^/admin/.*$ ) {
return 505;

}

}



#健康檢查的訪問路徑
location /status {
            check_status;
            access_log   off;
            allow 192.168.80.1; #允許訪問該路徑
    deny all;  #禁止所有的訪問請求

#增加訪問登錄限制
auth_basic   "請輸入用戶名以及密碼";   #auth_basic 相當於title的作用
auth_basic_user_file  /usr/local/nginx/conf/htpasswd;   #在/usr/local/nginx/conf目錄下建立文件htpasswd
#在該文件中加入用戶名密碼,格式: 用戶名:密碼  密碼使用密文可以在網上搜htpasswd生成
        }

#健康檢查的路徑:http://Linux的ip/status 訪問成功出現:Nginx http upstream check status



        #error_page  404              /404.html;


        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }


#指定返回值505的映射以及返回頁面
error_page   505  /505x.html;
        location = /505x.html {
            root   html;
        }


        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}


        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}


        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }




    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;


    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}




    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;


    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;


    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;


    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;


    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


}



傳統all in one 模式:單機資源的限制導致QPS(查詢)的效率低(1000m光纖 1024QPS)


反向代理模式:使用nginx的昇華版tengine
多機器 都部署相同的程序,多機器分擔所有的請求壓力
nginx 提供將請求平均到後臺的所有的機器

Nginx的算法:
1>輪詢算法:平均將請求分配到每一臺機器上(tengine默認算法)
2>權重算法:指定機器分擔請求的分配
3>最小連接算法:根據機器的性能高低,性能高的機器分配處理的請求多


代理分爲 正向代理,透明代理,反向代理


    1》正向代理爲 局域網中的機器 訪問目標地址(比如www.baidu.com) 不可達時  可以通過一箇中間代理服務器(可以訪問www.baidu.com)進行轉發 


      正向代理 一般用於局域網聯網等用途  
  
    2》反向代理:互聯網中的機器 訪問局域網中代理服務器  代理服務器通過各種規則找到對應後臺服務器 獲取數據響應  反向代理中 訪問機器明確訪問的


    資源就是位於代理服務器  不需要在機器進行任何設置 可以用於設計cdn服務器 代理後端的靜態資源

    3》透明代理:
         正向代理的一種特殊代理 直接在網關中 設置好代理的服務器 pc機器 直接訪問網關即可連接 














    





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