使用Tengine替代Nginx实战部署

              使用Tengine替代Nginx实战部署

                                          作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

  大概是2019年3月11日,F5 Networks宣布将以6.7亿美元收购Nginx,消息一出,网上很多小伙伴开始炸锅了,纷纷议论Nginx后续的版本会不会收费之类的话。Nginx的确是一款优秀的web服务器,不过咱们中国还有一款基于Nginx二次开发的软件叫Tengine,它是由阿里巴巴集团开源的,目前阿里巴巴的电商网站使用的就是Tengine,据说性能在Nginx之上。

  淘宝网,天猫网使用就是阿里巴巴公司自己开源的Tengine这款web服务器,当然,还有其它一些公司也在使用Tengine。

 

一.Tengine概述

1>.什么是Tenine

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

  从2011年12月开始,Tengine成为一个开源项目,Tengine团队在积极地开发和维护着它。Tengine团队的核心成员来自于淘宝、搜狗等互联网企业。

  官网地址:http://tengine.taobao.org/

2>.下载Tenine

下载地址:
  http://tengine.taobao.org/download.html

二.编译安装Tengine

1>.停用现有的nginx服务

[root@node101.yinzhengjie.org.cn ~]# netstat -ntalp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      24954/nginx: master 
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      24954/nginx: master 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -s stop
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# netstat -ntalp | grep nginx
[root@node101.yinzhengjie.org.cn ~]# 

2>.下载Tengine的安装包

[root@node101.yinzhengjie.org.cn ~]# cd /usr/local/src/
[root@node101.yinzhengjie.org.cn /usr/local/src]# 
[root@node101.yinzhengjie.org.cn /usr/local/src]# ll
total 992
drwxr-xr-x 6 root root     186 Dec 23 22:35 echo-nginx-module
drwxr-xr-x 9 1001 1001     186 Dec 22 08:19 nginx-1.14.2
-rw-r--r-- 1 root root 1015384 Dec  4  2018 nginx-1.14.2.tar.gz
[root@node101.yinzhengjie.org.cn /usr/local/src]# 
[root@node101.yinzhengjie.org.cn /usr/local/src]# wget http://tengine.taobao.org/download/tengine-2.3.2.tar.gz
--2019-12-24 20:55:13--  http://tengine.taobao.org/download/tengine-2.3.2.tar.gz
Resolving tengine.taobao.org (tengine.taobao.org)... 140.205.230.4
Connecting to tengine.taobao.org (tengine.taobao.org)|140.205.230.4|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2835884 (2.7M) [application/octet-stream]
Saving to: ‘tengine-2.3.2.tar.gz’

100%[===========================================================================================================>] 2,835,884    482KB/s   in 5.9s   

2019-12-24 20:55:19 (470 KB/s) - ‘tengine-2.3.2.tar.gz’ saved [2835884/2835884]

[root@node101.yinzhengjie.org.cn /usr/local/src]# 
[root@node101.yinzhengjie.org.cn /usr/local/src]# ll
total 3764
drwxr-xr-x 6 root root     186 Dec 23 22:35 echo-nginx-module
drwxr-xr-x 9 1001 1001     186 Dec 22 08:19 nginx-1.14.2
-rw-r--r-- 1 root root 1015384 Dec  4  2018 nginx-1.14.2.tar.gz
-rw-r--r-- 1 root root 2835884 Sep  5 16:58 tengine-2.3.2.tar.gz
[root@node101.yinzhengjie.org.cn /usr/local/src]# 

3>.编译安装Tengine

