Linux系統運維之負載均衡Tengine


一、介紹

  Tengine是由淘寶網發起的Web服務器項目。它在Nginx的基礎上,針對大訪問量網站的需求,添加了很多高級功能和特性。Tengine的性能和穩定性已經在大型的網站如淘寶網,天貓商城等得到了很好的檢驗。它的最終目標是打造一個高效、穩定、安全、易用的Web平台。

二、需求

  由於目前項目組負責多個項目,甲方的登錄方式必須攜帶域名;故通過tengine配置upstream模塊,實現負載均衡(由於目前業務都是單點,所以只是反向代理效果),由於無法提供,故選擇四層交換上落VIP,大致拓撲如下:

         

三、安裝環境

CentOS Linux release 7.1    *    2
tengine-2.1.2
yum install openssl openssl-devel pcre 

四、安裝Tengine

  解壓、編譯安裝tengine

[root@SIMEt-NgxProxy01 ~]# tar zxvf tengine-2.1.2.tar.gz
[root@SIMEt-NgxProxy01 ~]# cd  tengine-2.1.2
[root@SIMEt-NgxProxy01 tengine-2.1.2]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre
[root@SIMEt-NgxProxy01 tengine-2.1.2]# make && make install

  配置tengine,主配置文件nginx.conf:

[root@SIMEt-NgxProxy01 ~]# vim /usr/local/nginx/conf/nginx.conf
================================
user  nobody nobody;
worker_processes  auto;
worker_rlimit_nofile 65535;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;


events {
    worker_connections  65535;
    use epoll;
}

# load modules compiled as Dynamic Shared Object (DSO)
#
#dso {
#    load ngx_http_fastcgi_module.so;
#    load ngx_http_rewrite_module.so;
#}

http {
    include       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;
    charset uft-8;


    server_names_hash_bucket_size 256;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 128k; #最大緩存為4個128KB
    client_max_body_size 20m;   #允許客戶端請求的最大的單個文件字節數

    sendfile    on;
    tcp_nopush    on;
    tcp_nodelay    on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    
    include gzip.conf;      #HttpGzip的配置文件
    include proxy.conf;     #配置代理文件
    include vhost/*.conf;     #虛擬主機的配置文件
    include myupstream.conf;       #配置后端的服務器列表文件
}
====================================

  Httpgzip配置文件,gzip.conf:

gzip on;
gzip_min_length 1k;     #設置允許壓縮的頁面最小字節數。
gzip_buffers 4 16k;     #用來存儲gzip的壓縮結果
gzip_http_version 1.1;  #識別HTTP協議版本
gzip_comp_level 2;      #設置gzip的壓縮比 1-9 1壓縮比最小但最快 9相反
gzip_types text/plain application/x-javascript text/css application/xml;        #指定壓縮類型
gzip_proxied any;       #無論后端服務器的headers頭返回什么信息,都無條件啟用壓縮
gzip_vary on;
gzip_disable "MSIE [1-6].";     #禁用IE6的gzip壓縮

  代理配置文件,proxy.conf:

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;
client_body_buffer_size  512k;
proxy_connect_timeout 30;
proxy_read_timeout 30;
proxy_send_timeout 30;
proxy_buffer_size 32k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;

  虛擬機配置文件,vhost/*.conf:

server {
    listen 80;
    server_name 100.100.100.100;
    index index.jsp index.htm index.html;

    location /web {
        proxy_pass http://ui_web;
        proxy_pass_header Set-Cookie;
    }

    location /android {
        proxy_pass http://ui_android;
        proxy_pass_header Set-Cookie;
    }

    location /NginxStatus {
        stub_status on;
        access_log off;
        auth_basic "NginxStatus";
    }
}

  負載均衡配置,myupstream.conf:

upstream ui_web {
    ip_hash;
    server 10.10.10.10:80 max_fails=1 fail_timeout=60s;
}

upstream ui_android {
    ip_hash;
    server 10.10.10.10:8081 max_fails=1 fail_timeout=60s;
}

五、配置啟動腳本

  由於系統是centos7系列,故配置systemctl腳本:

[root@SIMEt-NgxProxy01 ~]# vim /usr/lib/systemd/system/tengine.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
[root@SIMEt-NgxProxy01 ~]# systemctl restart tengine.service

  注:以上為一台負載的配置,另外一台同上。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM