nginx调优
nginx调优:
1隐藏版本号和服务器类型
1.1配置
[[email protected] ~]# ls /root/nginx/ #准备nginx软件
nginx-1.12.2.tar.gz
[[email protected] ~]# tar -xf /root/nginx/nginx-1.12.2.tar.gz -C /usr/local/src/ #解压
[[email protected] ~]# cd !$
[[email protected] src]# vim nginx-1.12.2/src/core/nginx.h #修改这个文件
将
12 #define nginx_version 1012002 #这里可以修改小写的nginx版本
13 #define NGINX_VERSION "1.12.2" #这里可以修改大写的NGINX版本
14 #define NGINX_VER "nginx/" NGINX_VERSION #这里可以修改软件名称
改为
12 #define nginx_version 1234
13 #define NGINX_VERSION "5678"
14 #define NGINX_VER "Bob" NGINX_VERSION
[[email protected] src]# vim nginx-1.12.2/src/http/ngx_http_header_filter_module.c #修改请求头
将
49 static u_char ngx_http_server_string[] = "Server: nginx" CRLF; #这里需要改请求头
50 static u_char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
51 static u_char ngx_http_server_build_string[] = "Server: " NGINX_VER_BUILD CRLF;
改为
49 static u_char ngx_http_server_string[] = "Server: Bob" CRLF; #请求头改掉,达到隐藏服务器的目的
50 static u_char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
51 static u_char ngx_http_server_build_string[] = "Server: " NGINX_VER_BUILD CRLF;
[[email protected] src]# vim nginx-1.12.2/src/http/ngx_http_special_response.c #修改响应头
将
21 static u_char ngx_http_error_full_tail[] =
22 "<hr><center>" NGINX_VER "</center>" CRLF #这里需要修改响应头(这里的NGINX_VER是变量,所以有双引号)
23 "</body>" CRLF
24 "</html>" CRLF
25 ;
改为
21 static u_char ngx_http_error_full_tail[] =
22 "<hr><center>Bob</center>" CRLF #修改响应头(修改之后不能带引号)
23 "</body>" CRLF
24 "</html>" CRLF
25 ;
1.2源码安装nginx
[[email protected] src]# yum install -y gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre-devel
[[email protected] ~]# cd /usr/local/src/nginx-1.12.2/
[[email protected] nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre
[[email protected] nginx-1.12.2]# make && make install
[[email protected] nginx-1.12.2]# /usr/local/nginx/sbin/nginx #启动
[[email protected] nginx-1.12.2]# netstat -anptu | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 30592/nginx
1.3访问测试
1.3.1访问正确测试
[[email protected] ~]# curl -I 47.100.15.98 #测试访问
HTTP/1.1 200 OK
Server: Bob5678 #这里我们看到服务器类型被隐藏了
Date: Mon, 13 Nov 2017 17:12:57 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 13 Nov 2017 17:03:12 GMT
Connection: keep-alive
ETag: "5a09d050-264"
Accept-Ranges: bytes
1.3.2访问错误测试
我们用浏览器访问服务器中不存在的页面使其报错,我们可以看到报错中也没有显示相关信息,说明我们隐藏成功!
2设置nginx运行用户
[[email protected] ~]# ps -aux | grep nginx #查看nginx运行的用户
root 30592 0.0 0.0 20044 656 ? Ss 01:06 0:00 nginx: master process /usr/local/nginx/sbin/nginx #nginx的管理进程,负责产生及销毁主进程并负责产生日志
nobody 30593 0.0 0.1 20488 1584 ? S 01:06 0:00 nginx: worker process #nginx的运行进程,默认是nobody用户运行
[[email protected] ~]# useradd -M -s /sbin/nologin nginx #创建nginx用户
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
将
2 #user nobody; #其实使用nobody用户也是很安全的,这里可改可不改
改为:
2 user nginx; #修改运行用户
[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload
[[email protected] ~]# ps -aux | grep nginx
root 30592 0.0 0.1 20180 1368 ? Ss 01:06 0:00 nginx: master process /usr/local/nginx/sbin/nginx #这里看到管理用户没有变化
nginx 30988 0.0 0.1 20608 1404 ? S 01:53 0:00 nginx: worker process #这里看到运行用户变为nginx了
3修改运行进程个数
worker_processes 定义了nginx对外提供web服务时的worker进程数。最优值取决于许多因素,包括(但不限于)CPU核的数量、存储数据的硬盘数量及负载模式。不能确定的时候,将其设置为可用的CPU内核数将是一个好的开始(设置为“auto”将尝试自动检测它)。
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf #修改配置文件
3 worker_processes 1; #在这里设置运行进程数
4设置cpu运行亲和力
nginx默认是没有开启利用多核cpu的配置的。需要通过增加worker_cpu_affinity配置参数来充分利用多核cpu,cpu是任务处理,当计算最费时的资源的时候,cpu核使用上的越多,性能就越好。
4.1例1
2核cpu,开启2个进程:
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 2;
worker_cpu_affinity 01 10;
解释:01表示启用第一个CPU内核,10表示启用第二个CPU内核。worker_cpu_affinity 01 10;表示开启两个进程,第一个进程对应着第一个CPU内核,第二个进程对应着第二个CPU内核。
4.2例2
2核cpu,开启4个进程:
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 4;
worker_cpu_affinity 01 10 01 10;
解释:开启了四个进程,它们分别对应着开启2个CPU内核。
4.3例3
4个cpu,开启4个进程:
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
解释:0001表示启用第一个CPU内核,0010表示启用第二个CPU内核,依此类推。
4.4例4
4核cpu,开启2个进程:
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 2;
worker_cpu_affinity 0101 1010;
解释:0101表示开启第一个和第三个内核,1010表示开启第二个和第四个内核;2个进程对应着四个内核;worker_cpu_affinity配置是写在/etc/nginx/nginx.conf里面的;2核是 01,四核是0001,8核是00000001,有多少个核,就有几位数,1表示该内核开启,0表示该内核关闭。
4.5例5
8核cpu,开启8个进程:
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
解释:0001表示启用第一个CPU内核,0010表示启用第二个CPU内核,依此类推;worker_processes最多开启8个,8个以上性能提升不会再提升了,而且稳定性变得更低,所以8个进程够用了。
配置完之后可以重启nginx,用ab工具或者wrk工具,可以进行性能测试,在服务器上执行top,然后按1,就可以看到cpu工作情况,如果多个cpu内核的利用率差不多,就证明nginx已经成功利用了多核cpu,测试结束后,cpu内核的负载都同时降低。
5限制最大打开文件数
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
12 events {
13 worker_connections 1024; #此处设置
14 }
这个指令是指当一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。
6事件处理模型
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
12 events {
13 worker_connections 1024;
14 use epoll; #此处设置
15 }
use设置用于复用客户端线程的轮询方法。如果你使用Linux 2.6+,你应该使用epoll。如果你使用*BSD,你应该使用kqueue。
7开启高效传输
sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
17 http {
18 include mime.types;
19 default_type application/octet-stream;
20
21 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
22 # '$status $body_bytes_sent "$http_referer" '
23 # '"$http_user_agent" "$http_x_forwarded_for"';
24
25 #access_log logs/access.log main;
26
27 sendfile on; #开启高效传输模式
28 tcp_nopush on; #要配合这个一起使用
8设置链接超时时间
主要目的是保护服务器资源,CPU,内存,控制连接数,因为建立连接也是需要消耗资源的,TCP的三次握手四次挥手等,我们一般断掉的是那些建立连接但是不做事儿,也就是我建立了链接开始,但是后续的握手过程没有进行,那么我们的链接处于等待状态的,全部断掉!同时我们也希望php建议短链接,消耗资源少。
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
17 http {
18 include mime.types;
19 default_type application/octet-stream;
20
21 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
22 # '$status $body_bytes_sent "$http_referer" '
23 # '"$http_user_agent" "$http_x_forwarded_for"';
24
25 #access_log logs/access.log main;
26
27 sendfile on;
28 tcp_nopush on;
29
30 #keepalive_timeout 0;
31 keepalive_timeout 65; #客户端连接保持会话超时时间,超过这个时间,服务器断开这个链接。
32 tcp_nodelay on; #也是防止网络阻塞,不过要包涵在keepalived参数才有效。
33 client_header_timeout 15; #客户端请求头读取超时时间,如果超过这个时间没有发送任何数据,nginx将返回request time out的错误。
34 client_body_timeout 15; #客户端求主体超时时间,超过这个时间没有发送任何数据,和上面一样的错误提示。
35 send_timeout 15; #响应客户端超时时间,这个超时时间仅限于两个活动之间的时间,如果超过这个时间,客户端没有任何活动,nginx关闭连接。
9限制上传文件大小
9.1介绍
PHP可以修改上传文件大小限制,nginx也可以修改。
9.2示例
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
1
2 user nginx;
3 worker_processes 1;
4
5 #error_log logs/error.log;
6 #error_log logs/error.log notice;
7 #error_log logs/error.log info;
8
9 #pid logs/nginx.pid;
10
11
12 events {
13 worker_connections 1024;
14 }
15
16
17 http {
18 include mime.types;
19 default_type application/octet-stream;
20
21 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
22 # '$status $body_bytes_sent "$http_referer" '
23 # '"$http_user_agent" "$http_x_forwarded_for"';
24
25 #access_log logs/access.log main;
26
27 sendfile on;
28 tcp_nopush on;
29
30 #keepalive_timeout 0;
31 keepalive_timeout 65;
32 tcp_nodelay on;
33 client_header_timeout 15;
34 client_body_timeout 15;
35 send_timeout 15;
36 client_max_body_size 10m; #设置最大上传10兆的文件。
10fastcgi调优
11使用gzip压缩
11.1介绍
使用gzip压缩功能,可能为我们节约带宽,加快传输速度,有更好的体验,也为我们节约成本,所以说这是一个重点。Nginx启用压缩功能需要ngx_http_gzip_module模块,apache使用的是mod_deflate。一般我们需要压缩的内容有:文本,js,html,css,对于图片,视频,flash什么的不压缩,同时也要注意,我们使用gzip的功能是需要消耗CPU的。
11.2示例
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
17 http {
18 include mime.types;
19 default_type application/octet-stream;
20
21 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
22 # '$status $body_bytes_sent "$http_referer" '
23 # '"$http_user_agent" "$http_x_forwarded_for"';
24
25 #access_log logs/access.log main;
26
27 sendfile on;
28 tcp_nopush on;
29
30 #keepalive_timeout 0;
31 keepalive_timeout 65;
32 tcp_nodelay on;
33 client_header_timeout 15;
34 client_body_timeout 15;
35 send_timeout 15;
36
37 gzip on; #开启压缩功能
38 gzip_min_length 1k; #设置允许压缩的页面最小字节数,页面字节数从header头的Content-Length中获取,默认值是0,不管页面多大都进行压缩,建议设置成大于1K,如果小与1K可能会越压越大。
39 gzip_buffers 4 32k; #压缩缓冲区大小,表示申请4个单位为32K的内存作为压缩结果流缓存,默认值是申请与原始数据大小相同的内存空间来存储gzip压缩结果。
40 gzip_http_version 1.1; #压缩版本(默认1.1,前端为squid2.5时使用1.0)用于设置识别HTTP协议版本,默认是1.1,目前大部分浏览器已经支持GZIP解压,使用默认即可。
41 gzip_comp_level 9; #压缩比例,用来指定GZIP压缩比,1压缩比最小,处理速度最快,9压缩比最大,传输速度快,但是处理慢,也比较消耗CPU资源。
42 gzip_types text/css text/xml application/javascript; #用来指定压缩的类型,‘text/html’类型总是会被压缩。
43 gzip_vary on; #vary header支持,该选项可以让前端的缓存服务器缓存经过GZIP压缩的页面,例如用Squid缓存经过nginx压缩的数据。
12
12设置expires缓存
12.1介绍
缓存,主要针对于图片,css,js等元素更改机会比较少的情况下使用,特别是图片,占用带宽大,我们完全可以设置图片在浏览器本地缓存365d,css,js,html可以缓存个10来天,这样用户第一次打开加载慢一点,第二次就比较快!缓存的时候,我们需要将需要缓存的拓展名列出来。expires缓存配置在server字段里面。
12.2优点
1)expires可以降低网站购买的带宽,节约成本
2)提升用户访问体验
3)减轻服务的压力,节约服务器成本,甚至可以节约人力成本,是web服务非常重要的功能。
12.3缺点
被缓存的页面或数据更新了,用户看到的可能还是旧的内容,反而影响用户体验。
12.4示例
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
45 server {
46 listen 80;
47 server_name localhost;
48
49 #charset koi8-r;
50
51 #access_log logs/host.access.log main;
52
53 location / {
54 root html;
55 index index.html index.htm;
56 }
57 location ~ .*\.(gif|jpg|jpeg)$ { #设置图片缓存30天
58 expires 30d;
59 }
60 location ~ .*\.(css|js)$ { #设置css及js缓存10天
61 expires 10d;
62 }
63 location ~ ^/(image|javascript|)/ { #设置image及javascript目录缓存365天
64 expires 365s;
65 }
13设置日志格式
14目录文件访问控制
15来源访问控制
16IP和301优化
16.1普通跳转
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
45 server {
46 listen 80;
47 server_name localhost;
48 rewrite ^ http://www.baidu.com$request_uri?; #将请求的域名或地址替换为www.baidu.com,后面的uri不替换。
16.2错误(403)跳转
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
45 server {
46 listen 80;
47 server_name localhost;
48 return 403; #将请求转到403提示
访问测试:
16.3重定向(301)跳转
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
45 server {
46 listen 80;
47 server_name localhost;
48 if ($host = 'ziqidonglai.org') {
49 rewrite ^/(.*)$ (http:/www.ziqidonglai.org/$1 permanent; #将访问的ziqidonglai.org跳转到www.ziqidonglai.org
50 }
17防盗链
17.1介绍
防止别人直接从你网站引用图片等链接,消耗了你的资源和网络流量,那么我们的解决办法由几种:
1)水印,品牌宣传,你的带宽,服务器足够。
2)防火墙,直接控制,前提是你知道IP来源。
3)防盗链策略。
17.2示例
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
45 server {
46 listen 80;
47 server_name localhost;
48
49 #charset koi8-r;
50
51 #access_log logs/host.access.log main;
52
53 location / {
54 root html;
55 index index.html index.htm;
56 }
57 location ~ .*\.(gif|jpg|jpeg)$ {
58 expires 30d;
59 }
60 location ~ .*\.(css|js)$ {
61 expires 10d;
62 }
63 location ~ ^/(image|javascript|)/ {
64 expires 365s;
65 }
66 location ~ .*\.(gif|jpg|jpeg)$ {
67 valid_referers none blocked *.a.com a.com; #描述不是从a.com链接过来的
68 if ( $invalid_referer ) { #判断是否为真(if和后面括号以及变量等号这些元素都要有空格)
69 return 404; #返回404即防盗链
70 }
71 }
72 location ~ .*\.(css|js)$ {
73 valid_referers none blocked *.a.com a.com;
74 if ( $invalid_referer ) {
75 rewrite ^ /www.a.com/img/nolink.png; #将链接到本地的链接指向/www.a.com/img/nolink.png
76 }
77 }
18设置错误页面提示
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
45 server {
46 listen 80;
47 server_name localhost;
48
49 #charset koi8-r;
50
51 #access_log logs/host.access.log main;
52
53 location / {
54 root html;
55 index index.html index.htm;
56 }
57
58 #error_page 404 /404.html;
59
60 # redirect server error pages to the static page /50x.html
61 #
62 error_page 500 502 503 504 /50x.html; #设置错误提示的页面
63 location = /50x.html {
64 root html;
65 }
19内部身份验证
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
45 server {
80 location /pub { #定义需要验证的访问目录
81 auth_basic 'haha'; #验证名
82 auth_basic_user_file /usr/local/nginx/conf/passwd; #验证的用户名密码文件目录
83 }
[[email protected] ~]# yum install -y httpd-tools #要使用htpasswd命令需要安装该软件
[[email protected] ~]# htpasswd -cb /usr/local/nginx/conf/passwd admin 123456 #创建用户名密码文件
Adding password for user admin
[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload #重启
访问测试: