一.简介
Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台
功能描述:
继承Nginx-1.2.3 的所有特性,100%兼容Nginx的配置;
动态模块加载(DSO)支持。加入一个模块不再需要重新编译整个Tengine;
输入过滤器机制支持。通过使用这种机制Web应用防火墙的编写更为方便;
动态脚本语言Lua支持。扩展功能非常高效简单;
支持管道(pipe)和syslog(本地和远端)形式的日志以及日志抽样;
组合多个CSS、JavaScript文件的访问请求变成一个请求;
可以对后端的服务器进行主动健康检查,根据服务器状态自动上线下线;
自动根据CPU数目设置进程个数和绑定CPU亲缘性;
监控系统的负载和资源占用从而对系统进行保护;
显示对运维人员更友好的出错信息,便于定位出错机器;
更强大的防攻击(访问速度限制)模块;
更方便的命令行参数,如列出编译的模块列表、支持的指令等;
可以根据访问文件类型设置过期时间;
特性:
模块化设计、较好的扩展性;
高可靠:master –> worker
低内存消耗:10000个keep-alive模式下的connection,仅需要2.5MB的内存;
支持热部署:不停机而更新配置文件、日志文件滚动、升级程序版本;
支持事件驱动、AIO、mmap;
工作模式:
nginx的工作模式:非阻塞、事件驱动、由一个master进程生成多个worker线程,每个worker响应n个请求。
二.下载安装
1)准备安装前的步骤
下载tengine
tengine-1.5.2.tar.gz
安装前需要安装依赖的库文件:
#yum install pcre-devel pcer是提供正则表达的
[[email protected] src]# tar xf tengine-1.5.2.tar.gz
[[email protected] tengine-1.5.2]# useradd -r nginx
[[email protected] tengine-1.5.2]#./configure –prefix=/usr/local/nginx –user=nginx –group=nginx –error-log-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –pid-path=/var/run/nginx/nginx.pid –lock-path=/var/lock/nginx.lock –with-http_ssl_module –with-http_stub_status_module –with-http_gzip_static_module –with-http_flv_module –with-http_mp4_module –http-client-body-temp-path=/var/tmp/nginx/client –http-proxy-temp-path=/var/tmp/nginx/proxy –http-fastcgi-temp-path=/var/tmp/nginx/fastcgi –http-uwsgi-temp-path=/var/tmp/nginx/uwsgi && make && make install
[[email protected] sbin]# mkdir /var/tmp/nginx/{proxy,fastcgi,uuwsgi,client} -pv 创建上面配置所需的目录
2)启动nginx
[[email protected] sbin]# ./nginx -s reload 重读配置文件
[[email protected] sbin]# /usr/local/nginx/sbin/nginx 启动nginx
3)把nginx做成系统服务并添加到开机启动
3.1编辑下面文件,添加以下脚本。
[[email protected] sbin]# vim /etc/rc.d/init.d/nginx
#!/bin/sh
# chkconfig: – 55 45
# description: The nginx daemon is a web service.
# processname: nginx
. /etc/rc.d/init.d/functions
NGINX="/usr/local/nginx/sbin/nginx"
start()
{
echo -n $"Starting nginx: "
daemon $NGINX
echo
}
stop()
{
echo -n $"Shutting down nginx: "
$NGINX -s stop
echo
}
quit()
{
echo -n $"Shutting down nginx: "
$NGINX -s quit
echo
}
reload()
{
echo -n $"reload config:"
$NGINX -s reload
echo
}
[ -f $NGINX ] || exit 1
# See how we were called.
case "$1" in
start)
start
;;
stop)
quit
;;
reload)
reload
;;
restart)
stop
sleep 3
start
;;
*)
echo $"Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
3.2对该脚本赋予执行权限并且启动
[[email protected] sbin]# chmod +x /etc/rc.d/init.d/nginx 对该脚本赋予执行权限
[[email protected] nginx]# chkconfig –level 35 nginx on
[[email protected] nginx]# service nginx start 启动nginx
[[email protected] conf]# cp /usr/local/nginx/conf/nginx.conf{,.bak} 拷贝一份主配置文件
三.配置优化
/usr/local/nginx/conf/nginx.conf nginx主配置文件
/usr/local/nginx/sbin/nginx -s reload 每次修改配置文件之后重读即可生效
1.正常运行配置:
user nginx 指定运行worker进程的用户和组
pid /var/run/nginx/nginx.pid; pid的实际路径在编译的时指定的路径–pid-path=
worker_rlimit_nofile 1024;指worker进程所能够打开的最大文件句柄数
2.性能优化配置:
a.)#worker_processes 2; worker进程的个数,一般少于物理cpu的核心数支持auto
b.)#worker_cpu_affinity cpumask …; 绑定cpu掩码
c.)#timer_resolution;计时器解析度。降低可以减少gettimeofday()系统调用的次数
d.)#worker_priority -10; 指明worker进程的nice值 -20=100 10=110 19=139
3.事件相关配置:
a.)#accept_mutex {off|on};master调度用户请求至各worker进程时使用的负载均衡锁;on表示能让多个worker轮流地、序列化地去响应新请求;
b.)lock_file file; accept_mutex用到的锁文件路径;
c.)use [epoll|rtsig|select|poll]; 指明使用的事件模型;建议让Nginx自行选择;
d.)worker_connections 10240;设定单个worker进程所能够处理的最大并发连接数量; 并发=连接数*核心数
4.用户调试配置:
a.)daemon on; 调试功能- 该功能需要编译时候启用–with-debug
b.)master_process {on|off};
c.)error_log file | stderr | syslog:server=address[,parameter=value] | memory:size [debug | info | notice | warn | error | crit | alert | emerg]; 日志文件 可以保存到文件,远程主机内存 。日志级别
5.nginx作为web server时的配置
1)server 定义虚拟主机
在原有的server段下面添加,
server {
listen 8080;
server_name www.linuxsec.cn;
root "/web/linuxsec";
}
2)可跟端口
listen
指定监听的地址和端口;
listen address[:port];
listen port;
3)server_name后面可以跟多个主机,名称还可以使用正则表达式或通配符,匹配规则如下
a1: 先做精确匹配检查;
a2:左侧通配符匹配检查:*.linuxsec.cn
a3:右侧通配符匹配检查:如mail.*
a4:正则表达式匹配检查:如 ~^.*\.linuxsec\.com$
4)root path
设置资源路径映射;用于指明请求的URL所对应的资源所在的文件系统上的起始路径;
5)location [ = | ~ | ~* | ^~ ] uri { … }
location @name { … }
功能:允许根据用户请求的URI来匹配定义的各location;匹配到时,此请求将被相应的location配置块中的配置所处理,例如做访问控制等功能;
关于定义:
=:精确匹配检查;
~: 正则表达式模式匹配检查,区分字符大小写;
~*: 正则表达式模块匹配检查,不区分字符大小写;
^~:URI的前半部分匹配,不支持正则表达式;
匹配的优先级:精确匹配(=)、^~、~、~*、不带任何符号的location;
6)alias path 别名
用于location配置段,定义路径别名
server {
listen 8080;
server_name www.linuxsec.cn;
location / {
root "/web/linuxsec";
}
location /images/ {
root "/web/linuxsec";
}
location /images/{
alias "web1/pic";
}
}
location:http://linuxsec.cn/images/index.php –>/web/linuxsecn/images/index.html
alias:http://linuxsec.cn/images/index.php –>/web1/pic/index.html
注意:root表名指定路径为对应的location"/"的url,
alias表示路径映射,为location指定后定义的url是相对于alias所指定的路径而言。
7)index file
# index index.html index.htm; 设置默认主页
8)erro_page code [..] [=202] url |@name
根据http相应状态码指定也用页面。
erro_page 404 =200 /404.html;
9)基于ip访问控制
allow IP/Network;
deny IP/Network;
10)基于用户的访问控制
basic, digest;
账号密码文件建议使用htpasswd来创建
auth_basic "auth page";
auth_basic_user_file /usr/local/nginx/users/.htpasswd;
[[email protected] conf]# mkdir /usr/local/nginx/users/ -pv 创建目录
[[email protected] users]# htpasswd -c -m /usr/local/nginx/users/.htpasswd test 创建用户
[[email protected] users]# /usr/local/nginx/sbin/nginx -s reload 重载
访问测试。
11)基于https的web
a)生成私钥,生成证书签署请求,并获得证书。
[[email protected] ~]# cd /etc/pki/CA/
[[email protected] CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)
[[email protected] CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3655
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:ha
Locality Name (eg, city) [Default City]:zz
Organization Name (eg, company) [Default Company Ltd]:linuxsec
Organizational Unit Name (eg, section) []:ops
Common Name (eg, your name or your server's hostname) []:ca.linuxsec.cn
Email Address []:[email protected]
[[email protected] CA]# touch serial index.txt
[[email protected] CA]# echo 01 > serial
b)给nginx自己申请证书
[[email protected] CA]# cd /usr/local/nginx/
[[email protected] nginx]# mkdir ssl
[[email protected] nginx]# cd ssl
[[email protected] ssl]# openssl req -new -key nginx.key -out nginx.csr
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:ha
Locality Name (eg, city) [Default City]:zz
Organization Name (eg, company) [Default Company Ltd]:linuxsec
Organizational Unit Name (eg, section) []:ops
Common Name (eg, your name or your server's hostname) []:www.linuxsec.cn
Email Address []:[email protected]
c)为nginx签署证书
[[email protected] ssl]# openssl ca -in nginx.csr -out nginx.crt -days 3655
d)配置https,编辑nginx主配置文件添加私钥和证书路径
[[email protected] nginx]# vim conf/nginx.conf
server {
listen 443;
server_name www.linuxsec.cn;
ssl_certificate /usr/local/nginx/ssl/nginx.crt; 证书的位置
ssl_certificate_key /usr/local/nginx/ssl/nginx.key; 私钥的位置
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
e)重启之后查看443端口,访问https://www.linuxsec.cn
12)stub_status{on|off} 状态页面
location /status {
stub_status on ;
allow 172.16.0.0/16;
deny all;
}
http://172.16.249.127/status
Active connections: 2
server accepts handled requests request_time
2 2 2 0
Reading: 0 Writing: 1 Waiting: 1
(1)已经接受过的连接数
(2)已经处理过的连接数
(3)已经处理过的请求,在保持连接模式下。
reading 处于接受请求的连接数
wiring处于发送相应报文的
waiting 保持连接模式。处于活动的
13)防盗链
location ~* \.(jpg|gif|jpeg|png)$ {
valid_referer none blocked www.linuxsec.cn;
if ($invalid_referer) {
rewrite ^/ http://linuxsec.cn;
}
}
14)定制访问日志格式
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;
注意:此处可用变量为nginx各模块内建变量;
15)网络连接相关的配置:
1、keepalive_timeout #;
长连接的超时时长,默认75s;
2、keepalive_requests #;
在一个长连接上所能够允许请求的最大资源数;
3、keepalive_disable [msie6|safari|none];
为指定类型的User Agent禁用长连接;
4、tcp_nodelay on|off;
是否对长连接使用TCP_NODELAY选项;
5、client_header_timeout #;
读取http请求报文首部的超时时长;
6、client_body_timeout #;
读取http请求报文body部分的超时时长;
7、send_timeout #;
发送响应报文的超时时长;