您的位置:首页 > 运维架构 > Nginx

企业级Web Nginx 服务优化(3)

2017-02-13 15:34 651 查看
企业级Web Nginx 服务优化(3)
1.10设置连接的超时时间:
[b]什么是超时?
[/b]
php服务希望短链接,java服务是长连接。

keepalive_timeout 60;


<-设置客户端连接保持回话的超时时间,超过这个时间,服务器会关闭该链接。


tcp_nodelay no;


<-打开tcp_nodelay,在包含了keepalive参数才有效


client_header_timeout 15;


<-设置客户端请求头读取超时时间。如超过这个时间,客户端还没有发送任何数据,Nginx将返回“Request time out (408)”错误。


clinet_body_timeout 15;


<-设置客户端请求头读取超时时间。如超过这个时间,客户端还没有发送任何数据,Nginx将返回“Request time out (408)”错误,默认值是60秒。


send_timeout 15;

<-指定响应客户端的超时时间。这个超时仅限于两个连接活动之间的时间,如果超过这个时间,客户端没有任何活动,Nginx将会关闭连接。

官方说明:










1.11上传文件大小的限制(动态应用)
主配置文件加入如下参数,具体参数大小根据你自己的业务做调整。
client_max_body_size 10m;
官方说明:



HTTP/1.1 403 Forbidden


Server: nginx


Date: Fri, 05 Jun 2015 12:47:45 GMT


Content-Type: text/html


Content-Length: 162


Connection: keep-alive


1.12fastcgi(配合php引擎动态服务)

fastcgi_connect_timeout 300;


<-指定连接到后端FastCGI的超时时间


fastcgi_send_timeout 300;


<-向fastcgi传送请求的超时时间,这个值是指已经完成两次握手后向FastCGI传送请求的超时时间。


fastcgi_read_timeout 300;


<-指定接收Fastcgi应答的超时时间,这个值是指定已经完成两次握手后接收的fastcgi应答的超时时间。


fastcgi_buffer+size 64k;


<-指定读取FastCGI应答的第一部分需要用多大的缓冲区,这个值白哦是将使用1个64kb的缓冲区读取应答的第一部分(应答头),可以设置为fastcgi_buffers选项指定的缓冲区大小。


fastcg_buffers 4 64k;


<-指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答请求。如果一个PHP脚本所产生的页面大小为256kb,那么会为其分配4个64kb的缓冲区来缓存;如果页面大小大于256kb,那么大于256kb的部分会缓存到fastcgi_temp指定 的路径中,但是并不是好的办法,因为内存中的数据处理速度快于硬盘,一般这个值应该为站点中php脚本所产生的页面大小的中间值,如果站点大部分脚本所产生的页面的大小为256kb那么可以把这个设置为“16k” “4 64k” 等








1.13配置nginx zip压缩功能
nginx gzip压缩模块提供了对文件内容压缩的功能,允许nginx服务器将输出内容在发送到客户端之前根据具体的策略进行压缩,以节约万占贷款,同时提升童虎体验。
此功能同apache的mod_deflate压缩功能,以来ngx_http_gzip_nodule模块,默认已安装,我们已经详细讲解过了压缩的功能。
所有程序(js,css,html) 不要压缩的内容(图片,视频 ,FLASH)
1,对应压缩参数的说明如下:
压缩配置


gzip On


<-开启gizp压缩功能


gzip_min_length 1k;


<-设置允许压缩的页面最小字节数,页面字节数从header头的Contet-length中获取。默认是0,不管页面多大进行压缩。建议配置大于1k。如果小于1k可能会月牙越大。


gzip 4 16k;


<-压缩缓冲区大小。白哦是申请4各单位为16k的内存作为压缩结果流缓存,默认值是申请与原生数据大小相同的内存空间来存储gzip压缩的结果。


gzip_http_version 1.0


<-压缩版本(默认是1.1,前端为squid2.5使用1.0)用于设置识别HTTP协议版本,默认是1.1,目前大部分的浏览器都已经支持GZIP解压。默认即可


