官网地址:http://tengine.taobao.org/
版本记录:http://tengine.taobao.org/changelog_cn.html
下载链接:http://tengine.taobao.org/download_cn.html
一、基本环境说明
硬件配置 | 4核8G |
系统版本 | Centos6.8 |
Tengine版本 | 2.2.2 |
Keepalive版本 | 1.4.5 |
二、Tenginx目录结构
根目录 | 二级目录 | 二级目录说明 | 三级目录 | 三级目录说明 |
/usr/local/nginx/ | certs | 存放证书文件 |
|
|
| conf | 存放主配置文件 |
|
|
| conf.d | 虚拟主机配置 | demo | 示例配置文件 |
|
|
| backconfig | 配置文件备份 |
| logs | 存放日志文件 | bak | 日志文件备份 |
三、安装部署
安装依赖包:yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake jemalloc jemalloc-devel 配置:./configure --prefix=/usr/local/nginx --with-http_stub_status_module \ --add-module=nginx_tcp_proxy_module-master 编译:make |
编译参数说明:
--prefix=/usr/local/nginx #指定安装路径
--with-http_stub_status_module #获取nginx自上次启动以来的工作状态
--with-http_ssl_module #支持https请求,需已安装openssl
--with-http_gzip_static_module #静态预压缩文件
--with-http_concat_module #用于合并多个文件在一个响应报文中
--with-jemalloc #Tengine链接jemalloc库,使用jemalloc来分配和释放内存
--with-http_v2_module #提供对HTTP/2的支持
--with-http_secure_link_module #保护服务器文件不被任意下载盗用
--add-module=nginx_tcp_proxy_module-master #(待确认是否需要默认编译)
四、配置文件
1.主配置文件nginx.conf
nginx.conf
|
2.监控配置文件admin.conf
admin.conf
|
3.安全防护配置文件defend.conf
defend.conf
|
4.其他说明
(1)配置文件命名
文件目录:/usr/local/nginx/conf.d/
命名规则:<域名>.conf
(2)upstream命名
命名规则:二级域名+映射,二级域名+资源类型
示例:test_order,test_product(二级域名+映射)
test_static(静态资源)
test_server(后端服务)
五、脚本
1.日志切割脚本
脚本存放目录:/home/ops/nginx_daily_log.sh
定时任务:59 23 * * * /bin/sh /home/ops/nginx_daily_log.sh > /dev/null 2>&1
nginx_daily_log.sh
|
2.服务化脚本
脚本存放目录:/etc/init.d/nginx
nginx
|
六、高可用Keepalive
详见《https://blog.csdn.net/weixin_43046724/article/details/91127026》
七、常用命令
1.Keepalive常用命令
service keepalived stop
service keepalived status
service keepalived start
2.Tenignx常用命令
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
service nginx stop
service nginx status
service nginx start
八、版本升级
升级脚本
update_tenginx2.2.2.sh
|
salt脚本
update.sls
|
目录文件
九、获取用户源IP
一、Tegine处理逻辑:
1.如果存在remote_addr,则将remote_addr传给变量clientRealIp;
2.如果不存在remote_addr,则取x_forwarded_for第一个地址传给变量clientRealIp;
3.将变量clientRealIp添加到HTTP头,程序读取HTTP头获取用户IP;(目前定义的字段名为X-User-IP)
具体在Tegine主配置文件nginx.conf 进行添加(红色字体):
proxy_set_header Host $host;
proxy_set_header X-User-IP $clientRealIp;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass_header User-Agent;
proxy_set_header X-Forwarded-Proto $scheme;
map $http_x_forwarded_for $clientRealIp
{
""
$remote_addr;
~
^(?P<firstAddr>
[
0-9\.
]
+)
,
?.*$ $firstAddr;
}
二、通过CDN的访问:用户通过CDN访问的要想获取源IP信息必须先将CDN配置字段修改为:“X-Forwarded-For
”.
以网宿CDN为例,默认字段为“Cdn-Src-Ip” ,须登录CDN后台将默认字段修改为X-Forwarded-For
,如下图: