码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx调优

时间:2017-12-05 23:55:37      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:names   两种   nobody   超时   content   swa   结果   站点   tsig   

 目录

软件调优
    1.隐藏 Nginx 版本号
    2.隐藏 Nginx 版本号和软件名
    3.更改 Nginx 服务的默认用户
    4.优化 Nginx worker 进程数
    5.绑定 Nginx 进程到不同的 CPU 上
    6.优化 Nginx 处理事件模型
    7.优化 Nginx 单个进程允许的最大连接数
    8.优化 Nginx worker 进程最大打开文件数
    9.优化服务器域名的散列表大小
    10.开启高效文件传输模式
    11.优化 Nginx 连接超时时间
    12.限制上传文件的大小
    13.FastCGI 相关参数调优
    14.配置 Nginx gzip 压缩
    15.配置 Nginx expires 缓存
    16.优化 Nginx日志(日志切割)
    17.优化 Nginx 站点目录
    18.配置 Nginx 防盗链
    19.配置 Nginx 错误页面优雅显示
    20.优化 Nginx 文件权限
    21.Nginx 防爬虫优化
    22.控制 Nginx 并发连接数
    23. 集群代理优化
系统内核参数优化

Nginx软件调优

1. 隐藏 Nginx 版本号

  为什么要隐藏 Nginx 版本号:一般来说,软件的漏洞都与版本有关,隐藏版本号是为了防止恶意用户利用软件漏洞进行攻击

[root@localhost ~]# cat /usr/local/nginx/conf/nginx.conf
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        server_tokens   off;     # 隐藏版本号
        server {
            listen       80;
            server_name  www.abc.com;
            location / {
                root   html/www;
                index  index.html index.htm;
            }
        }
    }
    ...

[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

[root@localhost ~]# curl -I 127.0.0.1    # 查看是否隐藏版本号
HTTP/1.1 404 Not Found
Server: nginx              
Date: Thu, 25 May 2017 05:23:03 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive

2.隐藏 Nginx 版本号和软件名

  为什么要隐藏 Nginx 版本号和软件名:一般来说,软件的漏洞都与版本有关,隐藏版本号是为了防止恶意用户利用软件漏洞进行攻击,而软件名可以进行修改,否则黑客知道是 Nginx 服务器更容易进行攻击,需要注意的是,隐藏 Nginx 软件名需要重新编译安装 Nginx ,如果没有该方面需求尽量不要做

1) 修改:/usr/local/src/nginx-1.6.3/src/core/nginx.h

#define NGINX_VERSION      "8.8.8.8"                  # 修改为想要显示的版本号
#define NGINX_VER          "Google/" NGINX_VERSION    # 修改为想要显示的软件名
#define NGINX_VAR          "Google"                   # 修改为想要显示的软件名

 2) 修改:/usr/local/src/nginx-1.6.3/src/http/ngx_http_header_filter_module.c

static char ngx_http_server_string[] = "Server: Google" CRLF;           # 修改为想要显示的软件名
static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;

 3) 修改:/usr/local/src/nginx-1.6.3/src/http/ngx_http_special_response.c

static u_char ngx_http_error_full_tail[] =
"<hr><center>" NGINX_VER "(www.google.com)</center>" CRLF    # 此行定义对外展示的内容
"</body>" CRLF
"</html>" CRLF
;

static u_char ngx_http_error_tail[] =
"<hr><center>Google</center>" CRLF       # 此行定义对外展示的软件名
"</body>" CRLF
"</html>" CRLF
;

 4) 重新编译 Nginx

cd /usr/local/src/nginx-1.6.3
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
/usr/local/nginx/sbin/nginx

 3.更改 Nginx 服务的默认用户

  为什么要更改 Nginx 服务的默认用户:就像更改 ssh 的默认 22 端口一样,增加安全性,Nginx 服务的默认用户是 nobody ,我们更改为 nginx
1) 添加 nginx 用户

useradd -s /sbin/nologin -M nginx

 2) 更改 Nginx 配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  1;
user nginx nginx;           # 指定Nginx服务的用户和用户组
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server_tokens   off;
    server {
        listen       80;
        server_name  www.abc.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
}

 3) 重新加载 Nginx

[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

 4) 验证是否生效

