nginx优化的一些建议

1.1隐藏Nginx header里版本号信息

1、查看版本号

 
  1. curl -I 127.0.0.1

  2. HTTP/1.1 200 OK

  3. Server: nginx/1.6.2

  4. Date: Sat, 14 Mar 2015 08:15:29 GMT

  5. Content-Type: text/html

  6. Content-Length: 25

  7. Last-Modified: Fri, 13 Mar 2015 10:52:27 GMT

  8. Connection: keep-alive

  9. ETag: "5502c16b-19"

  10. Accept-Ranges: bytes

2、隐藏版本号
在nginx配置文件的http标签内加入“server_tokens off; ”参数,也可以放大server标签和location标签中,如下:

 
  1. http {

  2. ..........

  3. server_tokens off;

  4. ..........

  5. }

  6. /application/nginx/sbin/nginx -t

  7. /application/nginx/sbin/nginx -s reload

再此查看如下,浏览器访问错误页面也就没有版本号了

 
  1. curl -I 127.0.0.1

  2. HTTP/1.1 200 OK

  3. Server: nginx

1.2更改源码隐藏软件名称及版本号

在nginx编译安装之前,先更改,之后再编译安装
1、更改版本号,修改nginx-1.3.4/src/core/nginx.h

 
  1. cd /home/memory/tools/

  2. cd nginx-1.6.2/src/core

  3. sed -n '13,17p'nginx.h

  4. #define NGINX_VERSION "1.6.2" 修改为想要的版本号如2.4.3

  5. #define NGINX_VER "nginx/" NGINX_VERSION 将nginx修改为想要修改的软件名称,如Apache。

修改后查看header结果:

 
  1. curl -I 10.0.0.182

  2. HTTP/1.1 200 OK

  3. Server:Apache/2.4.3

2、修改nginx-1.6.12/src/http/ngx_http_header_filter_module.c需要修改的字符串

 
  1. grep 'Server:nginx' ngx_http_header_filter_module.c

  2. static char ngx_http_server_string[] ="Server:nginx" CRLF;

  3.  
  4. sed -i 's#Server:nginx#Server:Apache#g' ngx_http_header_filter_module.c

修改后的字符串

 
  1. grep 'Server:Apache' ngx_http_header_filter_module.c

  2. static char ngx_http_server_string[] ="Server:Apache" CRLF;

3、修改ngx_http_special_response.c

 
  1. vi nginx-1.3.4/src/http/ngx_http_special_response.c

  2. static u_char ngx_http_error_full_tail[]=

  3. "<hr><center>"NGINX_VER"</center>"CRLF

  4. "</body>"CRLF

  5. "</html>"CRLF

  6. ;

  7. static u_char ngx_http_error_tail[]=

  8. "<hr><center>nginx</center>"CRLF

  9. "</body>"CRLF

  10. "</html>"CRLF

  11. ;

修改为:

 
  1. static u_char ngx_http_error_full_tail[]=

  2. "<hr><center>"NGINX_VER" (https://www.lvtao.net)</center>"CRLF

  3. "</body>"CRLF

  4. "</html>"CRLF

  5. ;

  6. static u_char ngx_http_error_tail[]=

  7. "<hr><center>Apache</center>"CRLF

  8. "</body>"CRLF

  9. "</html>"CRLF

  10. ;

1.3 更改掉nginx默认用户及用户组(worker进程服务用户优化)

1、默认情况下,nginx服务启动,使用的用户和组默认都是nobody,查看默认配置如下:

 
  1. grep '#user' nginx.conf.default

  2. #user nobody;

将web用户改为特殊的用户名如:nginx或更特殊点的dabaojian,但是这个用户必须是系统存在的。
2、建立nginx用户

 
  1. useradd nginx -s /sbin/nologin -M #禁止登陆,无家目录

  2. id nginx

3、配置文件nginx.conf中修改(也可以编译安装时指定默认)
在配置文件最外层上面

 
  1. worker_processes 1;

  2. user nginx;

4、让woker进程使用普通用户运行
为master服务降权:使用非root跑nginx master

 
  1. nginx -c config..

  2. /home/memory/

  3. bin,site,conf,logs

注意:不能用80特权端口 ,前端nginx反向代理转端口

 
  1. /home/nginx/sbin/nginx -c /home/nginx/conf/nginx.conf

  2. nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)

1.4配置nginx worker进程个数

