Linux系统运维之负载均衡Tengine
阅读原文时间:2023年08月23日阅读:1

一、介绍

  Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台。

二、需求

  由于目前项目组负责多个项目,甲方的登录方式必须携带域名;故通过tengine配置upstream模块,实现负载均衡(由于目前业务都是单点,所以只是反向代理效果),由于无法提供,故选择四层交换上落VIP,大致拓扑如下:

三、安装环境

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

四、安装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 -j `grep processor /proc/cpuinfo | wc -l` && 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

  注:以上为一台负载的配置,另外一台同上。