[root@localhost ~]# ps aux | grep nginx 
root       8901  0.0  0.1  45036  1784 ?        Ss   13:54   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      8909  0.0  0.1  45460  1828 ?        S    13:59   0:00 nginx: worker process      # Nginx进程的所属用户为nginx

 4.优化 Nginx worker 进程数

  Nginx 有 Master 和 worker 两种进程,Master 进程用于管理 worker 进程,worker 进程用于 Nginx 服务

  worker 进程数应该设置为等于 CPU 的核数,高流量并发场合也可以考虑将进程数提高至 CPU 核数 * 2

[root@localhost ~]# grep -c processor /proc/cpuinfo         # 查看CPU核数
2

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf    # 设置worker进程数
worker_processes  2;
user nginx nginx;
......

[root@localhost ~]# /usr/local/nginx/sbin/nginx -t          # 重新加载Nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

[root@localhost ~]# ps -ef | grep nginx | grep -v grep      # 验证是否为设置的进程数
root       8901      1  0 13:54 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      8937   8901  0 14:14 ?        00:00:00 nginx: worker process      
nginx      8938   8901  0 14:14 ?        00:00:00 nginx: worker process   

 5.绑定 Nginx 进程到不同的 CPU 上

   为什么要绑定 Nginx 进程到不同的 CPU 上 :默认情况下,Nginx 的多个进程有可能跑在某一个 CPU 或 CPU 的某一核上,导致 Nginx 进程使用硬件的资源不均,因此绑定 Nginx 进程到不同的 CPU 上是为了充分利用硬件的多 CPU 多核资源的目的。

[root@localhost ~]# grep -c processor /proc/cpuinfo    # 查看CPU核数
2
worker_processes  2;         # 2核CPU的配置
worker_cpu_affinity 01 10;

worker_processes  4;         # 4核CPU的配置
worker_cpu_affinity 0001 0010 0100 1000;    

worker_processes  8;         # 8核CPU的配置
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 1000000;