nginx由master和worker进程组成,master进程相当于管理员,worker进程为用户提供服务
一般设置为cpu核数或则核数x2,用top按1查看
修改nginx.conf配置文件第一行

worker_processes 4;

1.5 根据cpu核数进行nginx进程优化

把几个进程分配在一个cup上,cup亲和力
1、不同cpu设置如下
四核cpu配置:

 
  1. worker_processes 4;

  2. worker_cpu_affinity 0001 0010 0100 1000;

八核cpu服务器参数配置:

 
  1. worker_processes 8;

  2.  
  3. worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

  4. worker_cpu_affinity 0001 0010 0100 1000 0001 0010 0100 1000;

2、官方文档说明

 
  1. worker_processes 4;

  2. worker_cpu_affinity 0001 0010 0100 1000;

  3. binds each worker process to a separate CPU, while

  4. worker_processes 2; 4核2进程

  5. worker_cpu_affinity 0101 1010;

1.6 nginx事件处理模型优化

nginx的连接处理机制在不同的操作系统上采用不用的IO模型,在linux下,nginx使用epoll的IO多路复用模型,在freebsd使用kqueue的IO多路复用模型,在solaris使用/dev/pool方式的IO多路复用模型,在windows使用的icop等等。
根据系统类型不同选择不同的事务处理模型,选择有“use [ kqueue | rtsig |epool |dev/pool |select |pllo ];”我们使用的是Centos6.5的linux,因此将nginx的事件处理模型调整为epool模型。
1、具体参数如下在优化4下边挨着:

 
  1. events {

  2. use epoll;

  3. worker_connections 1024;

  4. }

1.7调整nginx worker单个进程允许的客户端最大连接数

这个值根据服务器性能和程序的内存来指定(一个进程启动使用的内存根据程序确定)

 
  1. events {

  2. use epoll;

  3. worker_connections 20480;

  4. }

这个参数是单个进程的最大链接数,实际最大链接数是worker技能书乘以这个数。
Max_client=worker_processes*worker_connections

1.8配置nginx worker进程最大打开文件数

worker_rlimit_nofile 65535;

相当于系统ulimit -HSn,应该是总的。
理念:配置参数不是越大越好,最好设为服务器承受的极限点。

1.9 开启高效的文件传输模式

在http字段设置

 
  1. http {

  2. include mime.types; 媒体类型

  3. default_type application/octet-stream; 默认媒体类型

  4. sendfile on;

  5. tcp_nopush on; 只有在sendfile开启模式下有效

  6. ..........

  7. }

tcp_nopush参数可以允许把http response header和文件的开始放在一个文件里发布,积极的作用是减少网络报文段的数量。From: http://nginx.org/en/docs/http/ngx_http_core_module.html

1.10设置连接超时时间

保护服务器资源,硬件CPU mem,连接数。
建立连接也是要消耗资源的,我们一般断掉那些连上的链接,但是不做事的
PHP网站建议短连接,PHP程序建立连接消耗的资源和时间要少。
JAVA网站建议长连接,JAVA程序建立连接消耗的资源和时间要多。
在http字段设置

 
  1. http {

  2. ..........

  3. keepalive_timeout 60;

  4. #设置客户端连接保持会话的超时时间,超过这个时间,服务器会关闭该连接。

  5.  
  6. tcp_nodelay on;

  7. #打开tcp_nodelay,在包含了keepalive参数才有效

  8.  
  9. client_header_timeout 15;

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

  11.  
  12. client_body_timeout 15;

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

  14.  
  15. send_timeout 15;

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

  17. .....

  18. }

1.11 上传文件大小限制(动态应用)

nginx.conf中http字段添加如下参数,具体大小根据业务做调整
即http协议原理中请求报文的请求主体,此功能在php参数中还有设置。

 
  1. http {

  2. ..........

  3. client_max_body_size 10m;

  4. ......

  5. }

1.12fastcgi调优(配合PHP引擎动态服务)

