Nginx软件优化

1、隐藏nginx版本信息

1、配置
# cat nginx.conf
  server_tokens off;     #隐藏版本号
2、官网
http://nginx.org/en/docs/http/ngx_http_core_module.html#server_tokens

3、测试
# curl -I 127.0.0.1
  Server: nginx

2、修改nginx版本信息

1、修改源码内核信息
# vim  src/core/nginx.h
  13 #define NGINX_VERSION      "1.0"
  14 #define NGINX_VER          "clsn/" NGINX_VERSION
  22 #define NGINX_VAR          "clsn"
  
2、修改头部信息
# vim  src/http/ngx_http_header_filter_module.c 
  49 static char ngx_http_server_string[] = "Server: clsn" CRLF;

3、修改错误页显示
# vim src/http/ngx_http_special_response.c 
  28 static u_char ngx_http_error_tail[] =
  29 "<hr><center> clsn </center>" CRLF

4、重新编译
# ./configure  --prefix=/opt/nginx --user=www --group=www

5、测试
# curl -I 127.0.0.1 
  Server: clsn

3、修改worker进程的用户

方法1:
# useradd -s /sbin/nologin -M www
# ./configure --user=www --group=www

方法2:
# cat nginx.conf
  user  www www;             

官方:http://nginx.org/en/docs/ngx_core_module.html#user

 4、设置文件的上传大小