gzip_comp_level 2;


压缩比率,用来指定GZIP压缩比,1压缩比最小,处理速度最快,9压缩比最大,传输速度快,但处理最慢,也比较小号cpu资源

gzip_types text/plain application/x-javascript text/css application/xml;

<-用来指定压缩的类型,“text/html” 类型总司会被压缩。

gzip_bary on;

<-vary header 支持。该选项可以让前端的缓存服务器缓存经过GZIP压缩的页面,例如使用SQUID缓存经过NGINX压缩数据

2.完整的配置如下:



修改的内容如下:
[root@nginx-01 conf]# nginx -t


nginx: the configuration file /application/nginx1.6.2/conf/nginx.conf syntax is ok


nginx: configuration file /application/nginx1.6.2/conf/nginx.conf test is successful


[root@nginx-01 conf]# cat nginx.conf


worker_processes  4;


worker_cpu_affinity 0001 0010 0100 1000;


events {


worker_connections  1024;


use epoll;


}


http {


server_tokens off;


gzip on;


gzip_min_length 1k;


gzip_buffers     4 32k;


gzip_http_version 1.1;


gzip_comp_level 9;


gzip_types text/html text/css text/xml application/javascripti;

gzip_vary on;


gzip的压缩的情况是根据不同版本可能会不同的,上述的nginx 1.6.2的版本类型的。

查看可以压缩的文件类型在哪里?都是什么?
上方加红的地方写法是按照这个来进行写的。
[root@nginx-01 conf]# cat mime.types




types {


text/html                             html htm shtml;


text/css                              css;


text/xml                              xml;


image/gif                             gif;


image/jpeg                            jpeg jpg;


application/javascript                js;


application/atom+xml                  atom;


application/rss+xml                   rss;




text/mathml                           mml;


text/plain                            txt;


text/vnd.sun.j2me.app-descriptor      jad;


text/vnd.wap.wml                      wml;


text/x-component                      htc;




image/png                             png;


image/tiff                            tif tiff;


image/vnd.wap.wbmp                    wbmp;


image/x-icon                          ico;


image/x-jng                           jng;


image/x-ms-bmp                        bmp;


image/svg+xml                         svg svgz;


image/webp                            webp;




application/font-woff                 woff;


application/java-archive              jar war ear;


application/json                      json;


application/mac-binhex40              hqx;


application/msword                    doc;


application/pdf                       pdf;


application/postscript                ps eps ai;


application/rtf                       rtf;


application/vnd.apple.mpegurl         m3u8;


application/vnd.ms-excel              xls;


application/vnd.ms-fontobject         eot;


application/vnd.ms-powerpoint         ppt;


application/vnd.wap.wmlc              wmlc;


application/vnd.google-earth.kml+xml  kml;


application/vnd.google-earth.kmz      kmz;


application/x-7z-compressed           7z;


application/x-cocoa                   cco;


application/x-java-archive-diff       jardiff;


application/x-java-jnlp-file          jnlp;


application/x-makeself                run;


application/x-perl                    pl pm;


application/x-pilot                   prc pdb;


application/x-rar-compressed          rar;


application/x-redhat-package-manager  rpm;


application/x-sea                     sea;


application/x-shockwave-flash         swf;


application/x-stuffit                 sit;


application/x-tcl                     tcl tk;


application/x-x509-ca-cert            der pem crt;


application/x-xpinstall               xpi;


application/xhtml+xml                 xhtml;


application/xspf+xml                  xspf;


application/zip                       zip;




application/octet-stream              bin exe dll;


application/octet-stream              deb;


application/octet-stream              dmg;


application/octet-stream              iso img;


application/octet-stream              msi msp msm;




application/vnd.openxmlformats-officedocument.wordprocessingml.document    docx;


application/vnd.openxmlformats-officedocument.spreadsheetml.sheet          xlsx;


application/vnd.openxmlformats-officedocument.presentationml.presentation  pptx;




audio/midi                            mid midi kar;


audio/mpeg                            mp3;


audio/ogg                             ogg;


audio/x-m4a                           m4a;


audio/x-realaudio                     ra;




video/3gpp                            3gpp 3gp;


video/mp2t                            ts;


video/mp4                             mp4;


video/mpeg                            mpeg mpg;


video/quicktime                       mov;


video/webm                            webm;


video/x-flv                           flv;


video/x-m4v                           m4v;


video/x-mng                           mng;


video/x-ms-asf                        asx asf;


video/x-ms-wmv                        wmv;


video/x-msvideo                       avi;


}


进行重新启动
[root@nginx-01 conf]# nginx -t


nginx: the configuration file /application/nginx1.6.2/conf/nginx.conf syntax is ok


nginx: configuration file /application/nginx1.6.2/conf/nginx.conf test is successful


[root@nginx-01 conf]# nginx


然后浏览器进行查看如下gzip的压缩以及expires的缓存结果:



这里面是火狐的浏览器需要安装Yslow 的这个扩展组件才是可以的。
需要和不需要的压缩的对象:
大于1K的纯文本文件html,,js ,css xml shtml
图片,视频等不要压缩。因为不但不会减少,在压缩时小号CPU和MEM资源。

nginx的翻向的一个优化 就是没必要的加载的模块是什么?

[root@nginx-01 nginx-1.6.2]# ./configure --help


--without-http_charset_module      disable ngx_http_charset_module


--without-http_gzip_module         disable ngx_http_gzip_module


--without-http_ssi_module          disable ngx_http_ssi_module


--without-http_userid_module       disable ngx_http_userid_module


--without-http_access_module       disable ngx_http_access_module


--without-http_auth_basic_module   disable ngx_http_auth_basic_module


--without-http_autoindex_module    disable ngx_http_autoindex_module


--without-http_geo_module          disable ngx_http_geo_module


--without-http_map_module          disable ngx_http_map_module


--without-http_split_clients_module disable ngx_http_split_clients_module


--without-http_referer_module      disable ngx_http_referer_module


--without-http_rewrite_module      disable ngx_http_rewrite_module


--without-http_proxy_module        disable ngx_http_proxy_module


--without-http_fastcgi_module      disable ngx_http_fastcgi_module


--without-http_uwsgi_module        disable ngx_http_uwsgi_module


--without-http_scgi_module         disable ngx_http_scgi_module


--without-http_memcached_module    disable ngx_http_memcached_module


--without-http_limit_conn_module   disable ngx_http_limit_conn_module


--without-http_limit_req_module    disable ngx_http_limit_req_module


--without-http_empty_gif_module    disable ngx_http_empty_gif_module


--without-http_browser_module      disable ngx_http_browser_module


--without-http_upstream_ip_hash_module


1.14配置nginx expires 缓存功能
在网站开发和运营中,对于图片,css,js等元素更改机会比较少,特别是图片,这时可以将图片设置在浏览器本地缓存365天或者更长,CSSJS,html等待吗缓存10天,这样用户第一次打开页面后,你,会在本地的浏览器缓存相应的上述内容,这样的缓存可以提高下次用户打开类似的页面的加载速度,并节省服务器端的大量资源的带宽,此功能同apache的expire是,我们已经详细讲解过了,这里通过location的,将需要缓存的扩展名列出来,然后指定缓存的时间。
好处:
1.第一次以后,访问网站会很快----->体验就好了
2.节省网站的带宽。。。。成本变低了。
3.服务器的压力降低了。----->成本降低了
坏处:
1.网站如果改版,对应的用户看到的还是旧的。(js,css,图片)
解决坏处的:
1.过期时间短一些
2.资源文件更新的时候,进行改名字就可以了。
图片和附件一般不会被用户修改,如果用户修改了,实际上也都是更改文件名字重新传了而已。
网站升级对于jss,css元素,一般可以改名。把css,jss,推送到CDN。
企业网站案例日期的案例:
1.51cto 1周
2.sina 15天
3.京东 25年
4.淘宝的 10年
一般不希望被缓存的内容:
1)广告图片、
2)网站流量统计文件
3)更新频繁的文件
1.根据文件扩展名进行判断,添加expires功能范例:
[root@nginx-01 conf]# vim nginx.conf