cache 写入缓存区
buffer 读取缓冲区
fastcgi是静态服务和动态服务之间的一个接口
1、参数详解:有的只能放在http标签

 
  1. fastcgi_connect_timeout 300;

  2. #指定链接到后端FastCGI的超时时间。

  3.  
  4. fastcgi_send_timeout 300;

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

  6.  
  7. fastcgi_read_timeout 300;

  8. #指定接收FastCGI应答的超时时间,这个值是指已经完成两次握手后接收FastCGI应答的超时时间。

  9.  
  10. fastcgi_buffer_size 64k;

  11. #指定读取FastCGI应答第一部分需要用多大的缓冲区,这个值表示将使用1个64KB的缓冲区读取应答的第一部分(应答头),可以设置为gastcgi_buffers选项指定的缓冲区大小。

  12.  
  13. fastcgi_buffers 4 64k;

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

  15.  
  16. fastcgi_busy_buffers_size 128k;

  17. #建议设置为fastcgi_buffer的两倍,繁忙时候的buffer

  18.  
  19. fastcgi_temp_file_write_size 128k;

  20. #在写入fastcgi_temp_path时将用多大的数据库,默认值是fastcgi_buffers的两倍,设置上述数值设置小时若负载上来时可能报502 Bad Gateway

  21.  
  22. fastcgi_cache memory_ngnix;

  23. #表示开启FastCGI缓存并为其指定一个名称。开启缓存非常有用,可以有效降低CPU的负载,并且防止502的错误放生,但是开启缓存也可能会引起其他问题,要很据具体情况选择

  24.  
  25. fastcgi_cache_valid 200 302 1h;

  26. #用来指定应答代码的缓存时间,实例中的值表示将2000和302应答缓存一小时,要和fastcgi_cache配合使用

  27.  
  28. fastcgi_cache_valid 301 1d;

  29. #将301应答缓存一天

  30.  
  31. fastcgi_cache_valid any 1m;

  32. #将其他应答缓存为1分钟

  33.  
  34. fastcgi_cache_min_uses 1;

  35. #请求的数量

  36.  
  37. fastcgi_cache_path

  38. #定义缓存的路径

2、参数配置如下:
修改nginx.conf配置文件
在http标签中添加如下:

 
  1. fastcgi_connect_timeout 300;

  2. fastcgi_send_timeout 300;

  3. fastcgi_read_timeout 300;

  4. fastcgi_buffer_size 64k;

  5. fastcgi_buffers 4 64k;

  6. fastcgi_busy_buffers_size 128k;

  7. fastcgi_temp_file_write_size 128k;

  8. #fastcgi_temp_path /data/ngx_fcgi_tmp;

  9. fastcgi_cache_path /data/ngx_fcgi_cache levels=2:2 keys_zone=ngx_fcgi_cache:512m

  10. inactive=1d max_size=40g;

缓存路径,levels目录层次2级,定义了一个存储区域名字,缓存大小,不活动的数据在缓存中多长时间,目录总大小
在server location标签添加如下:

 
  1. location ~ .*\.(php|php5)?$

  2. {

  3.  
  4. fastcgi_pass 127.0.0.1:9000;

  5. fastcgi_index index.php;

  6. include fastcgi.conf;

  7. fastcgi_cache ngx_fcgi_cache;

  8. fastcgi_cache_valid 200 302 1h;

  9. fastcgi_cache_valid 301 1d;

  10. fastcgi_cache_valid any 1m;

  11. fastcgi_cache_min_uses 1;

  12. fastcgi_cache_use_stale error timeout invalid_header http_500;

  13. fastcgi_cache_key http://$host$request_uri;

  14. }

1.13 配置nginx gzip压缩功能(重要)

1、优点:
a.节约贷款,省钱
b.传输速度快,用户体验好
2、使用模块:
nginx依赖ngx_http_gzip_module模块。
Apache使用的是mod_deflate压缩功能
3、需要压缩的内容:纯文本(js,CSS,html),对于图片,视频,FLASH什么的不压缩,gzip_types参数控制,因为压缩占用cpu啊。
4、对应参数含义如下:

 
  1. gzip on;

  2. ###开启压缩功能

  3.  
  4. gzip_min_length 1k;

  5. ###设置允许压缩的页面最小字节数,页面字节数从header头的Content-Length中获取,默认值是0,不管页面多大都进行压缩,建议设置成大于1K,如果小与1K可能会越压越大。

  6.  
  7. gzip_buffers 4 32k;

  8. ###压缩缓冲区大小,表示申请4个单位为32K的内存作为压缩结果流缓存,默认值是申请与原始数据大小相同的内存空间来存储gzip压缩结果。

  9.  
  10. gzip_http_version 1.1;

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

  12.  
  13. gzip_comp_level 9;

  14. ###压缩比例,用来指定GZIP压缩比,1压缩比最小,处理速度最快,9压缩比最大,传输速度快,但是处理慢,也比较消耗CPU资源。

  15.  
  16. gzip_types text/css text/xml application/javascript;

  17. ###用来指定压缩的类型,‘text/html’类型总是会被压缩。

  18.  
  19. gzip_vary on;

  20. ###vary header支持,改选项可以让前端的缓存服务器缓存经过GZIP压缩的页面,例如用Squid缓存经过nginx压缩的数据。

