Nginx软件调优
1. 隐藏 Nginx 版本号javascript
为何要隐藏 Nginx 版本号:通常来讲,软件的漏洞都与版本有关,隐藏版本号是为了防止恶意用户利用软件漏洞进行攻击php
1css
2html
3前端
4java
5node
6nginx
7web
8算法
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
[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 配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[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 多核资源的目的。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[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/
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[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 进程最大打开文件数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
[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 和文件的开始部分放在一个文件里发布,以减小网络报文段的数量
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[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; } |
11.优化 Nginx 链接超时时间
1. 什么是链接超时
(1) 举个例子,某饭店请了服务员招待顾客,可是如今饭店不景气,所以要解雇掉一些服务员,这里的服务员就至关于 Nginx 服务创建的链接
(2) 当服务器创建的链接没有接收处理请求时,能够在指定的时间内让它超时自动退出
2. 链接超时的做用
(1) 将无用的链接设置为尽快超时,能够保护服务器的系统资源(CPU、内存、磁盘)
(2) 当链接不少时,及时断掉那些创建好的但又长时间不作事的链接,以减小其占用的服务器资源
(3) 若是黑客攻击,会不断地和服务器创建链接,所以设置链接超时以防止大量消耗服务器的资源
(4) 若是用户请求了动态服务,则 Nginx 就会创建链接,请求 FastCGI 服务以及后端 MySQL 服务,设置链接超时,使得在用户容忍的时间内返回数据
3. 链接超时存在的问题
(1) 服务器创建新链接是要消耗资源的,所以,链接超时时间不宜设置得过短,不然会形成并发很大,致使服务器瞬间没法响应用户的请求
(2) 有些 PHP 站点会但愿设置成短链接,由于 PHP 程序创建链接消耗的资源和时间相对要少些
(3) 有些 Java 站点会但愿设置成长链接,由于 Java 程序创建链接消耗的资源和时间要多一些,这时由语言的运行机制决定的
4. 设置链接超时
(1) keepalive_timeout
:该参数用于设置客户端链接保持会话的超时时间,超过这个时间服务器会关闭该链接
(2) client_header_timeout
:该参数用于设置读取客户端请求头数据的超时时间,若是超时客户端尚未发送完整的 header 数据,服务器将返回 "Request time out (408)" 错误
(3) client_body_timeout
:该参数用于设置读取客户端请求主体数据的超时时间,若是超时客户端尚未发送完整的主体数据,服务器将返回 "Request time out (408)" 错误
(4) send_timeout
:用于指定响应客户端的超时时间,若是超过这个时间,客户端没有任何活动,Nginx 将会关闭链接
(5) tcp_nodelay
:默认状况下当数据发送时,内核并不会立刻发送,可能会等待更多的字节组成一个数据包,这样能够提升 I/O 性能,可是,在每次只发送不多字节的业务场景中,使用 tcp_nodelay 功能,等待时间会比较长
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[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; keepalive_timeout 65 ; tcp_nodelay on; client_header_timeout 15 ; client_body_timeout 15 ; send_timeout 25 ; include vhosts/*.conf; } |
12.限制上传文件的大小
client_max_body_size 用于设置最大的容许客户端请求主体的大小,在请求首部中有 "Content-Length" ,若是超过了此配置项,客户端会收到 413 错误,即请求的条目过大
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
worker_processes 2 ; worker_cpu_affinity 01 10 ; user nginx nginx; error_log logs /error.log error; events { use epoll; worker_connections 20480 ; } http { include mime.types; server_names_hash_bucket_size 512 ; default_type application/octet-stream; sendfile on; keepalive_timeout 65 ; server_tokens off; client_max_body_size 8m ; # 设置客户端最大的请求主体大小为8M include vhosts/*.conf; } |
13.FastCGI 相关参数调优
当 LNMP 组合工做时,首先是用户经过浏览器输入域名请求 Nginx Web 服务,若是请求的是静态资源,则由 Nginx 解析返回给用户;若是是动态请求(如 PHP),那么 Nginx 就会把它经过 FastCGI 接口发送给 PHP 引擎服务(即 php-fpm)进行解析,若是这个动态请求要读取数据库数据,那么 PHP 就会继续向后请求 MySQL 数据库,以读取须要的数据,并最终经过 Nginx 服务把获取的数据返回给用户,这就是 LNMP 环境的基本请求流程。
FastCGI 介绍:CGI 通用网关接口,是 HTTP 服务器与其余机器上的程序服务通讯交流的一种工具,CGI 接口的性能较差,每次 HTTP 服务器遇到动态程序时都须要从新启动解析器来执行解析,以后结果才会被返回 HTTP 服务器,所以就有了 FastCGI ,FastCGI 是一个在 HTTP 服务器和动态脚本语言间通讯的接口,主要是把动态语言和 HTTP 服务器分离开来,使得 HTTP 服务器专注地处理静态请求,提升总体性能,在 Linux 下,FastCGI 接口即为 socket ,这个 socket 能够是文件 socket 也能够是 IP socket
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
[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 ; fastcgi_connect_timeout 240 ; # Nginx 服务器和后端FastCGI服务器链接的超时时间 fastcgi_send_timeout 240 ; # Nginx 容许FastCGI服务器返回数据的超时时间,即在规定时间内后端服务器必须传完全部的数据,不然Nginx将断开这个链接 fastcgi_read_timeout 240 ; # Nginx 从FastCGI服务器读取响应信息的超时时间,表示链接创建成功后,Nginx等待后端服务器的响应时间 fastcgi_buffer_size 64k ; # Nginx FastCGI 的缓冲区大小,用来读取从FastCGI服务器端收到的第一部分响应信息的缓冲区大小 fastcgi_buffers 4 64k ; # 设定用来读取从FastCGI服务器端收到的响应信息的缓冲区大小和缓冲区数量 fastcgi_busy_buffers_size 128k ; # 用于设置系统很忙时可使用的 proxy_buffers 大小 fastcgi_temp_file_write_size 128k ; # FastCGI 临时文件的大小 # fastcti_temp_path /data/ngx_fcgi_tmp; # FastCGI 临时文件的存放路径 fastcgi_cache_path /data/ngx_fcgi_cache levels=2:2 keys_zone =ngx_fcgi_cache:512m inactive =1d max_size =40g; # 缓存目录 server { listen 80 ; server_name www.abc.com; location / { root html/www; index index.html index.htm; } location ~ .*\.(php|php5)?$ { root html/www; fastcgi_pass 127.0 . 0.1 : 9000 ; fastcgi_index index.php; include fastcgi.conf; fastcgi_cache ngx_fcgi_cache; # 缓存FastCGI生成的内容,好比PHP生成的动态内容 fastcgi_cache_valid 200 302 1h ; # 指定http状态码的缓存时间,这里表示将200和302缓存1小时 fastcgi_cache_valid 301 1d ; # 指定http状态码的缓存时间,这里表示将301缓存1天 fastcgi_cache_valid any 1m ; # 指定http状态码的缓存时间,这里表示将其余状态码缓存1分钟 fastcgi_cache_min_uses 1 ; # 设置请求几回以后响应被缓存,1表示一次即被缓存 fastcgi_cache_use_stale error timeout invalid_header http_500; # 定义在哪些状况下使用过时缓存 fastcgi_cache_key http://$host$request_uri; # 定义 fastcgi_cache 的 key } } } |
|
[root@localhost ~] # pkill php-fpm [root@localhost ~] # /usr/local/php/sbin/php-fpm
|
14.配置 Nginx gzip 压缩
Nginx gzip 压缩模块提供了压缩文件内容的功能,用户请求的内容在发送到客户端以前,Nginx 服务器会根据一些具体的策略实施压缩,以节约网站出口带宽,同时加快数据传输效率,来提高用户访问体验,须要压缩的对象有 html 、js 、css 、xml 、shtml ,图片和视频尽可能不要压缩,由于这些文件大多都是已经压缩过的,若是再压缩可能反而变大,另外,压缩的对象必须大于 1KB,因为压缩算法的特殊缘由,极小的文件压缩后可能反而变大
|
[root@localhost ~] # cat /usr/local/nginx/conf/nginx.conf ...... http { gzip on; # 开启压缩功能 gzip_min_length 1k ; # 容许压缩的对象的最小字节 gzip_buffers 4 32k ; # 压缩缓冲区大小,表示申请4个单位为32k的内存做为压缩结果的缓存 gzip_http_version 1.1 ; # 压缩版本,用于设置识别HTTP协议版本 gzip_comp_level 9 ; # 压缩级别,1级压缩比最小但处理速度最快,9级压缩比最高但处理速度最慢 gzip_types text/css text/xml application/javascript; # 容许压缩的媒体类型 gzip_vary on; # 该选项可让前端的缓存服务器缓存通过gzip压缩的页面,例如用代理服务器缓存通过Nginx压缩的数据 } |
检查压缩:能够用 Google 浏览器按 F12 查看,也能够在 Google 浏览器安装 yslow 插件(yslow.org)
15.配置 Nginx expires 缓存
(1) Nginx expires 的功能就是为用户访问的网站内容设定一个过时时间,当用户第一次访问这些内容时,会把这些内容存储在用户浏览器本地,这样用户第二次及之后继续访问该网站时,浏览器会检查加载已经缓存在用户浏览器本地的内容,就不会去服务器下载了,直到缓存的内容过时或被清除为止
(2) 不但愿被缓存的内容:广告图片、网站流量统计工具、更新很频繁的文件
(3) 缓存日期参考:51CTO 缓存 1 周,新浪缓存 15 天,京东缓存 25 年,淘宝缓存 10 年
|
server { listen 80 ; server_name www.abc.com abc.com; root html/www; location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ # 缓存的对象 { expires 3650d ; # 缓存的时间,3650天,即10年 } } |
使用 Google 浏览器安装 yslow 插件来查看:
16.优化 Nginx access 日志
1. 配置日志切割
|
[root@localhost ~] # vim /usr/local/nginx/conf/cut_nginx_log.sh #!/bin/bash savepath_log ='/usr/local/clogs' nglogs ='/usr/local/nginx/logs' mkdir -p $savepath_log/$(date +%Y)/$(date +%m) mv $nglogs /access.log $savepath_log/$(date +%Y)/$(date +%m)/access.$(date +%Y%m%d).log mv $nglogs /error.log $savepath_log/$(date +%Y)/$(date +%m)/error.$(date +%Y%m%d).log kill -USR1 `cat /usr/local/nginx/logs/nginx.pid` |
|
[root@localhost ~] # crontab -e # 天天凌晨0点执行脚本 0 0 * * * /bin/sh /usr/local/nginx/conf/cut_nginx_log.sh > /dev/null 2 >& 1 |
2. 不记录不须要的访问日志
|
location ~ . *\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$ { access_log off; } |
3. 设置访问日志的权限
|
chown -R root.root /usr/local/nginx/logs chmod -R 700 /usr/local/nginx/logs |
17.优化 Nginx 站点目录
1. 禁止解析指定目录下的指定程序
|
location ~ ^ /data/.*\.(php|php5|sh|pl|py)$ { # 根据实际来禁止哪些目录下的程序,且该配置必须写在 Nginx 解析 PHP 的配置前面 deny all ; } |
2. 禁止访问指定目录
|
location ~ ^ /data/.*\.(php|php5|sh|pl|py)$ { # 根据实际来禁止哪些目录下的程序,且该配置必须写在 Nginx 解析 PHP 的配置前面 deny all ; } |
3. 限制哪些 IP 不能访问网站
|
location ~ ^ /wordpress { # 相对目录,表示只容许 192.168.1.1 访问网站根目录下的 wordpress 目录 allow 192.168 . 1.1 /24; deny all ; } |
18.配置 Nginx 防盗链
什么是防盗链:简单地说,就是某些不法网站未经许可,经过在其自身网站程序里非法调用其余网站的资源,而后在本身的网站上显示这些调用的资源,使得被盗链的那一端消耗带宽资源 (1) 根据 HTTP referer 实现防盗链:referer 是 HTTP的一个首部字段,用于指明用户请求的 URL 是从哪一个页面经过连接跳转过来的
(2) 根据 cookie 实现防盗链:cookie 是服务器贴在客户端身上的 "标签" ,服务器用它来识别客户端
根据 referer 配置防盗链:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# 第一种,匹配后缀 location ~ . *\.(gif|jpg|jpeg|png|bm|swf|flv|rar|zip|gz|bz2)$ { # 指定须要使用防盗链的媒体资源 access_log off; # 不记录防盗链的日志 expires 15d; # 设置缓存时间 valid_referers none blocked *.test.com *.abc.com; # 表示这些地址能够访问上面的媒体资源 if ($invalid_referer) { # 若是地址不如上面指定的地址就返回403 return 403 } } # 第二种,绑定目录 location /images { root /web/www/img; vaild_referers nono blocked *.spdir.com *.spdir.top; if ($invalid_referer) { return 403 ; } } |
19.配置 Nginx 错误页面优雅显示
|
[root@localhost ~] # cat /usr/local/nginx/conf/nginx.conf ...... http { location / { root html/www; index index.html index.htm; error_page 400 401 402 403 404 405 408 410 412 413 414 415 500 501 502 503 506 = http: //www.xxxx.com/error.html; # 将这些状态码的页面连接到 http://www.xxxx.com/error.html ,也能够单独指定某个状态码的页面,如 error_page 404 /404.html } } |
20.优化 Nginx 文件权限
为了保证网站不受木马入侵,全部文件的用户和组都应该为 root ,全部目录的权限是 755 ,全部文件的权限是 644
|
[root@localhost ~] # chown -R root.root /usr/local/nginx/.... # 根据实际来调整 [root@localhost ~] # chmod 755 /usr/local/nginx/.... [root@localhost ~] # chmod 644 /usr/local/nginx/.... |
21.Nginx 防爬虫优化
咱们能够根据客户端的 user-agents 首部字段来阻止指定的爬虫爬取咱们的网站\
|
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 ; } |
22.控制 Nginx 并发链接数
1. 限制单个 IP 的并发链接数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@localhost ~] # cat /usr/local/nginx/conf/nginx.conf .... http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65 ; limit_conn_zone $binary_remote_addr zone=addr:10m; # 用于设置共享内存区域,addr 是共享内存区域的名称,10m 表示共享内存区域的大小 server { listen 80 ; server_name www.abc.com; location / { root html/www; index index.html index.htm; limit_conn addr 1 ; # 限制单个IP的并发链接数为1 } } } |
2. 限制虚拟主机总链接数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
.... http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65 ; limit_conn_zone $server_name zone=perserver:10m; server { listen 80 ; server_name www.abc.com; location / { root html/www; index index.html index.htm; limit_conn perserver 2 ; # 设置虚拟主机链接数为2 } } } |
23. 集群代理优化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
upstream bbs_com_pool{ # 定义服务器池 ip_hash; # 会话保持(当服务器集群中没有会话池时,且代理的是动态数据就必须写ip_hash,反之什么也不用写) #fair # 智能分配(第三方,须要下载upstream_fair模块)根据后端服务器的响应时间来调度 #url_hash # 更具URL的结果来分配请求(每一个url定向到同一个服务器,提升后端缓存服务器的效率,自己不支持,须要安装nginx_hash) # 当算法为ip_hash不能有 weight backup server 192.168 . 10.1 : 80 ; # 默认weight为1 server 192.168 . 10.3 : 80 weight =5; #weight 表示权重 server 192.168 . 10.4 : 80 down; #down: 不参与本次轮询 server 192.168 . 10.5 : 80 down backup; #backup: 当任何一台主机出现故障,将进行切换替换 server 192.168 . 10.6 : 80 max_fails =3 fail_timeout =20s; #max_fails 最大失败请求次数(默认为1),fail_timeout失败超时时间 server 192.168 . 10.7 : 8080 ; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
stream{ upstream cluster { # hash $remote_addr consistent; // 保持 session 不变,四层开启 server 192.168 . 1.2 : 80 max_fails =3 fail_timeout =30s; server 192.168 . 1.3 : 80 max_fails =3 fail_timeout =30s; } server { listen 80 ; proxy_pass cluster; proxy_connect_timeout 1s ; proxy_timeout 3s ; } location { proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header; 当发生其中任何一种错误,将转交给下一个服务器 proxy_redirect off; proxy_set_header Host &$host; # 设置后端服务器的真实地址 proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded_For &proxy_add_x_forwarded_for; client_body_buffer_size 128k ; # 缓冲区大小,本地保存大小 proxy_connect_timeout 90 ; # 发起握手等待的响应时间 proxy_read_timeout 90 ; # 创建链接后等待后端服务器响应时间(实际上是后端等候处理的时间) proxy_send_timeout 90 ; # 给定时间内后端服务器必须响应,不然断开 proxy_buffer_size 4k ; #proxy 缓冲区大小 proxy_buffers 4 32k ; # 缓冲区个数和大小 proxy_busy_buffers_size 64k ; # 系统繁忙时buffer的临时大小,官方要求proxy_buffer_size*2 proxy_temp_file_write_size 64k ; #proxy 临时文件的大小 } } |
系统内核参数优化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
vim /etc/sysctl.conf net.ipv4.tcp_syncookies = 1 fs. file -max = 999999 net.ipv4.tcp_max_tw_buckets = 6000 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 1 net.core.somaxconn =262114 net.core.netdev_max_backlog =262114 net.ipv4.tcp_max_syn_backlog = 262114 net.ipv4.tcp_max_orphans =262114 net.ipv4.tcp_synack_retries =1 net.ipv4.tcp_syn_retries =1 net.ipv4.tcp_keepalive_time = 600 net.ipv4.tcp_fin_timeout = 30 net.ipv4.ip_local_port_range = 1024 65000 net.ipv4.tcp_rmem = 10240 87380 12582912 net.ipv4.tcp_wmem = 10240 87380 12582912 net.core.netdev_max_backlog = 8096 net.core.rmem_default = 6291456 net.core.wmem_default = 6291456 net.core.rmem_max = 12582912 net.core.wmem_max = 12582912 |
参数详解
注意:滑动窗口的大小与套接字缓存区会在必定程度上影响并发链接的数目。每一个TCP链接都会为维护TCP滑动窗口而消耗内存,这个窗口会根据服务器的处理速度收缩或扩张。 参数net.core.wmem_max = 12582912的设置,须要平衡物理内存的总大小、Nginx并发处理的最大链接数量而肯定。固然,若是仅仅为了提供并发量使服务器不出现Out Of Memory问题而去下降滑动窗口大小,那么并不合适,由于滑动窗太小会影响大数据量的传输速度。net.core.rmem_default = 629145六、net.core.wmem_default = 629145六、 net.core.rmem_max = 12582912和net.core.wmem_max = 12582912这4个参数的设置须要根据咱们的业务特性以及实际的硬件成原本综合考虑。 Nginx并发处理的最大链接量:由nginx.conf中的work_processes和work_connections参数决定。