# cat nginx.conf
http {
client_max_body_size 8m; #默认1M }
#### context:http,server,location        #可放置的位置  

5、访问控制

1、禁止访问指定的信息
# cat nginx.conf
  location ~ ^/images/.*\.(php|php5|sh|pl|py|html)$ 
    { 
        deny all;
    } 
  location ~ ^/static/.*\.(php|php5|sh|pl|py)$ 
    { 
        deny all;
    } 
  location ~* ^/data/(attachment|avatar)/.*\.(php|php5)$ 
    { 
        deny all;
    } 

2、禁止访问并跳转
# cat nginx.conf
  location ~* \.(txt|doc)$ {
    if (-f $request_filename){
       root /data/www/www;
       rewrite ^/(.*) http://www.baidu.com/$1 permanent;
       break;                #重写后停止,不再匹配
     }
   }

3、禁止ip地址访问
# cat nginx.conf
  location / { 
      deny 192.168.1.1;
      allow 192.168.1.0/24;
      deny all;
  }
# cat nginx.conf
  if ($remote_addr = 10.0.0.7 ){
        return 403;
  }

6、 禁止非法域名解析访问

方法1:
# cat nginx.conf
  server {
    listen 80;
    server_name - ;        #配置虚拟主机区块
    return 501;
  }

方法2:
# cat nginx.conf
  server {
    listen 80 default_server;
    server_name _;
    rewrite ^(.*) http://www.abc.com/$1 permanent;
  }

方法3:
# cat nginx.conf
  server {
if ($host !~ ^www\.abc\.com$) { rewrite ^(.*) http://www.abc.com/$1 permanent; }
}

7、防盗链

利用referer,实现防盗链
# cat nginx.conf           
    location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {
        valid_referers none blocked server_names *.abc.com abc.com;
            if ($invalid_referer){
            #rewrite ^/ http://www.abc.com/123.jpg;    #不推荐,增加服务器负担
            return 403;
        }
    }

8、修改错误页面

1、403自动跳转
# cat nginx.conf      
  error_page  403  /403.html;                #当出现403时,自动跳转到根目录页面

2、500自动跳转
# cat nginx.conf 
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
      root   /data/html;                     #单独目录      
  }

3、改状态码
# cat nginx.conf
  error_page 404 =200 /empty.gif;
    location / {
        root   /data/www/bbs;
        fastcgi_intercept_errors on;         #开启自定义错误页面
        error_page  404 =200    /ta.jpg;               
    }
    
4、错误状态码重定向
# cat nginx.conf
  error_page 404  https://www.qq.com;        #当出现404时,自动跳转到指定的网址 

9、Nginx目录权限优化

服务器角色

权限处理

安全系数

 

 

 

动态Web集群

目录权限755

文件权限644

所用的目录,以及文件用户和组都是root

环境为Nginx+PHP   文件不能被改,目录不能被写入,安全系数10

static图片集群

目录权限755

文件权限644

所用的目录,以及文件用户和组都是root

环境为Nginx    文件不能被改,目录不能被写入,安全系数10

上传upload集群

目录权限755

文件权限644

所用的目录,以及文件用户和组都是root

特别:用户上传的目录设置为755,用户和组使用Nginx服务配置的用户    

文件不能被改,目录不能被写入,但是用户上传的目录允许写入文件且需要通过Nginx的其他功能来禁止读文件,安全系数8

10、Nginx防爬虫优化

方法1:阻止下载协议代理
# cat nginx.conf
  if ($http_user_agent ~* LWP::Simple|BBBike|wget)
   {
      return 403;
  }
  
方法2:禁止爬虫的ua代理访问网站
# cat nginx.conf
  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")
  {
      return 403;
  }
  

11、限制HTTP的请求方法

# cat nginx.conf
if
($request_method !~ ^(GET|HEAD|POST)$ ) { return 501; }

12、 使用普通用户启动nginx

1、创建普通用户
# useradd test
# chown -R test  /opt/nginx

2、修改配置,普通用户不能使用1024一下端口
$ cat nginx.conf
  server {
      listen       8080;
      server_name  www.abc.com;
      root   /home/nginx/blog/html;
      location / {
          index  index.html index.htm;
  }
  
3、普通用户启动
$ /opt/nginx/sbin/nginx -t
$ /opt/nginx/sbin/nginx 
$ /opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf

13、优化worker进程个数

1、nginx两个重要进程
master进程:控制nginx服务的启动、停止、重启
worker进程:处理用户请求信息

2、修改worker进程数
# cat nginx.conf
  worker_processes  1;          #推荐worker数==cpu核数

3、查看cpu核数
# top 按1
# lscpu |grep CPU
# grep processor /proc/cpuinfo    

14、nginx绑定到不同的CPU

1、4个worker进程绑定CPU
# cat nginx.conf
  worker_processes    4;                     #4个进程
  worker_cpu_affinity 0001 0010 0100 1000;   #绑定4个核数  
  
2、8个worker进程绑定CPU
# cat nginx.conf
  worker_processes    8;
  worker_cpu_affinity 0001 0010 0100 1000 0001 0010 0100 1000; 
  worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 100000003、4个worker进程绑定2个cpu核数
# cat nginx.conf
  worker_processes    4;
  worker_cpu_affinity 0101 1010

15、优化nginx事件处理模型

# cat nginx.conf
  use epoll;           #使用epoll模型处理

16、修改nginx单个进程最大连接数

1、查看nginx打开文件数
# lsof -i:80

2、查看系统打开文件数
# ulimit -a

3、修改最大连接数方法
# cat nginx.conf
  events                             #Nginx的工作模式及连接数上限
  {
    worker_connections 1024;         #单个进程的连接数,不超过系统打开文件数
  }

4、修改worker进程打开文件数
# cat nginx.conf
  worker_rlimit_nofile 2048;   

5、其他
# cat nginx.conf
server_names_hash_bucket_size;     #服务器名字的hash表大小

17、优化nginx高效文件传输模式

方法1:
# cat nginx.conf
  sendfile on;        #开启高效文件传输模式
  tcp_nopush;         #数据包积攒到一定量时,再进行传输
  
方法2:  
# cat nginx.conf
  sendfile on;
  tcp_nodelay;        #默认方式,不管大小多少,只要有数据包,就进行传输 

18、nginx超时参数

# cat nginx.conf
  keepalive_timeout 75;            #应用在长连接,连接超时时间,连接会话保持,超时时间
  client_header_timeout 60;        #客户端发送请求,间隔时间,读取客户端请求头信息,超时时间
  client_body_timeout 60;          #服务端回应请求,间隔时间,读取客户端主体信息,超时时间
  send_timeout 60s;                #客户端回应请求,间隔时间,回应客户端,超时时间

19、gzip压缩

# cat nginx.conf
  gzip on;                          #开启gzip压缩功能
  gzip_min_length       1k;         #压缩最小字节数,默认0,从header的Content-Length中获取
  gzip_buffers       4 16k;         #压缩缓冲区大小,4:缓存空间的个数,16k:每个缓存空间的大小
  gzip_http_version    1.1;         #压缩版本,默认1.1
  gzip_comp_level        4;         #压缩级别,1压缩比最小,最快,9压缩比最大,最慢
  gzip_types  text/css text/xml;    #压缩类型,以空格分隔
  gzip_vary             on;         #给代理服务器加vary标识,客户端浏览器是否要压缩,IE6不支持gzip
  
##官网:http://nginx.org/en/docs/http/ngx_http_gzip_module.html#gzip

20、expires缓存

# cat nginx.conf 
  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
          expires      max;           #缓存10年
          root   html/blog;
  }
  location ~ .*\.(js|css)$ {
          expires       30d;          #缓存30天
          root    html/blog;
  } 
  location / {
        expires -1;                   #不缓存,永久过期
  }
##ms(毫秒),s(秒),m(分钟),h(小时),d(天),w(周), M(月),y(年)

21、配置FastCGI优化

fastcgi_connect_timeout 60;                   #连接超时时间,默认60秒,连接越多,消耗越大
fastcgi_send_timeout 240;                     #请求之间的间隔超时时间
fastcgi_read_timeout 240;                     #接收应答的超时时间
fastcgi_buffer_size 64k;                      #读取应答头信息,缓冲区大小
fastcgi_buffers 4 64k;                        #缓冲区数量及大小
fastcgi_busy_buffers_size 128k;               #繁忙时的缓冲区大小,推荐fastcgi_buffer的两倍,
fastcgi_temp_file_write_size 128k;            #临时文件大小,太小会报502
fastcgi_temp_path /data/ngx_fcgi_tmp;         #临时文件路径
fastcgi_cache ngx_fcgi_cache;                 #开启缓存并指定名称
fastcgi_cache_path /data/ngx_fcgi_cache levels=2:2 keys_zone=ngx_fcgi_cache:512m inactive=1d max_size=40g;
                    缓存路径          生成256*256子目录      缓存名及大小         缓存时间    最大容量
fastcgi_cache_valid 200 302 1h;                #状态200、302,缓存1小时
fastcgi_cache_valid 301 1d;                    #状态301,缓存1天
fastcgi_cache_valid any 1m;                    #任意状态码,缓存1分钟
fastcgi_cache_min_uses 1;                      #请求1次就缓存
fastcgi_cache_use_stale error http_500;        #错误状态码,不缓存
fastcgi_cache_key http://$host$request_uri;    #设置缓存key,host域名),request_uri(请求的路径)
###官网:http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_cache

22、日志切割

# cat nginx_log.sh 
  #!/bin/bash
  nginx_dir=/application/nginx
  cd $nginx_dir/logs
  /bin/mv  access.log  access_$(date +%F -d -1day).log   
  $nginx_dir/sbin/nginx -s reload  
# crontab  -l
00 00 * * * /bin/sh  /data/scripts/nginx_log.sh > /dev/null 2>&1

23、查看软件编译时的参数

查看软件编译时的参数
1、查看nginx参数
# nginx -V
    
2、查看apache参数    
# apachectl -V                                   #查看安装时编译信息,但显示的不全
    
3、查看mysql参数    
# grep CONFIGURE_LINE /app/mysql/bin/mysqlbug    #二进制安装方式,查看不到
    
4、查看php参数    
# ./bin/php -i | grep configure

 24、防DOS攻击

控制IP或域名的访问次数
# cat nginx.conf limit_conn_zone $binary_remote_addr zone
=test:10m; #定义10M内存及名称,限制ip limit_conn_zone $binary_remote_addr zone=test1:10m rate=2r/s; #同个ip,1秒请求2次 limit_conn_zone $server_remote_addr zone=test2:10m; #定义10M内存及名称,限制虚拟主机 server { limit_conn test 10; #限制客户端ip连接数10 limit_req zone=test1 burst=5 nodelay; #burst:队列长度,nodelay:不延时 limit_conn test2 100; #限制虚拟主机处理并发数100 limit_rate 100k; #对每个连接限速100k }

 25、配置nginx用户和密码

1、修改配置文件
# cat nginx.conf
  location /phpmyadmin/ {
    auth_basic     "Please input password";        #验证提示
    auth_basic_user_file /home/nginx/passwd;       #用户和密码文件
  }

2、创建用户和密码
# htpasswd -cb /home/nginx/passwd admin 123456
# cat /home/nginx/conf/htpasswd
  admin:Pl80EhiC2Z/5I
# chmod 400 /home/nginx/passwd
# chown nginx /home/nginx/passwd

 

 

posted on 2018-07-26 17:33  五光十色  阅读(225)  评论(0编辑  收藏  举报

导航