5、具体配置如下
在http标签中配置

 
  1. gzip on;

  2. gzip_min_length 1k;

  3. gzip_buffers 4 32k;

  4. gzip_http_version 1.1;

  5. gzip_comp_level 9;

  6. gzip_types text/css text/xml application/javascript;

  7. gzip_vary on;

1.14配置nginx expires缓存功能(重要)

对于图片,CSS,JS等元素更改的机会较少,特别是图片,这时可以将图片设置在浏览器本地缓存365天或更长,CSS,JS,html等代码缓存10天,这样用户第一次打开页面后,会在本地缓存上述内容,提高了以后打开的页面加载速度,节省服务端大量贷款,此功能同apache的expires。这里通过location,将需要缓存的扩展名列出来,然后指定缓存时间。
1、根据文件扩展名进行判断,添加expires功能
在server字段添加

 
  1. 范例1:

  2. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

  3. {

  4. expires 3650d;

  5. }

  6. 范例2:

  7. location ~ .*\.(js|css)?$

  8. {

  9. expires 30d;

  10. }

2、根据目录及其他进行判断,添加expires功能范例

 
  1. ## Add expires header according to dir.

  2. location ~ ^/(images|javascript|js|css|flash|media|static)/ {

  3. expires 360d;

  4. }

  5.  
  6. location ~(robots.txt) {

  7. expires 7d;

  8. break;

  9. }