[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# cd /usr/local/src/   # 进行压力测试,教程:http://os.51cto.com/art/201202/317803.htm
[root@localhost src]# wget http://home.tiscali.cz/~cz210552/distfiles/webbench-1.5.tar.gz 
[root@localhost src]# tar -zxvf webbench-1.5.tar.gz
[root@localhost src]# cd webbench-1.5
[root@localhost src]# yum install -y ctags gcc
[root@localhost src]# mkdir -m 644 -p /usr/local/man/man1
[root@localhost src]# make && make install
[root@localhost src]# webbench -c 10000 -t 60 http://192.168.5.131/
[root@localhost ~]# top    # 按1查看CPU调度结果,这里是虚拟机测试,效果并不太明显
top - 14:44:46 up 4:40, 3 users, load average: 0.01, 0.32, 0.24
Tasks: 85 total, 1 running, 84 sleeping, 0 stopped, 0 zombie
Cpu0 : 0.8%us, 0.8%sy, 0.0%ni, 97.9%id, 0.3%wa, 0.0%hi, 0.2%si, 0.0%st
Cpu1 : 0.6%us, 0.7%sy, 0.0%ni, 98.1%id, 0.0%wa, 0.0%hi, 0.5%si, 0.0%st
Mem: 1534840k total, 304824k used, 1230016k free, 3932k buffers
Swap: 204792k total, 0k used, 204792k free, 191364k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 
4319 root 20 0 98308 3932 2964 S 3.2 0.3 0:15.76 sshd 
18989 root 20 0 15016 1292 1008 R 1.6 0.1 0:00.04 top 
1 root 20 0 19232 1388 1112 S 0.0 0.1 0:02.19 init 
2 root 20 0 0 0 0 S 0.0 0.0 0:00.08 kthreadd 
3 root RT 0 0 0 0 S 0.0 0.0 0:00.61 migration/0 
4 root 20 0 0 0 0 S 0.0 0.0 0:03.60 ksoftirqd/0 
5 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0 
6 root RT 0 0 0 0 S 0.0 0.0 0:00.50 watchdog/0

 6.优化 Nginx 处理事件模型

  Nginx 的连接处理机制在不同的操作系统会采用不同的 I/O 模型,要根据不同的系统选择不同的事件处理模型,可供选择的事件处理模型有:kqueue 、rtsig 、epoll 、/dev/poll 、select 、poll ,其中 select 和 epoll 都是标准的工作模型,kqueue 和 epoll 是高效的工作模型,不同的是 epoll 用在 Linux 平台上,而 kqueue 用在 BSD 系统中。

(1) 在 Linux 下,Nginx 使用 epoll 的 I/O 多路复用模型
(2) 在 Freebsd 下,Nginx 使用 kqueue 的 I/O 多路复用模型
(3) 在 Solaris 下,Nginx 使用 /dev/poll 方式的 I/O 多路复用模型
(4) 在 Windows 下,Nginx 使用 icop 的 I/O 多路复用模型

[root@localhost ~]# cat /usr/local/nginx/conf/nginx.conf
......
events {
    use epoll;
}
......

 7.优化 Nginx 单个进程允许的最大连接数

 (1) 控制 Nginx 单个进程允许的最大连接数的参数为 worker_connections ,这个参数要根据服务器性能和内存使用量来调整
 (2) 进程的最大连接数受 Linux 系统进程的最大打开文件数限制,只有执行了 "ulimit -HSn 65535" 之后,worker_connections 才能生效
 (3) 连接数包括代理服务器的连接、客户端的连接等,Nginx 总并发连接数 = worker 数量 * worker_connections, 总数保持在3w左右

[root@localhost ~]# cat /usr/local/nginx/conf/nginx.conf
worker_processes  2;
worker_cpu_affinity 01 10;
user nginx nginx;
events {
    use epoll;
    worker_connections  15000;
}
......

 8.优化 Nginx worker 进程最大打开文件数

[root@localhost ~]# cat /usr/local/nginx/conf/nginx.conf
worker_processes  2;
worker_cpu_affinity 01 10;
worker_rlimit_nofile 65535;    # worker 进程最大打开文件数,可设置为优化后的 ulimit -HSn 的结果
user nginx nginx;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server_tokens   off;
    server {
        listen       80;
        server_name  www.abc.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
}

 9.优化服务器域名的散列表大小

    如下,如果在 server_name 中配置了一个很长的域名,那么重载 Nginx 时会报错,因此需要使用 server_names_hash_max_size 来解决域名过长的问题,该参数的作用是设置存放域名的最大散列表的存储的大小,根据 CPU 的一级缓存大小来设置。

server {
        listen       80;
        server_name  www.abcdefghijklmnopqrst.com;        # 配置一个很长的域名
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
[root@localhost conf]# /usr/local/nginx/sbin/nginx -t     # 如果配置的域名很长会出现如下错误
nginx: [emerg] could not build the server_names_hash, you should increase server_names_hash_bucket_size: 64
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
[root@localhost ~]# cat /usr/local/nginx/conf/nginx.conf
......
http {
    include       mime.types;
    server_names_hash_bucket_size  512;     # 配置在 http 区块,默认是 512kb ,一般设置为 cpu 一级缓存的 4-5 倍,一级缓存大小可以用 lscpu 命令查看
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server_tokens off;
    include vhosts/*.conf;
}

 10.开启高效文件传输模式

 (1) sendfile 参数用于开启文件的高效传输模式,该参数实际上是激活了 sendfile() 功能,sendfile() 是作用于两个文件描述符之间的数据拷贝函数,这个拷贝操作是在内核之中的,被称为 "零拷贝" ,sendfile() 比 read 和 write 函数要高效得多,因为 read 和 write 函数要把数据拷贝到应用层再进行操作

 (2) tcp_nopush 参数用于激活 Linux 上的 TCP_CORK socket 选项,此选项仅仅当开启 sendfile 时才生效,tcp_nopush 参数可以允许把 http response header 和文件的开始部分放在一个文件里发布,以减少网络报文段的数量

[root@localhost ~]# cat /usr/local/nginx/conf/nginx.conf
......
http {
    include       mime.types;
    server_names_hash_bucket_size  512;     
    default_type  application/octet-stream;
    sendfile      on;    # 开启文件的高效传输模式
    tcp_nopush    on;    # 激活 TCP_CORK socket 选择
        tcp_nodelay on;	 #数据在传输的过程中不进缓存
    keepalive_timeout  65;
    server_tokens off;
    include vhosts/*.conf;
}

 

 未完待续。。。

 

Nginx调优

标签:names   两种   nobody   超时   content   swa   结果   站点   tsig   

原文地址:http://www.cnblogs.com/zhichaoma/p/7989655.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!