server {
location ~.*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 3650d;
root html/bbs

}
location ~.*\.(js|css)?$
{
expires 30d;
root html/bbs

}
在server里面的标签。
这个时间是跟服务器的时间对应的



单个文件添加expires功能范例:
给robots.txt设置过期时间;这里为robots.txt 为7天并不记录404错误日志












nginx防止爬虫的配置的是什么?



结果如下:






1.2更改源码文件来更改源码名称

更改编译的源码文件:
vim src/http/ngx_http_header_filter_module.c +49


static char ngx_http_server_string[] = "Server: BWS" CRLF;


static char ngx_http_server_full_string[] = "Server: BWS"  CRLF;






static ngx_str_t ngx_http_status_lines[] = {


进行查看编译的参数是什么:
[root@nginx-01 nginx-1.6.2]# nginx -Vnginx version: nginx/1.6.2built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC) TLS SNI support enabledconfigure arguments: --user=nginx --group=nginx --prefix=/application/nginx1.6.2 --with-http_stub_status_module --with-http_ssl_module

修改错误信息的返回页面:
[root@nginx-01 nginx-1.6.2]# vim src/http/ngx_http_special_response.c


static u_char ngx_http_error_tail[] =


"<hr><center>BWS(http://www.baidu.com</center>)" CRLF


"</body>" CRLF


"</html>" CRLF


;


重新编译安装:
安装完成后测试的结果如下:
本机测试:

[root@nginx-01 nginx-1.6.2]# curl -I 127.0.0.1


HTTP/1.1 403 Forbidden


Server: BWS


Date: Fri, 05 Jun 2015 13:55:03 GMT


Content-Type: text/html


Content-Length: 223


Connection: keep-alive


错误页面的显示:




1.3 Nginx 日志相关优化与安全
1.3.1编写脚本实现Nginx access 日志轮训
Nginx没有类似Apache的cronolog日志分割处理的功能,但是,可以通过nginx的信号控制功能或者reload新家在,然后利用脚本来实现日志的自动切割。
详细操作过程如下:
1.配置日志切割脚本
[root@nginx-01 scripts]# cat nginxlog.sh


#!/bin/sh


cd /application/nginx/logs &&\


/bin/mv www_access.log www_access_$(date +%F -d -1day).log


/application/nginx/sbin/nginx -s reload


1.3.2 不记录不需要的访问日志
对于健康检查或某些图片,jss,css的日志,一般不需要记录,因为在同级PV时是按照页面计算。而且日志写入频繁消耗磁盘IO,降低服务性能。



1.3.3访问日志的权限设置
假如日志目录/app/logs,则授权方法、
chown -R root.root /app/logs
chmod -R 700 /app/logs

不需要在日志目录上给nginx用户读或者写许可,这个问题很多网友都没在意,直接给了nginx或apache用户。

最小化apache和nginx目录及文件权限设置
安全的权限:
为了保证apache的网站不遭受木马入侵上传及修改文件。
1.所有站点目录的用户和组都应该为root,
2.所有目录权限755;
3.所有文件的权限644;
注意网站的服务用户不能用root;




以上的权限设置可以做到防止黑客上传木马,以及修改站点文件,但是,合理的用户上传的内容也被据之门外了。那么如何解决可以让合法的用户传文件又不至于被黑客利用攻击呐?
这就是对业务进行分离,在比较好的网站业务架构中,应该把资源文件,包括用户上传的图片,附件等的服务和程序服务分离,最好把上传程序服务也分离,这样就可以从容按照前面安全的标准来进行授权了。
缺一个把资源文件,包括用户上传图片,包括上传程序液分离的图



大多数公司的不是很安全的授权如下:
1)chown -R 777 /sitedir(最不安全的)
2)chown -R apache.apache /sitedir(最不安全的)
如果按照大多数公司授权一般公司的授权,会给网站带来非常大的安全隐患。


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息