3、expire总结
expire功能优点
1)expires可以降低网站购买的贷款,节约成本
2)同时提升用户访问体验
3)减轻服务的压力,节约服务器成本,甚至可以节约人力成本,是web服务非常重要的功能。
expire功能缺点:
被缓存的页面或数据更新了,用户看到的可能还是旧的内容,反而影响用户体验。
解决办法:
第一个 缩短缓存时间,例如:1天,不彻底,除非更新频率大于1天
第二个 对缓存的对象改名
a.图片,附件一般不会被用户修改,如果用户修改了,实际上也是更改文件名重新传了而已
b.网站升级对于js,css元素,一般可以改名,把css,js,推送到CDN。
企业网站缓存日期案例
1、51cto 1周
2、sina 15天
3、京东 25年
4、淘宝 10年
网站不希望被缓存的内容
1)广告图片
2)网站流量统计工具
3)更新频繁的文件(Google的loGo

1.15nginx日志相关优化与安全

1、配置日志切割脚本并写入计划任务

 
  1. cd /server/scripts/

  2. cat cut_nginx_log.sh

  3.  
  4. #!/bin/sh

  5. cd /app/logs

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

  7. mv bbs_access.log bbs_access_$(date +%F -d -1day).log

  8. mv blog_access.log blog_access_$(date +%F -d -1day).log

  9. /application/nginx/sbin/nginx -s reload

  10. cat >>/var/spool/cron/root>>eof

  11. 00 00 * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&amp;1

  12. eof

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

 
  1. location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$ {

  2. access_log off;

  3. }

3、访问日志的权限设置
假设日志目录为/app/logs,则授权

 
  1. chown -R root.root /app/logs

  2.  
  3. chmod -R 700 /app/logs

不需要再日志目录给nginx用户读或者写许可。因为nginx的master进程是root,不要担心权限不够写不进去日志

1.16 nginx站点目录及文件URL访问控制(重要:防止恶意解析)

1、根据扩展名限制程序和文件访问
作用:禁止目录下指定文件北风文,或者禁止指定目录下所有内容被访问
最佳应用场景:集群的共享存储,本来就应该只是资源文件,禁止指定扩展名程序被执行,例如:.php,.sh,.pl
nginx下禁止访问资源目录下的php程序文件,配置方法如下:
范例1:nginx配置限制指定目录下的php程序被解析

 
  1. location ~ ^/images/.*\.(php|php5|.sh|.pl|.py)$

  2. {

  3. deny all;

  4. }

  5.  
  6. ---------------------------------------

  7.  
  8. ## Only allow these request methods ##

  9. if ($request_method !~ ^(GET|HEAD|POST)$ ) {

  10. return 444;

  11. }

  12. 禁止请求方法

  13.  
  14. ---------------------------------------

  15.  
  16. location ~ ^/static/.*\.(php|php5|.sh|.pl|.py)$

  17. {

  18. deny all;

  19. }

  20.  
  21. location ~* ^/data/(attachment|avatar)/.*\.(php|php5)$

  22. {

  23. deny all;

  24. }

  25.  
  26. 需要放在php解析location的下面

范例2:Nginx下配置禁止访问*.txt文件

 
  1. location ~* \.(txt|doc)$ {

  2. if (-f $request_filename) {

  3. root /data/www/www;

  4. #rewrite …..可以重定向到某个URL

  5. break;

  6. }

  7. }

  8. location ~* \.(txt|doc)${

  9. root /data/www/www;

  10. deny all;

  11. }

  12.  
  13. location ~ ^/(static)/ {

  14. deny all;

  15. }

  16.  
  17. location ~ ^/static {

  18. deny all;

  19. }

  20. location ~ ^/(static|js) {

  21. deny all;

  22. }

  23. location /admin/ { return 404; }

  24. location /templates/ { return 403; }

2、限制来源ip访问
使用ngx_http_access_module限制ip访问
例如:phpmyadmin数据库web客户端,内部开发人员用
范例1:禁止某目录让外界访问,但允许某ip访问改目录,且支持php解析。

 
  1. location ~ ^/memory/ {

  2. allow 127.0.0.1;

  3. deny all;

  4. }

范例2限制及指定ip或ip段访问

 
  1. location / {

  2. deny 192.168.1.1;

  3. allow 192.168.1.0/24;

  4. allow 10.1.1.0/16;

  5. deny all;

  6. }

其他写法

 
  1. if ( $remote_addr = 10.0.0.7 ) {

  2. return 403;

  3. }

企业问题案例:nginx做反向代理的时候可以限制客户端IP吗?
解答:
法1:使用if来控制

 
  1. if ( $remote_addr = 10.0.0.7 ) {

  2. return 403;

  3. }

  4. if ( $remote_addr = 127.0.0.1) {

  5. set $allow_access_root 'true';

  6. }

3、限制使用网站ip访问网站
防止别的网站指定到你的ip,盗用你的流量,占你贷款,假冒你。
方法1:ngnix第一个虚拟主机设置403,不够友好

 
  1. server {

  2. listen 80 default_server;

  3. server_name _;

  4. return 403;

  5. }

方法2:添加301跳转,也是第一个虚拟主机(一般不这么搞,一般用上面的)

 
  1. server {

  2. listen 80 default_server;

  3. server_name _;

  4. rewrite ^(.*) http://www.lvtao.net/$1 permanent;

  5. }

1.17 配置nginx图片及目录防盗链

1、什么是防盗链
简单的说,就是某些不法的网站,通过在其自身网站程序里未经许可非法调用其他网站的资源,然后在自己的网站上显示这些调用的资源,达到了填充自身网站显示的效果,但是浪费了调用资源网站的网站流量,造成其他网站的带宽及服务压力吃紧,甚至宕机。
2、解决办法
1)图片,视频上打水印,品牌。
2)防火墙控制,根据ip控制。
3)防盗链(根据referer控制)
如何及时发现问题
第一、对IDC及CDN带宽做监控报警。
第二、作为高级运维或者运维经理,每天上班的一个重要任务,就是经常查看网站流量图,关注流量变化,关注异常流量。
第三、对访问日志做分析,对于异常流量能迅速定位,并且和公司市场推广等有比较好的默契沟通交流,以便调度贷款和服务器资源。确保网站正常的访问体验得到保证。
3、利用http referer设置防盗链
HTTP Referer是header的一部分,当浏览器向web服务器发送请求的时候,一般会带上Referer,告诉服务器我是从哪个页面链接过来的,服务器籍此可以获得一些信息用于处理。
利用referer并且针对扩展名rewrite重定向

 
  1. location ~* \.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ {

  2. valid_referers none blocked *.lvtao.net;

  3. if ($invalid_referer) {

  4. rewrite ^/ http://www.lvtao.net/img/nolink.jpg;

  5. }

  6. }