[root@node101.yinzhengjie.org.cn /usr/local/src]# 
[root@node101.yinzhengjie.org.cn /usr/local/src]# tar -xf tengine-2.3.2.tar.gz 
[root@node101.yinzhengjie.org.cn /usr/local/src]# 
[root@node101.yinzhengjie.org.cn /usr/local/src]# cd tengine-2.3.2/
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# 
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# nginx -V          #由于Tengine是兼容Nginx的,咱们直接复制Nginx的编译参数来安装Tengine,但千万别忘记修改安装路径哈~
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/yinzhengjie/softwares/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip
_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module 
--add-module=/usr/local/src/echo-nginx-module
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# 
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# ./configure --prefix=/yinzhengjie/softwares/tengine --user=nginx --group=nginx --with-h
ttp_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream 
--with-stream_ssl_module --with-stream_realip_module --add-module=/usr/local/src/echo-nginx-module ...... [root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# echo $? 0 [root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# [root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# make -j 4 && make install [root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# [root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# echo $? 0 [root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# [root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# ll /yinzhengjie/softwares/      #很显然,安装完成啦~ total 0 drwxr-xr-x 13 nginx nginx 178 Dec 22 09:21 nginx drwxr-xr-x 6 root root 54 Dec 24 21:14 tengine [root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]#

4>.检查Tengine的版本

[root@node101.yinzhengjie.org.cn ~]# /yinzhengjie/softwares/tengine/sbin/nginx -V
Tengine version: Tengine/2.3.2
nginx version: nginx/1.17.3
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/yinzhengjie/softwares/tengine --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_real
ip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module 
--add-module=/usr/local/src/echo-nginx-module
[root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#

三.使用Nginx的配置文件启动Tengine

1>.修改Tengine的子配置文件(此处请允许我先买个坑)

[root@node101.yinzhengjie.org.cn ~]# cd /yinzhengjie/softwares/tengine/
[root@node101.yinzhengjie.org.cn /yinzhengjie/softwares/tengine]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/softwares/tengine]# ll
total 0
drwxr-xr-x 2 root root 333 Dec 24 21:14 conf
drwxr-xr-x 2 root root  40 Dec 24 21:14 html
drwxr-xr-x 2 root root   6 Dec 24 21:14 logs
drwxr-xr-x 2 root root  19 Dec 24 21:14 sbin
[root@node101.yinzhengjie.org.cn /yinzhengjie/softwares/tengine]# 
[root@node101.yinzhengjie.org.cn /yinzhengjie/softwares/tengine]# vim conf/nginx.conf            #在Tengine的主配置文件末尾加载Nginx的配置文件,如下图所示

2>.我们发现检查语法时报错啦,因为我们是在Tengine中加载的Nginx的子配置文件,而Nginx中子配置文件使用Nginx主配置文件中的变量

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/node101_yinzhengjie_org.cn.conf 
server {
    listen 80;
    listen 443 ssl;
    server_name node101.yinzhengjie.org.cn;
 
    access_log /yinzhengjie/softwares/nginx/logs/node101_yinzhengjie_org_cn_access.log my_access_json;
    error_log /yinzhengjie/softwares/nginx/logs/node101_yinzhengjie_org_cn_error.log;

    location / {
       root /yinzhengjie/data/web/nginx/static/cn;
       index index.html;
       #定义有效的请求referer,用空格隔开即可
       valid_referers none blocked server_names *.baidu.com example.*  ~.google.;
       #如果没有在上面的有效链接定义那么均属于无效请求referer
       if ($invalid_referer) {
           return 403;
       }

       #如果是一些常见的压测试工具,咱们直接进给他拒绝访问
       if ($http_user_agent ~ "ApacheBench|WebBench|TurnitinBot|Sougou web spider|Grid Server"){
           return 403;
       }
    }

    location = /favicon.ico {
       root /yinzhengjie/data/web/nginx/images/jd;
    }
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/node101_yinzhengjie_org.cn.conf
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/node101_yinzhengjie_com.conf 
server {
    listen 80;
    listen 443 ssl;
    server_name node101.yinzhengjie.com;
 
    access_log /yinzhengjie/softwares/nginx/logs/node101_yinzhengjie_com_access.log my_access_json;
    error_log /yinzhengjie/softwares/nginx/logs/node101_yinzhengjie_com_error.log;

    location / {
       root /yinzhengjie/data/web/nginx/static/com;
       index index.html;
    }

    location = /favicon.ico {
       root /yinzhengjie/data/web/nginx/images/jd;
    }
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/node101_yinzhengjie_com.conf
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
worker_processes  4;
worker_cpu_affinity 00000001 00000010 00000100 00001000; 
 
events {
   worker_connections  100000;
   use epoll;
   accept_mutex on;
   multi_accept on; 
}
   
   http {
     include       mime.types;
       
     default_type  text/html;
    
     server_tokens off; 
      
     charset utf-8;
   
     log_format my_access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"re
sponsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"uri":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' '"http_user_agent":"$http_user_agent",' '"status":"$status"}';   
    access_log logs/access_json.log my_access_json;
 
    ssl_certificate /yinzhengjie/softwares/nginx/certs/www.yinzhengjie.org.cn.crt;
    ssl_certificate_key /yinzhengjie/softwares/nginx/certs/www.yinzhengjie.org.cn.key;
    ssl_session_cache shared:sslcache:20m;
    ssl_session_timeout 10m;
  
    include /yinzhengjie/softwares/nginx/conf.d/*.conf;
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/nginx/conf.d/                    #这里存放着Nginx的子配置文件,关于Nginx的配置文件内容戳上面的代码查看详情
total 8
-rw-r--r-- 1 root root 461 Dec 24 18:16 node101_yinzhengjie_com.conf
-rw-r--r-- 1 root root 972 Dec 24 19:57 node101_yinzhengjie_org.cn.conf
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# /yinzhengjie/softwares/tengine/sbin/nginx -t                 #我们检查语法时发现报错啦,这是因为Nginx主配置文件中定义了一个叫"my_access_jason"的变量
nginx: [emerg] unknown log format "my_access_json" in /yinzhengjie/softwares/nginx/conf.d/node101_yinzhengjie_com.conf:6
nginx: configuration file /yinzhengjie/softwares/tengine/conf/nginx.conf test failed
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# egrep -v "^ *#|^$" /yinzhengjie/softwares/tengine/conf/nginx.conf     #查看Tengine的默认配置文件
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    include /yinzhengjie/softwares/nginx/conf.d/*.conf;
}
[root@node101.yinzhengjie.org.cn ~]# 

3>.自定义Tengine的日志格式,名称要和Nginx子配置文件中调用的一致

[root@node101.yinzhengjie.org.cn ~]# egrep -v "^ *#|^$" /yinzhengjie/softwares/tengine/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    log_format my_access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"res
ponsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"uri":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' '"http_user_agent":"$http_user_agent",' '"status":"$status"}';    
    include /yinzhengjie/softwares/nginx/conf.d/*.conf;
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# /yinzhengjie/softwares/tengine/sbin/nginx -t          #Duang~我们发现报错信息变量,说是缺少SSL相关配置
nginx: [emerg] no "ssl_certificate" is defined for the "listen ... ssl" directive in /yinzhengjie/softwares/nginx/conf.d/node101_yinzhengjie_com.conf
:1nginx: configuration file /yinzhengjie/softwares/tengine/conf/nginx.conf test failed
[root@node101.yinzhengjie.org.cn ~]# 

4>.将Nginx的证书文件在Tengine中配置问题得到解决

[root@node101.yinzhengjie.org.cn ~]# egrep -v "^ *#|^$" /yinzhengjie/softwares/tengine/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    ssl_certificate /yinzhengjie/softwares/nginx/certs/www.yinzhengjie.org.cn.crt;
    ssl_certificate_key /yinzhengjie/softwares/nginx/certs/www.yinzhengjie.org.cn.key;
    ssl_session_cache shared:sslcache:20m;
    ssl_session_timeout 10m;
    log_format my_access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"res
ponsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"uri":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' '"http_user_agent":"$http_user_agent",' '"status":"$status"}';    
    include /yinzhengjie/softwares/nginx/conf.d/*.conf;
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# /yinzhengjie/softwares/tengine/sbin/nginx -t
nginx: the configuration file /yinzhengjie/softwares/tengine/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/tengine/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 

5>.若不想经理上述麻烦的修改配置文件过程,直接将Nginx的主配置文件拷贝到Tengine的主配置目录即可(埋坑结束)

[root@node101.yinzhengjie.org.cn ~]# /yinzhengjie/softwares/tengine/sbin/nginx -t        #经过上面的排错过程中,终于配置文件语法没有啥问题了。
nginx: the configuration file /yinzhengjie/softwares/tengine/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/tengine/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q                             Local Address:Port                                            Peer Address:Port              
LISTEN      0      128                                            *:22                                                         *:*                  
LISTEN      0      128                                           :::22                                                        :::*                  
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# /yinzhengjie/softwares/tengine/sbin/nginx           #启动Tengine
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q                             Local Address:Port                                            Peer Address:Port              
LISTEN      0      128                                            *:80                                                         *:*                  
LISTEN      0      128                                            *:22                                                         *:*                  
LISTEN      0      128                                            *:443                                                        *:*                  
LISTEN      0      128                                           :::22                                                        :::*                  
[root@node101.yinzhengjie.org.cn ~]# 

6>.浏览器访问"http://node101.yinzhengjie.org.cn/",注意观察Web Server的版本哟~

四.隐藏Tengine的版本号

1>.修改主配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/tengine/conf/nginx.conf
worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    #将Nginx的ssl相关配置拷贝过来
    ssl_certificate /yinzhengjie/softwares/nginx/certs/www.yinzhengjie.org.cn.crt;
    ssl_certificate_key /yinzhengjie/softwares/nginx/certs/www.yinzhengjie.org.cn.key;
    ssl_session_cache shared:sslcache:20m;
    ssl_session_timeout 10m;

    #自定义Nginx日志格式
    log_format my_access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"res
ponsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"uri":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' '"http_user_agent":"$http_user_agent",' '"status":"$status"}';    
    #加载Nginx的子配置文件以供Tengine使用
    include /yinzhengjie/softwares/nginx/conf.d/*.conf;

    #隐藏Tengine的版本号
    server_tokens off; 
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# /yinzhengjie/softwares/tengine/sbin/nginx -t
nginx: the configuration file /yinzhengjie/softwares/tengine/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/tengine/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 

2>.重新加载Tengine的配置文件

[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep tengine | grep -v grep
root       671     1  0 22:14 ?        00:00:00 nginx: master process /yinzhengjie/softwares/tengine/sbin/nginx
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# pstree -p 671
nginx(671)───nginx(727)
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# /yinzhengjie/softwares/tengine/sbin/nginx -s reload
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep tengine | grep -v grep
root       671     1  0 22:14 ?        00:00:00 nginx: master process /yinzhengjie/softwares/tengine/sbin/nginx
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# pstree -p 671      
nginx(671)───nginx(737)
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

3>.浏览器访问"http://node101.yinzhengjie.org.cn/",注意观察Web Server的版本哟~

五.生产环境中推荐使用官方最新的稳定版本

  以上演示案例采用的是2019年9月80日发布的版本部署,经测试,有些特定功能该版本并没有,于是更换为2015年12月31日发布的最新稳定版,版本不追求最新,我们运维人员最应该追求的功能稳定,安全。

1>.下载Tengine 2.1.2版本

[root@node101.yinzhengjie.org.cn ~]# cd /usr/local/src/
[root@node101.yinzhengjie.org.cn /usr/local/src]# 
[root@node101.yinzhengjie.org.cn /usr/local/src]# ll
total 3768
drwxr-xr-x  6 root root     186 Dec 23 22:35 echo-nginx-module
drwxr-xr-x  9 1001 1001     186 Dec 22 08:19 nginx-1.14.2
-rw-r--r--  1 root root 1015384 Dec  4  2018 nginx-1.14.2.tar.gz
drwxrwxr-x 14 root root    4096 Dec 24 21:11 tengine-2.3.2
-rw-r--r--  1 root root 2835884 Sep  5 16:58 tengine-2.3.2.tar.gz
[root@node101.yinzhengjie.org.cn /usr/local/src]# 
[root@node101.yinzhengjie.org.cn /usr/local/src]# wget http://tengine.taobao.org/download/tengine-2.1.2.tar.gz
--2019-12-26 06:01:15--  http://tengine.taobao.org/download/tengine-2.1.2.tar.gz
Resolving tengine.taobao.org (tengine.taobao.org)... 140.205.230.4
Connecting to tengine.taobao.org (tengine.taobao.org)|140.205.230.4|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2137295 (2.0M) [application/octet-stream]
Saving to: ‘tengine-2.1.2.tar.gz’

100%[==================================================================================================================================>] 2,137,295    458KB/s   in 4.6s   

2019-12-26 06:01:19 (452 KB/s) - ‘tengine-2.1.2.tar.gz’ saved [2137295/2137295]

[root@node101.yinzhengjie.org.cn /usr/local/src]# 
[root@node101.yinzhengjie.org.cn /usr/local/src]# ll
total 5856
drwxr-xr-x  6 root root     186 Dec 23 22:35 echo-nginx-module
drwxr-xr-x  9 1001 1001     186 Dec 22 08:19 nginx-1.14.2
-rw-r--r--  1 root root 1015384 Dec  4  2018 nginx-1.14.2.tar.gz
-rw-r--r--  1 root root 2137295 Sep  5 16:58 tengine-2.1.2.tar.gz
drwxrwxr-x 14 root root    4096 Dec 24 21:11 tengine-2.3.2
-rw-r--r--  1 root root 2835884 Sep  5 16:58 tengine-2.3.2.tar.gz
[root@node101.yinzhengjie.org.cn /usr/local/src]# 
[root@node101.yinzhengjie.org.cn /usr/local/src]# 
[root@node101.yinzhengjie.org.cn /usr/local/src]# wget http://tengine.taobao.org/download/tengine-2.1.2.tar.gz

2>.查看最新版本和最稳定版本支持的编译参数

[root@node101.yinzhengjie.org.cn /usr/local/src]# ll
total 5856
drwxr-xr-x  6 root root     186 Dec 23 22:35 echo-nginx-module
drwxr-xr-x  9 1001 1001     186 Dec 22 08:19 nginx-1.14.2
-rw-r--r--  1 root root 1015384 Dec  4  2018 nginx-1.14.2.tar.gz
-rw-r--r--  1 root root 2137295 Sep  5 16:58 tengine-2.1.2.tar.gz
drwxrwxr-x 14 root root    4096 Dec 24 21:11 tengine-2.3.2
-rw-r--r--  1 root root 2835884 Sep  5 16:58 tengine-2.3.2.tar.gz
[root@node101.yinzhengjie.org.cn /usr/local/src]# 
[root@node101.yinzhengjie.org.cn /usr/local/src]# tar -xf tengine-2.1.2.tar.gz 
[root@node101.yinzhengjie.org.cn /usr/local/src]# 
[root@node101.yinzhengjie.org.cn /usr/local/src]# cd tengine-2.1.2/
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.1.2]# 
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.1.2]# ./configure --help | grep concat        #大家看这里,2015年发布的稳定版本编译是支持"--with-http_concat_module"配置参数的
  --with-http_concat_module          enable ngx_http_concat_module
  --with-http_concat_module=shared   enable ngx_http_concat_module (shared)
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.1.2]# 
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.1.2]# cd ../tengine-2.3.2/                #很明显,现在最新版本的Tengine是不支持上面提到的编译参数的,根据淘宝官网的提示说需要使用上面的功能得单独源码安装该模块,为了简单省事,我推荐大家直接使用稳定版本即可。
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# 
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# ./configure --help | grep concat
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]# 
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.3.2]#  

3>.编译安装稳定的Tengine版本

[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.1.2]# /yinzhengjie/softwares/tengine/sbin/nginx -s stop
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.1.2]# 
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.1.2]# /yinzhengjie/softwares/tengine/sbin/nginx -V
Tengine version: Tengine/2.3.2
nginx version: nginx/1.17.3
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/yinzhengjie/softwares/tengine --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_s
tub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=/usr/local/src/echo-nginx-module
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.1.2]# 
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.1.2]# ./configure --prefix=/yinzhengjie/softwares/tengine-2.1.2 --user=nginx --group=nginx --with-http_ssl_module 
--with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module 
--with-stream_realip_module --add-module=/usr/local/src/echo-nginx-module 
./configure: error: invalid option "--with-stream"         #如果出现这个报错那是因为2015年发布的稳定版本压根不支持反向代理(Nginx1.9以上版本才支持的TCP反向代理功能),因此需要将含有"with-stream"的配置参数都去掉。
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.1.2]# 
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.1.2]# ./configure --prefix=/yinzhengjie/softwares/tengine-2.1.2 --user=nginx --group=nginx --with-http_ssl_module 
--with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre  --add-module=/usr/local/src/echo-nginx-module
......
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.1.2]# echo $?
0
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.1.2]# 
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.1.2]# make -j 4 && make install
......
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.1.2]# echo $?
0
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.1.2]# 
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.1.2]# ll /yinzhengjie/softwares/
total 0
drwxr-xr-x 13 nginx nginx 178 Dec 22 09:21 nginx
drwxr-xr-x 11 root  root  151 Dec 24 22:11 tengine
drwxr-xr-x  8 root  root   84 Dec 26 06:53 tengine-2.1.2
[root@node101.yinzhengjie.org.cn /usr/local/src/tengine-2.1.2]# 

4>.将最新版本的Tengine的配置文件直接拷贝到最稳定版本的Tengine的配置文件中

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/tengine/conf/nginx.conf
worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    #将Nginx的ssl相关配置拷贝过来
    ssl_certificate /yinzhengjie/softwares/nginx/certs/www.yinzhengjie.org.cn.crt;
    ssl_certificate_key /yinzhengjie/softwares/nginx/certs/www.yinzhengjie.org.cn.key;
    ssl_session_cache shared:sslcache:20m;
    ssl_session_timeout 10m;

    #自定义Nginx日志格式
    log_format my_access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_tim
e,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"uri":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' '"http_user_agent":"$http_user_agent",' '"status":"$status"}';    
    #加载Nginx的子配置文件以供Tengine使用
    include /yinzhengjie/softwares/nginx/conf.d/*.conf;

    #隐藏Tengine的版本号
    server_tokens off; 
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/tengine/conf/nginx.conf
[root@node101.yinzhengjie.org.cn ~]# cp  /yinzhengjie/softwares/tengine/conf/nginx.conf /yinzhengjie/softwares/tengine-2.1.2/conf/nginx.conf
cp: overwrite ‘/yinzhengjie/softwares/tengine-2.1.2/conf/nginx.conf’? y
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# /yinzhengjie/softwares/tengine-2.1.2/sbin/nginx -t
the configuration file /yinzhengjie/softwares/tengine-2.1.2/conf/nginx.conf syntax is ok
configuration file /yinzhengjie/softwares/tengine-2.1.2/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State      Recv-Q Send-Q                                         Local Address:Port                                                        Peer Address:Port              
LISTEN     0      128                                                        *:22                                                                     *:*                  
LISTEN     0      128                                                       :::22                                                                    :::*                  
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# /yinzhengjie/softwares/tengine-2.1.2/sbin/nginx 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State      Recv-Q Send-Q                                         Local Address:Port                                                        Peer Address:Port              
LISTEN     0      128                                                        *:80                                                                     *:*                  
LISTEN     0      128                                                        *:22                                                                     *:*                  
LISTEN     0      128                                                        *:443                                                                    *:*                  
LISTEN     0      128                                                       :::22                                                                    :::*                  
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# /yinzhengjie/softwares/tengine-2.1.2/sbin/nginx -v
Tengine version: Tengine/2.1.2 (nginx/1.6.2)
[root@node101.yinzhengjie.org.cn ~]#

5>.浏览器访问"http://node101.yinzhengjie.org.cn/",可以正常访问,如下图所示

原文地址:https://www.cnblogs.com/yinzhengjie/p/12081823.html