如何Nginx优化与防盗链和单机部署LNMP

文章资讯 2020-06-14 20:51:55

如何Nginx优化与防盗链和单机部署LNMP

Nginx优化与防盗链+单机部署LNMP
Nginx是编写的十分轻量级的HTTP服务器,Nginx,它的发音为“engineX”,是一个高性能的HTTP和反向服务器,同时也是一个IMAPPOP3SMTP服务器.Nginx是由IgorSysoev为俄罗斯访问量第二的Rambler.ru站点开发
Nginx以事件驱动(eoll)的方式编写,所以有非常好的性能,同时也是一个非常高效的反
向、负载平衡。但是Nginx并不支持cgi方式运行,原因是可以减少因此带来的一些程
序上的漏洞。所以必须使用FastCGI方式来执行PHP程序
由于Nginx本身的一些优点,轻量,开源,易用,越来越多的公司使用nginx作为自己公司
的web应用服务器,本文详细介绍nginx源码安装的同时并对nginx进行优化配置
一、Nginx的优化
1、编译安装前的优化
编译前的优化主要是用来修改程序名等等,目的更改源码隐藏软件名称和版本号
(1)安装zb-devel、c-devel等依赖包
[root@nginx~]#yum-yinstallgccgcc-c++makebtoolzbzb-develcc-develoenssloenssl-devel(2)下载Nginx源码包
[root@nginx~]#wgethtt:nginx.orgdownloadnginx-1.10.2.tar.gz(3)解压源码包
[root@nginx~]#tarzxfnginx-1.10.2.tar.gz
[root@nginx~]#cdnginx-1.10.2(4)隐藏软件名称和版本号
[root@nginxnginx-1.10.2]#vimsrcconginx.h
#此行修改的是你想要的版本
#defineNGINX_VERSION"1.10.2"#第13行
#此行修改的是你想修改的软件名称
#defineNGINX_VER"nginx"NGINX_VERSION#第14行修改上面的信息,即可更改nginx显示版本。例如:(curl–I可看到,请求头和响应头显示)
#defineNGINX_VERSION"7.0"
#defineNGINX_VER"IIS"NGINX_VERSION修改HTTP头信息中的connection字段,防止回显具体版本号
拓展:通用htt头,通用头包含请求和响应消息都支持的头,通用头包含Cache-Control、
Connection、Date、Pragma、Transfer-Encoding、Ugrade、Via。对通用头的扩展要求通讯双方都支持此扩展,如果存在不支持的通用头,一般将会作为实体头处理。那么也就是说有部分设备,或者是软件,能获取到connection,部分不能,要隐藏就要彻底![root@nginxnginx-1.10.2]#vimsrchttngx_htt_header_filter_mode.c
#修改前
staticcharngx_htt_server_string[]="Server:nginx"CRLF;#第49行
#修改后
staticcharngx_htt_server_string[]="Server:IIS"CRLF;
定义了htt错误码的返回:
有时候我们页面程序出现错误,Nginx会代我们返回相应的错误代码,回显的时候,会带上
nginx和版本号,我们把他隐藏起来
[root@nginxnginx-1.10.2]#vimsrchttngx_htt_secial_sonse.c
#修改前
staticu_charngx_htt_error_tail[]=
"<hr><center>nginx<center>"CRLF#第29行
"<body>"CRLF
"<html>"CRLF
;
#修改后
staticu_charngx_htt_error_tail[]=
"<hr><center>IIS<center>"CRLF
"<body>"CRLF
"<html>"CRLF
;2、安装nginx
(1)添加nginx组
创建nginx运行账户nginx并加入到nginx组,不允许www用户直接登录系统
[root@nginxnginx-1.10.2]#grouaddnginx
[root@nginxnginx-1.10.2]#useradd-gnginxnginx-ssbinnologin(2)编译安装
[root@nginxnginx-1.10.2]#.configu--fix=usrlocalnginx1.10
>--with-htt_dav_mode--with-htt_stub_status_mode
>--with-htt_addition_mode--with-htt_sub_mode
>--with-htt_flv_mode--with-htt_m4_mode--with-c
>--with-htt_ssl_mode--with-htt_gzi_static_mode
>--user=nginx--grou=nginx
[root@nginxnginx-1.10.2]#make&am;&am;makeinstall相关选项说明:
–with-htt_dav_mode增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法–with-htt_stub_status_mode获取Nginx的状态统计信息–with-htt_addition_mode作为一个输出过滤器,支持不完全缓冲,分部分相应请求–with-htt_sub_mode允许一些其他文本替换Nginx相应中的一些文本–with-htt_flv_mode提供支持flv视频文件支持–with-htt_m4_mode提供支持m4视频文件支持,提供伪流媒体服务端支持–with-htt_ssl_mode启用ngx_htt_ssl_mode如果c是通过编译安装的话,例如tarzxvfusrlocalsrcc-8.36.tar.gz-Cusrlocalsrc
cdusrlocalsrcc-8.36
.configu&am;&am;make&am;&am;makeinstall
则–with-c=usrlocalsrcc-8.36
需要注意,这里指的是源码,用.configu--hel|gc查看帮助[root@nginxnginx-1.10.2]#ln-susrlocalnginx1.10sbinnginxusrlocalsbin
[root@nginxnginx-1.10.2]#nginx-t
nginx:theconfigurationfileusrlocalnginx1.10confnginx.confsyntaxisok
nginx:configurationfileusrlocalnginx1.10confnginx.conftestissuccessf(3)启动nginx
[root@nginxnginx-1.10.2]#nginx
[root@nginxnginx-1.10.2]#netstat-ant|gnginx
tc000.0.0.0:800.0.0.0:*LISTEN8303nginx:master(4)测试是否隐藏了版本和软件名
[root@nginx~]#curl-I127.0.0.1
HTTP1.1200OK
Server:IIS7.0
Date:Fri,12Jun202001:08:48GMT
Content-Tye:texthtml
Content-Length:612
Last-Modified:Fri,12Jun202000:59:55GMT
Connection:kee-ave
ETag:"5ee2d38b-264"
Accet-Ranges:bytes3、nginx配置项优化
[root@nginx~]#s-ef|gnginx
root83031009:07?00:00:00nginx:masterrocessnginx
nginx83048303009:07?00:00:00nginx:workerrocess在这里我们还可以看到在查看的时候,work进程是nginx程序用户,但是master进程还是
root,其中,master是监控进程,也叫主进程,work是工作进程,部分还有cache相关进程,关系如图:可以直接理解为master是管理员,work进程才是为用户提供服务的!
(1)Nginx运行工作置进程个数
一般我们设置CPU的核心或者核心数x2
如果不了解cu的核数,可以to命令之后按1也可以看出来,也可以查看roccuinfo文件
[root@nginx~]#g^rocessorroccuinfo|wc-l
1
[root@nginx~]#vimusrlocalnginx1.10confnginx.conf
worker_rocesses2;
[root@nginx~]#nginx-sload
[root@nginx~]#s-aux|gnginx|g-vg
root83030.00.1460281920?Ss09:070:00nginx:masterrocessnginx
nginx102420.00.2485402072?S09:140:00nginx:workerrocess
nginx102430.00.2485402072?S09:140:00nginx:workerrocess(2)Nginx运行CPU亲和力
比如4核配置
worker_rocesses4;
worker_cu_affinity0001001001001000比如8核配置
worker_rocesses8;
worker_cu_affinity0000000100000010000001000000100000010000001000000100000010000000;worker_rocesses最多开启8个,8个以上性能提升不会再提升了,而且稳定性变得更低,
所以8个进程够用了
(3)Nginx最多可以打开文件数
worker_rt_nofile65535;这个指令是指当一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数
(it-n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与it-n的值保持一致
注:
文件资源限制的配置可以在etcsecurityts.conf设置,针对rootuser等各个用户或者*代表所有用户来设置。
用户重新登录生效(it-n)
[root@nginx~]#vimetcsecurityts.conf
*softnofile65535
*hardnofile65535
[root@nginx~]#it-n
65535(4)Nginx事件处理模型
events{
useeoll;
worker_connections65535;
mti_acceton;
}nginx采用eoll事件模型,处理效率高
work_connections是单个worker进程允许客户端最大连接数,这个数值一般根据服务器性
能和内存来制定,实际最大值就是worker进程数乘以work_connections实际我们填入一个65535,足够了,这些都算并发值,一个网站的并发达到这么大的数量,也算一个大站了!
mti_accet告诉nginx收到一个新连接通知后接受尽可能多的连接
(5)开启高效传输模式
htt{
includeme.tyes;
defat_tyeacationoctet-stam;
......
sendfileon;
tc_noushon;Includeme.tyes;媒体类型,include只是一个在当前文件中包含另一个文件内容的指令defat_tyeacationoctet-stam;默认媒体类型足够sendfileon;开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来
输出文件,对于普通应用设为on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统的负载
注意:如果图片显示不正常把这个改成off。tc_noushon;必须在sendfile开启模式才有效,防止网路阻塞,积极的减少网络报文段的数量(告诉nginx在一个数据包里发送所有头文件,而不一个接一个的发送。)(6)连接超时时间
主要目的是保护服务器资源,CPU,内存,控制连接数,因为建立连接也是需要消耗资源的
keeave_timeout65;
tc_nodelayon;
cent_header_buffer_size4k;
oen_file_cachemax=102400inactive=20s;
oen_file_cache_vad30s;
oen_file_cache_n_uses1;
cent_header_timeout15;
cent_body_timeout15;
set_timedout_connectionon;
send_timeout15;
server_tokensoff;
cent_max_body_size10m;keeaved_timeout客户端连接保持会话超时时间,超过这个时间,服务器断开这个链接tc_nodelay;也是防止网络阻塞,不过要包涵在keeaved参数才有效cent_header_buffer_size4k;客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。分页大小可以用命令getconfPAGESIZE取得oen_file_cachemax=102400inactive=20s;这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件
数一致,inactive是指经过多长时间文件没被请求后删除缓存oen_file_cache_vad30s;这个是指多长时间检查一次缓存的有效信息oen_file_cache_n_uses1;oen_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive时间内一次没被使用,它将被移除cent_header_timeout设置请求头的超时时间。我们也可以把这个设置低些,如果超过这个时间没有发送任何数据,nginx将返回questtimeout的错误cent_body_timeout设置请求体的超时时间。我们也可以把这个设置低些,超过这个时间没有发送任何数据,和上面一样的错误提示set_timeout_connection告诉nginx关闭不响应的客户端连接。这将会释放那个客户端所占有的内存空间send_timeout响应客户端超时时间,这个超时时间仅限于两个活动之间的时间,如果超过这个时间,客户端没有任何活动,nginx关闭连接server_tokens并不会让nginx执行的速度更快,但它可以关闭在错误页面中的nginx版本数字,这样对于安全性是有好处的cent_max_body_size上传文件大小限制(7)fastcgi调优
#接上个位置继续写
fastcgi_connect_timeout600;
fastcgi_send_timeout600;
fastcgi_ad_timeout600;
fastcgi_buffer_size64k;
fastcgi_buffers464k;
fastcgi_busy_buffers_size128k;
fastcgi_tem_file_write_size128k;
fastcgi_tem_athusrlocalnginx1.10nginx_tm;
fastcgi_intercet_errorson;
fastcgi_cache_athusrlocalnginx1.10fastcgi_cachelevels=1:2keys_zone=cache_fastcgi:128minactive=1dmax_size=10g;
Cache:写入缓存区
Buffer:读取缓存区
Fastcgi是静态服务和动态服务的一个接口fastcgi_connect_timeout600;指定连接到后端FastCGI的超时时间fastcgi_send_timeout600;向FastCGI传送请求的超时时间fastcgi_ad_timeout600;指定接收FastCGI应答的超时时间fastcgi_buffer_size64k;指定读取FastCGI应答第一部分需要用多大的缓冲区,默认的缓冲区大小为fastcgi_buffers指令中的每块大小,可以将这个值设置更小fastcgi_buffers464k;指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答请求,如果
一个h脚本所产生的页面大小为256KB,那么会分配4个64KB的缓冲区来缓存,如果页面大小大于256KB,那么大于256KB的部分会缓存到fastcgi_tem_ath指定的路径中,但是这并不是好方法,因为内存中的数据处理速度要快于磁盘。一般这个值应该为站点中h脚本所产生的页面大小的中间值,如果站点大部分脚本所产生的页面大小为256KB,那么可以把这个值设置为“832K”、“464k”等fastcgi_busy_buffers_size128k;建议设置为fastcgi_buffers的两倍,繁忙时候的bufferfastcgi_tem_file_write_size128k;在写入fastcgi_tem_ath时将用多大的数据块,默认值是fastcgi_buffers的两倍,该数值设置小时若负载上来时可能报502BadGatewayfastcgi_tem_ath缓存临时目录fastcgi_intercet_errorson;这个指令指定是否传递4xx和5xx错误信息到客户端,或者允许nginx使用error_age处理错误信息
注:静态文件不存在会返回404页面,但是h页面则返回空白页!!fastcgi_cache_athusrlocalnginx1.10fastcgi_cachelevels=1:2keys_zone=cache_fastcgi:128nactive=1dmax_size=10g;fastcgi_cache缓存目录,可以设置目录层级,比如1:2会生成
16*256个子目录,cache_fastcgi是这个缓存空间的名字,cache是用多少内存(这样热门的
内容nginx直接放内存,提高访问速度),inactive表示默认失效时间,如果缓存数据在失效
时间内没有被访问,将被删除,max_size表示最多用多少硬盘空间fastcgi_cachecache_fastcgi;表示开启FastCGI缓存并为其指定一个名称。开启缓存非常有用,可以有效降低CPU的负载,并且防止502的错误放生。cache_fastcgi为roxy_cache_ath指令创建的缓存区名称fastcgi_cache_vad2003021h;用来指定应答代码的缓存时间,实例中的值表示将200和302应答缓存一小时,要和fastcgi_cache配合使用fastcgi_cache_vad3011d;将301应答缓存一天fastcgi_cache_vadany1m;将其他应答缓存为1分钟fastcgi_cache_n_uses1;该指令用于设置经过多少次请求的相同URL将被缓存。fastcgi_cache_keyhtt:hosthosthostquest_uri;#该指令用来设置web缓存的Key值,nginx根据Key值md5哈希存储.一般根据host(域名)、host(域名)、host(域名)、quest_uri(请求的路径)等变量组合成roxy_cache_keyfastcgi_ass指定FastCGI服务器监听端口与地址,可以是本机或者其它总结:nginx的缓存功能有:roxy_cachefastcgi_cache
roxy_cache的作用是缓存后端服务器的内容,可能是任何内容,包括静态的和动态
fastcgi_cache的作用是缓存fastcgi生成的内容,很多情况是h生成的动态的内容
roxy_cache缓存减少了nginx与后端通信的次数,节省了传输时间和后端宽带
fastcgi_cache缓存减少了nginx与h的通信的次数,更减轻了h和数据库(mysql)的压力(8)gzi调优
使用gzi压缩功能,可能为我们节约带宽,加快传输速度,有更好的体验,也为我们节约
成本,所以说这是一个重点
Nginx启用压缩功能需要你来ngx_htt_gzi_mode模块,aache使用的是mod_deflate一般我们需要压缩的内容有:文本,js,html,css,对于图片,视频,flash什么的不压缩,同时也要注意,我们使用gzi的功能是需要消耗CPU的!
gzion;
gzi_n_length2k;
gzi_buffers432k;
gzi_htt_version1.1;
gzi_com_level6;gzi_tyestextlaintextcsstextjavascritacationjsonacationjavascritacationx-javascritacationxml;
gzi_varyon;
gzi_roxiedany;gzion;开启压缩功能gzi_n_length1k;设置允许压缩的页面最小字节数,页面字节数从header头的Content-Length中获取,默认值是0,不管页面多大都进行压缩,建议设置成大于1K,如果小与1K可能会越压越大gzi_buffers432k;压缩缓冲区大小,表示申请4个单位为32K的内存作为压缩结果流缓存,默认值是申请与原始数据大小相同的内存空间来存储gzi压缩结果gzi_htt_version1.1;压缩版本,用于设置识别HTTP协议版本,默认是1.1,目前大部分浏览器已经支持GZIP解压,使用默认即可gzi_com_level6;压缩比例,用来指定GZIP压缩比,1压缩比最小,处理速度最快,9压缩比最大,传输速度快,但是处理慢,也比较消耗CPU资源gzi_tyestextcsstextxmlacationjavascrit;用来指定压缩的类型,‘texthtml’类型总是会被压缩
默认值:gzi_tyestexthtml(默认不对jscss文件进行压缩)
压缩类型,匹配MIME类型进行压缩
不能用通配符text*
(无论是否指定)texthtml默认已经压缩
设置哪压缩种文本文件可参考confme.tyesgzi_varyon;varyheader支持,改选项可以让前端的缓存服务器缓存经过GZIP压缩的页面,例如用Squid缓存经过nginx压缩的数据(9)exis缓存调优
缓存,主要针对于图片,css,js等元素更改机会比较少的情况下使用,特别是图片,占用
带宽大,我们完全可以设置图片在浏览器本地缓存365d,css,js,html可以缓存个10来天,这样用户第一次打开加载慢一点,第二次,就非常快了!缓存的时候,我们需要将需要缓存的拓展名列出来,Exis缓存配置在server字段里面
location~*.(ico|je?g|gif|ng|bm|swf|flv)${
exis30d;
#log_not_foundoff;
access_logoff;
}location~*.(js|css)${
exis7d;
log_not_foundoff;
access_logoff;
}注:log_not_foundoff;是否在error_log中记录不存在的错误。默认是
总结:
exi功能优点:exis可以降低网站购买的带宽,节约成本
同时提升用户访问体验
减轻服务的压力,节约服务器成本,是web服务非常重要的功能exi功能缺点:被缓存的页面或数据更新了,用户看到的可能还是旧的内容,反而影响用户体验解决办法:第一个缩短缓存时间,例如:1天,但不彻底,除非更新频率大于1天;
第二个对缓存的对象改名网站不希望被缓存的内容:网站流量统计工具
更新频繁的文件(10)防盗链
防止别人直接从你网站引用图片等链接,消耗了你的资源和网络流量,那么我们的解决办法
由几种:水印,品牌宣传,你的带宽,服务器足够
防火墙,直接控制,前提是你知道IP来源
防盗链策略下面的方法是直接给予404的错误提示location~*^.+.(jg|gif|ng|swf|flv|wma|wmv|asf|m3|mmf|zi|rar)${
vad_fersnoneblocked192.168.1.20;
if($invad_fer){
#turn302htt:192.168.1.20imgnonk.jg;
turn404;
eak;
}
access_logoff;
}(11)内核参数优化
[root@nginx~]#vimetcsysctl.conf
fs.file-max=999999
net.iv4.i_forward=0
net.iv4.conf.defat.r_filter=1
net.iv4.conf.defat.accet_source_route=0
kernel.sysrq=0
kernel.co_uses_id=1
net.iv4.tc_syncookies=1
kernel.msgmnb=65536
kernel.msgmax=65536
kernel.shmmax=68719476736
kernel.shmall=4294967296
net.iv4.tc_max_tw_buckets=6000
net.iv4.tc_sack=1
net.iv4.tc_window_scang=1
net.iv4.tc_rmem=102408738012582912
net.iv4.tc_wmem=102408738012582912
net.co.wmem_defat=8388608
net.co.rmem_defat=8388608
net.co.rmem_max=16777216
net.co.wmem_max=16777216
net.co.netdev_max_backlog=262144
net.co.somaxconn=40960
net.iv4.tc_max_orhans=3276800
net.iv4.tc_max_syn_backlog=262144
net.iv4.tc_timestams=0
net.iv4.tc_synack_tries=1
net.iv4.tc_syn_tries=1
net.iv4.tc_tw_cycle=1
net.iv4.tc_tw_use=1
net.iv4.tc_mem=94500000915000000927000000
net.iv4.tc_fin_timeout=1
net.iv4.tc_keeave_time=30
net.iv4.i_local_ort_range=102465000
[root@nginx~]#sysctl-fs.file-max=999999这个参数表示进程(比如一个worker进程)可以同时打开的最大句柄数,这个参数直线限制最大并发连接数,需根据实际情况配置net.iv4.tc_max_tw_buckets=6000这个参数表示操作系统允许TIME_WAIT套接字数量的最大值,如果超过这个数字,TIME_WAIT套接字将立刻被清除并打印警告信息。该参数默认为180000,过多的TIME_WAIT套接字会使Web服务器变慢
注:主动关闭连接的服务端会产生TIME_WAIT状态的连接net.iv4.i_local_ort_range=102465000允许系统打开的端口范围net.iv4.tc_tw_cycle=1启用timewait快速回收net.iv4.tc_tw_use=1开启重用。允许将TIME-WAITsockets重新用于新的TCP连接。这对于服务器来说很有意义,因为服务器上总会有大量TIME-WAIT状态的连接net.iv4.tc_keeave_time=30这个参数表示当keeave启用时,TCP发送keeave消息的频度。默认是2小时,若将其设置的小一些,可以更快地清理无效的连接net.iv4.tc_syncookies=1开启SYNCookies,当出现SYN等待队列溢出时,启用cookies来处理net.co.somaxconn=40960web应用中sten函数的backlog默认会给我们内核参数的net.co.somaxconn限制到128,而nginx定义的NGX_LISTEN_BACKLOG默认为511,所以有必要调整这个值
注:
对于一个TCP连接,Server与Cent需要通过三次握手来建立网络连接.当三次手成后,我们可以看到端口的状态由LISTEN转变为ESTABLISHED,接着这条链路上就可以开始传送数据了.每一个处于监听(Listen)状态的端口,都有自己的监听队列.监听队列的长度与如somaxconn参数和使用该端口的程序中sten()函数有关
somaxconn参数:定义了系统中每一个端口最大的监听队列的长度,这是个全局的参数,默认值为128,对于一个经常处理新连接的高负载web服务环境来说,默认的128太小了。大多数环境这个值建议增加到1024或者更多。大的侦听队列对防止拒绝服务DoS攻击也会有所帮助net.co.netdev_max_backlog=262144每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目net.iv4.tc_max_syn_backlog=262144这个参数标示TCP三次握手建立阶段接受SYN请求队列的最大长度,默认为1024,将其设置得大一些可以使出现Nginx繁忙来不及accet新连接的情况时,Linux不至于丢失客户端发起的连接请求net.iv4.tc_rmem=102408738012582912这个参数定义了TCP接受缓存(用于TCP接受滑动窗口)的最小值、默认值、最大值net.iv4.tc_wmem=102408738012582912这个参数定义了TCP发送缓存(用于TCP发送滑动窗口)的最小值、默认值、最大值net.co.rmem_defat=6291456这个参数表示内核套接字接受缓存区默认的大小net.co.wmem_defat=6291456这个参数表示内核套接字发送缓存区默认的大小net.co.rmem_max=12582912这个参数表示内核套接字接受缓存区的最大大小net.co.wmem_max=12582912这个参数表示内核套接字发送缓存区的最大大小net.iv4.tc_syncookies=1该参数与性能无关,用于解决TCP的SYN攻击(12)关于系统连接数的优化:
nux默认值oenfiles为1024】
说明server只允许同时打开1024个文件】
使用it-a可以查看当前系统的所有限制值,使用it-n可以查看当前的最大打开文
件数
新装的nux默认只有1024,当作负载较大的服务器时,很容易遇到error:toomanyoenfiles。因此,需要将其改大
在etcsecurityts.conf最后增加:
*softnofile65535
*hardnofile65535
*softnoroc65535
*hardnoroc65535二、部署LNMP
软件连接
提取码:vzsu
1、安装h
(1)解决依赖关系
[root@nginx~]#yum-yinstallbxml2-develbcurl-develoenssl-develbzi2-devel安装bmcyt
[root@nginxbmcryt-2.5.7]#.configu--fix=usrlocalbmcryt&am;&am;make&am;&am;makeinstall(2)编译安装h
[root@nginx~]#tarzxfh-5.6.27.tar.gz
[root@nginx~]#cdh-5.6.27
[root@nginxh-5.6.27]#.configu--fix=usrlocal.6--with-mysql=mysqlnd
>--with-do-mysql=mysqlnd--with-mysq=mysqlnd--with-oenssl--enable-fm
>--enable-sockets--enable-sysvshm--enable-mbstring--with-fetye-dir--with-jeg-dir
>--with-ng-dir--with-zb--with-bxml-dir=usr--enable-xml--with-mhash
>--with-mcryt=usrlocalbmcryt--with-config-file-ath=etc
>--with-config-file-scan-dir=etch.d--with-bz2--enable-maintainer-zts
[root@nginxh-5.6.27]#make&am;&am;makeinstall(3)提供h配置文件
[root@nginxh-5.6.27]#ch.ini-roductionetch.ini(4)为h-fm提供脚本
[root@nginxh-5.6.27]#csaifminit.d.h-fmetcinit.dh-fm
[root@nginxh-5.6.27]#chmod+xetcinit.dh-fm
[root@nginxh-5.6.27]#chkconfig--addh-fm
[root@nginxh-5.6.27]#chkconfigh-fmon(5)提供h-fm配置文件并编辑
[root@nginxh-5.6.27]#cusrlocal.6etch-fm.conf.defatusrlocal.6etch-fm.conf
[root@nginxh-5.6.27]#vimusrlocal.6etch-fm.conf
#修改内容如下
id=runh-fm.id
sten=0.0.0.0:9000
m.max_childn=50
m.start_servers=5
m.n_sa_servers=5
m.max_sa_servers=35启动h-fm服务
[root@nginxh-5.6.27]#serviceh-fmstart
Startingh-fmdone
[root@nginxh-5.6.27]#netstat-ant|gh-fm
tc000.0.0.0:90000.0.0.0:*LISTEN125567h-fm:mas
[root@nginx~]#fiwall-cmd--ermanent--add-ort=9000tc
success
[root@nginx~]#fiwall-cmd--load
Success在nginx.conf文件的server中添加下面内容支持h
[root@nginx~]#vimusrlocalnginx1.10confnginx.conf
location{
roothtml;
indexindex.hindex.htmlindex.htm;
}location~.*.(h|)?${
roothtml;
fastcgi_ass127.0.0.1:9000;
fastcgi_indexindex.h;
includefastcgi.conf;
fastcgi_cachecache_fastcgi;
fastcgi_cache_vad2003021h;
fastcgi_cache_vad3011d;
fastcgi_cache_vadany1m;
fastcgi_cache_n_uses1;
fastcgi_cache_use_staleerrortimeoutinvad_headerhtt_500;
fastcgi_cache_keyhtt:$host$quest_uri;
}重载nginx服务
[root@nginx~]#nginx-sload下面是nginx.conf的一个完整配置文件
[root@nginx~]#catusrlocalnginx1.10confnginx.confusernginxnginx;
worker_rocesses2;
worker_cu_affinity0110;
worker_rt_nofile65535;#error_loglogserror.log;
#error_loglogserror.lognotice;
#error_loglogserror.loginfo;#idlogsnginx.id;
events{
useeoll;
worker_connections65535;
mti_acceton;
}
htt{
includeme.tyes;
defat_tyeacationoctet-stam;#log_formatmain'$mote_addr-$mote_user[$time_local]"$quest"'
#'$status$body_bytes_sent"$htt_fer"'
#'"$htt_user_agent""$htt_x_forwarded_for"';#access_loglogsccess.logmain;sendfileon;
tc_noushon;#keeave_timeout0;
keeave_timeout65;
tc_nodelayon;
cent_header_buffer_size4k;
oen_file_cachemax=102400inactive=20s;
oen_file_cache_vad30s;
oen_file_cache_n_uses1;
cent_header_timeout15;
cent_body_timeout15;
set_timedout_connectionon;
send_timeout15;
server_tokensoff;
cent_max_body_size10m;
fastcgi_connect_timeout600;
fastcgi_send_timeout600;
fastcgi_ad_timeout600;
fastcgi_buffer_size64k;
fastcgi_buffers464k;
fastcgi_busy_buffers_size128k;
fastcgi_tem_file_write_size128k;
fastcgi_tem_athusrlocalnginx1.10nginx_tm;
fastcgi_intercet_errorson;
fastcgi_cache_athusrlocalnginx1.10fastcgi_cachelevels=1:2keys_zone=cache_fastcgi:128minactive=1dmax_size=10g;gzion;
gzi_n_length2k;
gzi_buffers432k;
gzi_htt_version1.1;
gzi_com_level6;
gzi_tyestextlaintextcsstextjavascritacationjsonacationjavascritacationx-javascritacationxml;
gzi_varyon;
gzi_roxiedany;server{
sten80;
server_namelocalhost;#charsetkoi8-r;#access_loglogshost.access.logmain; location~*^.+.(jg|gif|ng|swf|flv|wma|wmv|asf|m3|mmf|zi|rar)${
vad_fersnoneblocked192.168.1.20;
if($invad_fer){
#turn302htt:192.168.1.20imgnonk.jg;
turn404;
eak;
}
access_logoff;
}
location{
roothtml;
indexindex.hindex.htmlindex.htm;
}

location~*.(ico|je?g|gif|ng|bm|swf|flv)${
exis30d;
#log_not_foundoff;
access_logoff;
} location~*.(js|css)${
exis7d;
log_not_foundoff;
access_logoff;
}
location~.*.(h|)?${
roothtml;
fastcgi_ass127.0.0.1:9000;
fastcgi_indexindex.h;
includefastcgi.conf;
fastcgi_cachecache_fastcgi;
fastcgi_cache_vad2003021h;
fastcgi_cache_vad3011d;
fastcgi_cache_vadany1m;
fastcgi_cache_n_uses1;
fastcgi_cache_use_staleerrortimeoutinvad_headerhtt_500;
fastcgi_cache_keyhtt:$host$quest_uri;
}

#error_age404404.html;#dictservererroragestothestaticage50x.html
#
error_age50050250350450x.html;
location=50x.html{
roothtml;
}#roxythePHPscritstoAachesteningon127.0.0.1:80
#
#location~.h${
#roxy_asshtt:127.0.0.1;
#}#assthePHPscritstoFastCGIserversteningon127.0.0.1:9000
#
#location~.h${
#roothtml;
#fastcgi_ass127.0.0.1:9000;
#fastcgi_indexindex.h;
#fastcgi_aramSCRIPT_FILENAMEscrits$fastcgi_scrit_name;
#includefastcgi_arams;
#}#denyaccessto.htaccessfiles,ifAache'sdocumentroot
#concurswithnginx'sone
#
#location~.ht{
#denyall;
#}
}
#anothervirtualhostusingxofIP-,name-,andort-basedconfiguration
#
#server{
#sten8000;
#stensomename:8080;
#server_namesomenameaasanother.aas;#location{
#roothtml;
#indexindex.htmlindex.htm;
#}
#}
#HTTPSserver
#
#server{
#sten443ssl;
#server_namelocalhost;#ssl_certificatecert.em;
#ssl_certificate_keycert.key;#ssl_session_cacheshad:SSL:1m;
#ssl_session_timeout5m;#ssl_cihersHIGH:!aNULL:!MD5;
#ssl_fer_server_ciherson;#location{
#roothtml;
#indexindex.htmlindex.htm;
#}
#}}三、验证、压力测试
1、验证防盗链
使用httd做为一个测试站点,192.168.1.30,在测试页上做一个超链接,链接nginx站点的一张图片
[root@httd~]#vimvarwwwhtmlindex.html
<ahf="htt:192.168.1.2011.gif">anjie<>Nginx站点的网页目录结如下
[root@nginx~]#teusrlocalnginx1.10html
usrlocalnginx1.10html
├──11.gif
├──50x.html
├──img
│└──nonk.jg
├──index.html
└──test.h在客户端浏览器中输入192.168.1.30点击页面链接将turn的404关闭,指定跳转文件
turn302htt:192.168.1.20imgnonk.jg;
#turn404;11.gif图片nonk.jg图片根据防盗链的设置,会跳转到nonk.jg图片
配置已经生效
2、验证gzi功能用户访问test.h文件,在上图中content-encoding:gzi表明响应给用户的数据是压缩传输
3、压力测试
安装httd-tools软件包
[root@nginx~]#yum-yinstallhttd-tools
[root@nginx~]#ab-c500-n50000htt:192.168.1.20index.html
ThisisAacheBench,Version2.3<$Revision:1430300$>
Coyright1996AdamTwiss,ZeusTechnologyLtd,htt:www.zeustech.net
LicensedtoTheAacheSoftwaFoundation,htt:www.aache.orgBenchmarking192.168.1.20(beatient)
Comleted5000quests
Comleted10000quests
Comleted15000quests
Comleted20000quests
Comleted25000quests
Comleted30000quests
Comleted35000quests
Comleted40000quests
Comleted45000quests
Comleted50000quests
Finished50000quests
ServerSoftwa:IIS
ServerHostname:192.168.1.20
ServerPort:80DocumentPath:index.html
DocumentLength:612bytesConcurncyLevel:500
Timetakenfortests:2.544seconds
Comletequests:50000
Failedquests:0
Writeerrors:0
Totaltransferd:41800000bytes
HTMLtransferd:30600000bytes
Requestsersecond:19657.71[#sec](mean)
Timeerquest:25.435[ms](mean)
Timeerquest:0.051[ms](mean,acrossallconcurntquests)
Transferrate:16048.68[Kbytessec]ceivedConnectionTimes(ms)
nmean[+-sd]medianmax
Connect:0103.91020
Processing:9164.01529
Waiting:081.0812
Total:15251.52536Percentageofthequestsservedwithinacertaintime(ms)
50%25
66%26
75%26
80%26
90%27
95%28
98%29
99%30
100%36(longestquest)第二次压力测试,比较两次的差异
[root@nginx~]#ab-c1000-n100000htt:192.168.1.20index.html
ThisisAacheBench,Version2.3<$Revision:1430300$>
Coyright1996AdamTwiss,ZeusTechnologyLtd,htt:www.zeustech.net
LicensedtoTheAacheSoftwaFoundation,htt:www.aache.orgBenchmarking192.168.1.20(beatient)
Comleted10000quests
Comleted20000quests
Comleted30000quests
Comleted40000quests
Comleted50000quests
Comleted60000quests
Comleted70000quests
Comleted80000quests
Comleted90000quests
Comleted100000quests
Finished100000quests
ServerSoftwa:IIS
ServerHostname:192.168.1.20
ServerPort:80DocumentPath:index.html
DocumentLength:612bytesConcurncyLevel:1000
Timetakenfortests:5.633seconds
Comletequests:100000
Failedquests:0
Writeerrors:0
Totaltransferd:83600000bytes
HTMLtransferd:61200000bytes
Requestsersecond:17753.07[#sec](mean)
Timeerquest:56.328[ms](mean)
Timeerquest:0.056[ms](mean,acrossallconcurntquests)
Transferrate:14493.71[Kbytessec]ceivedConnectionTimes(ms)
nmean[+-sd]medianmax
Connect:0178.01744
Processing:9397.93969
Waiting:0245.42448
Total:28565.956101Percentageofthequestsservedwithinacertaintime(ms)
50%56
66%58
75%59
80%60
90%62
95%64
98%68
99%75
100%101(longestquest)
第一次:Requestsersecond:19657.71[#sec](mean)
第二次:Requestsersecond:17753.07[#sec](mean)5、xcache加速h
(1)安装xcache
[root@nginx~]#wgethtt:xcache.ghttd.netubReleases3.2.0xcache-3.2.0.tar.gz
[root@nginx~]#tarzxfxcache-3.2.0.tar.gz
[root@nginx~]#cdxcache-3.2.0
#用hize生成configu配置文件
[root@nginxxcache-3.2.0]#usrlocal.6binhize
[root@nginxxcache-3.2.0]#.configu--enable-xcache--enable-xcache-coverager--enable-xcache-otizer--with-h-config=usrlocal.6binh-config
[root@nginxxcache-3.2.0]#make&am;&am;makeinstall安装完成之后,出现下面的界面,记住以下路径,后面会用到
Instalngshadextensions:usrlocal.6bhextensionsno-debug-zts-20131226(2)创建xcache缓存文件
[root@nginxxcache-3.2.0]#touchtmxcache
[root@nginxxcache-3.2.0]#chmod777tmxcache(3)拷贝xcache后台管理程序到网站根目录
[root@nginxxcache-3.2.0]#c-rhtdocsusrlocalnginx1.10htmlxcache(4)配置h支持xcache
[root@nginx~]#vimetch.ini
#最后一样添加
[xcache-common]
extension=usrlocal.6bhextensionsno-debug-zts-20131226xcache.so
#注意目录
[xcache.adn]
xcache.adn.enable_auth=Off
[xcache]
xcache.shm_scheme="mma"
xcache.size=60M
xcache.count=1
xcache.slots=8K
xcache.ttl=0
xcache.gc_interval=0
xcache.var_size=64M
xcache.var_count=1
xcache.var_slots=8K
xcache.var_ttl=0
xcache.var_maxttl=0
xcache.var_gc_interval=300
xcache.test=Off
xcache.adonly_rotection=Off
xcache.mma_ath="tmxcache"
xcache.codum_dictory=""
xcache.cacher=On
xcache.stat=On
xcache.otizer=Off
[xcache.coverager]
xcache.coverager=On
xcache.coveragedum_dictory=""6、测试
重启h-fm
[root@nginx~]#serviceh-fmstart
Graceflyshuttingdownh-fm.done
Startingh-fmdone浏览器打开网站根目录下面的xcache测试对h动态页面的压力测试
[root@nginx~]#ab-c1000-n100000htt:192.168.1.20test.h
ThisisAacheBench,Version2.3<$Revision:1430300$>
Coyright1996AdamTwiss,ZeusTechnologyLtd,htt:www.zeustech.net
LicensedtoTheAacheSoftwaFoundation,htt:www.aache.orgBenchmarking192.168.1.20(beatient)
Comleted10000quests
Comleted20000quests
Comleted30000quests
Comleted40000quests
Comleted50000quests
Comleted60000quests
Comleted70000quests
Comleted80000quests
Comleted90000quests
Comleted100000quests
Finished100000quests
ServerSoftwa:IIS
ServerHostname:192.168.1.20
ServerPort:80DocumentPath:test.h
DocumentLength:84586bytesConcurncyLevel:1000
Timetakenfortests:9.496seconds
Comletequests:100000
Failedquests:0
Writeerrors:0
Totaltransferd:8476300000bytes
HTMLtransferd:8458600000bytes
Requestsersecond:10531.17[#sec](mean)
Timeerquest:94.956[ms](mean)
Timeerquest:0.095[ms](mean,acrossallconcurntquests)
Transferrate:871732.00[Kbytessec]ceivedConnectionTimes(ms)
nmean[+-sd]medianmax
Connect:0147.41358
Processing:138112.381146
Waiting:2269.22579
Total:359511.793165Percentageofthequestsservedwithinacertaintime(ms)
50%93
66%97
75%100
80%103
90%110
95%114
98%124
99%130
100%165(longestquest)
Requestsersecond:10531.17[#sec](mean)