说明:设定一个location,如果访问为指定的扩展名文件,则进行判断,如果不是来自我指定的网站,则转交给我提前设置的其他页面及图片
特别说明:防盗链跳转的地址,不能再是设置防盗链的虚拟主机地址,要用第三个虚拟主机,要不就成死循环了!

 
  1. # Stop deep linking or hot linking

  2. location /images/ {

  3. valid_referers none blocked www.example.com example.com;

  4. if ($invalid_referer) {

  5. return 403;

  6. }

  7. }

1.18 nginx错误页面优雅显示

范例:403跳转

 
  1. ###www

  2. server {

  3. listen 80;

  4. server_name www.lvtao.net;

  5. location / {

  6. root html/www;

  7. index index.html index.htm;

  8. }

  9. error_page 403 /403.html; 此路径相对于root html/www;的

  10. #error_page 404 http;//www.lvtao.net/error.html 写法2

  11. #error_page 500 502 503 504 /50x.html;

  12. }

阿里门户天猫网站的nginx优雅显示配置案例:

 
  1. error_page 500 501 502 503 504 http://err.tmall.com/error2.html;

  2. error_page 400 403 404 405 408 410 411 412 413 414 415 http://err.tmall.com/error1.html;

1.19 nginx防爬虫优化

Robots协议(也称为爬虫协议、机器人协议等)的全称是“网络爬虫排除标准”(Robots Exclusion Protocol),网站通过Robots协议告诉搜索引擎哪些页面可以抓取,哪些页面不能抓取。
问题:可能会暴漏网站目录结构
nginx防爬虫优化
nginx根据$http_user_agent获取客户端agent,然后判断是否允许或者返回指定页面

添加如下内容可防止爬虫(防搜索引擎的,但是网站展示宣传就不行了,所以看情况搞)

 
  1. if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot")

  2. {

  3. return 403;

  4. }

实战演示:禁止不同浏览器软件访问:

 
  1. if ($http_user_agent ~* "Firefox|MSIE")

  2. {

  3. return 403;

  4. rewrite ^(.*) http://www.lvtao.net/$1 permanent;

  5. }

1.20 使用tmpfs文件系统给/tmp

提高效率,部分程序切图片操作临时放到/tmp下,可以把tmp设置成内存文件系统,占用内存空间的,就是从内存里拿出一块来当磁盘用

mount -t tmpfs  -o size=16m tmpfs /tmp

1.21防DOS攻击

1、控制单个ip的并发请求防止DOS攻击
使用limit_conn_zone进行控制,控制单个ip或域名的访问次数,限制连续访问<
在http标签添加控制,可添加多个,在server或location中使用

 
  1. limit_conn_zone $binary_remote_addr zone=perip:10m;

  2. limit_conn_zone $server_name zone=perserver:10m;

  3. server {

  4. ...

  5. limit_conn perip 10;

  6. limit_conn perserver 100;

  7. }

  8.  
  9. -----------------------------------

  10.  
  11. limit_conn_zone $binary_remote_addr zone=addr:10m;

  12. server {

  13. location /download/ {

  14. limit_conn addr 1;

  15. }

2、限制单个ip的请求速率防止DOS攻击
使用limit_req_zone进行控制,控制单个ip的访问速率

1.22 防DDOS策略

1.23 限制客户端请求的HTTP方法

 
  1. #Only allow these request methods

  2. if ($request_method !~ ^(GET|HEAD|POST)$ ) {

  3. return 501;

  4. }

  5. #Do not accept DELETE, SEARCH and other methods

1.24 为web服务增加用户身份验证(适合内部机密网址)

控制力度:可以是一个虚拟主机,或一个目录
场景:内部使用的网址,例如phpmyadmin客户端
配置方法如下:
nginx配置:

 
  1. location /phpmyadmin/ {

  2. auth_basic "memory author";

  3. auth_basic_user_file /home/nginx/conf/htpasswd;

  4. }

创建密码文件:

 
  1. htpasswd -cb /home/nginx/conf/htpasswd memory 123456

  2. Adding password for user memory

  3.  
  4. cat /home/nginx/conf/htpasswd

  5. memory:Pl80EhiC2Z/5I

  6.  
  7. chmod 400 /application/nginx/conf/htpasswd

  8. chown nginx /application/nginx/conf/htpasswd

1.26 其他优化

1、安全优化:web磁盘挂载优化
2、内核优化
3、移除不想要的nginx模块(最小化原则)编译时加--without-http-**不安装模块
4、开启iptables防护

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值