nginx优化项
- 隐藏nginx版本
- nginx 配置项优化
- 开启高效传输模式
- fastcgi 调优
- gzip 调优
- expires 缓存调优
- 内核参数优化
- 系统连接数的优化
准备nginx测试环境
-
nginx安装略
-
准备测试文件
vim /usr/local/nginx/conf/nginx.conf echo 'Hello world ' > /usr/local/nginx/html/index.html
-
启动nginx服务
systemctl start nginx [root@localhost ~]# curl -I http://172.16.46.114 HTTP/1.1 200 OK Server: nginx/1.12.2 #这里显示版本号 Date: Sat, 04 Jul 2020 05:20:43 GMT Content-Type: text/html Content-Length: 13 Last-Modified: Sat, 04 Jul 2020 03:57:39 GMT Connection: keep-alive ETag: "5efffe33-d" Accept-Ranges: bytes [root@localhost ~]# [root@localhost ~]# [root@localhost ~]# curl http://172.16.46.114 Hello world
隐藏nginx版本的2种方法
-
通过修改nginx配置文件的方法
在HTTP字段添加下
再次查看请求头
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# curl -I http://172.16.46.114
HTTP/1.1 200 OK
Server: nginx
Date: Sat, 04 Jul 2020 05:22:34 GMT
Content-Type: text/html
Content-Length: 13
Last-Modified: Sat, 04 Jul 2020 03:57:39 GMT
Connection: keep-alive
ETag: "5efffe33-d"
Accept-Ranges: bytes
-
重新编译法
修改源码包文件
src/core/nginx.h
[root@localhost src]# cd nginx-1.12.2/ [root@localhost nginx-1.12.2]# vim src/core/nginx.h [root@localhost nginx-1.12.2]# vi src/http/ngx_http_header_filter_module.c #彻底隐藏HTTP 头信息中的 connection 字段 [root@localhost nginx-1.12.2]# vi src/http/ngx_http_special_response.c #隐藏错误页面版本显示 [root@localhost nginx-1.12.2]# make && make install
src/core/nginx.h 修改如下:
[root@www nginx-1.12.2]# vi src/http/ngx_http_header_filter_module.c
修改前:
static char ngx_http_server_string[] = "Server: nginx" CRLF; //第 49 行
修改后:
static char ngx_http_server_string[] = "Server: IIS" CRLF;
[root@www nginx-1.12.2]# vi src/http/ngx_http_special_response.c
修改前
static u_char ngx_http_error_tail[] = //第 29 行
"<hr><center>nginx</center>" CRLF
"</body>" CRLF
"</html>" CRLF
;
修改后
static u_char ngx_http_error_tail[] =
"<hr><center>IIS</center>" CRLF
"</body>" CRLF
"</html>" CRLF
;
[root@localhost ~]# nginx -v
nginx version: IIS/8.8.8.8
nginx配置项优化
nginx运行CPU亲和力
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000
8核如下
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000
10000000;
2核如下:
worker_processes 2;
worker_cpu_affinity 0101 1010;
nginx最多可以打开的文件数
一个工作进程(worker process)建立一个连接后,进程将会打开一个文件副本,所以这个数(worker_connections)的大小还和操作系统设定的进程最大可打开的文件副本数有关。
worker_rlimit_nofile 65535;
#这个指令是指当一个 nginx 进程打开的最多文件描述符数目,理论值应该是最多打开文件数
(ulimit -n)与 nginx 进程数相除,但是 nginx 分配请求并不是那么均匀,所以最好与 ulimit -n
的值保持一致
#文件资源限制的配置可以在/etc/security/limits.conf 设置,针对 root/user 等各个用户或者*
代表所有用户来设置。
* soft nofile 65535
* hard nofile 65535
* soft noproc 65535
* hard noproc 65535
nginx事件处理模型
events {
use epoll;
worker_connections 65535;
multi_accept on;
}
#nginx 采用 epoll 事件模型,处理效率高
#work_connections 是单个 worker 进程允许客户端最大连接数,这个数值一般根据服务器性
能和内存来制定,实际最大值就是 `worker 进程数乘以 work_connections`
实际我们填入一个 65535,足够了,这些都算并发值,一个网站的并发达到这么大的数量,
也算一个大站了!
`multi_accept 告诉 nginx 收到一个新连接通知后接受尽可能多的连接`
开启高效传输模式
http {
include mime.types;
default_type application/octet-stream;
……
sendfile on;
tcp_nopush on;
……
注释:
# Include mime.types; //媒体类型, include 只是一个在当前文件中包含另一个文件内容的指令
# default_type application/octet-stream; //默认媒体类型足够
# sendfile on; //开启高效文件传输模式,
# tcp_nopush on; 必须在 sendfile 开启模式才有效,防止网路阻塞,积极的减少网络报文段的数量
连接超时时间
keepalive_timeout 60;
tcp_nodelay on;
client_header_buffer_size 4k;
open_file_cache max=102400 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
client_header_timeout 15;
client_body_timeout 15;
reset_timedout_connection on;
send_timeout 15;
server_tokens off;
client_max_body_size 10m;
# keepalived_timeout; 客户端连接保持会话超时时间,超过这个时间,服务器断开这个链接
# tcp_nodelay;也是防止网络阻塞,不过要包涵在 keepalived 参数才有效
# client_header_buffer_size 4k; 客户端请求头部的缓冲区大小
# open_file_cache max=102400 inactive=20s;这个将为打开文件指定缓存,默认是没有启用的
# open_file_cache_valid 30s;这个是指多长时间检查一次缓存的有效信息。
# open_file_cache_min_uses 1;
# open_file_cache 指令中的 inactive 参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的
# client_header_timeout 设置请求头的超时时间
# client_body_timeout 设置请求体的超时时间
# reset_timeout_connection 告诉 nginx 关闭不响应的客户端连接。
# send_timeout 响应客户端超时时间
# server_tokens 并不会让 nginx 执行的速度更快,但它可以关闭在错误页面中的 nginx 版本数字,这样对于安全性是有好处的。
# client_max_body_size; 上传文件大小限制
fastcgi与proxy 缓存调优
nginx 的 web 缓存功能的主要是由 proxy_cache、fastcgi_cache 指令集和关指令收集完成。
proxy_cache 指令负责反向代理缓存后端服务器的静态内容
fastcgi_cache 主要用来处理FastCGI 动态进程缓存
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 75;
proxy_send_timeout 75;
proxy_read_timeout 75;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_buffering on;
proxy_temp_path /usr/local/nginx/proxy_temp;
proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=my-cache:100m
max_size=1000m inactive=600m max_size=2g;
nginx做反向代理转发优化
代理参数
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
如果nginx后面接的是动态的网站,需要fastcgi接口转发请求到(PHP)
缓存调优如下:
fastcgi_connect_timeout 600;
fastcgi_send_timeout 600;
fastcgi_read_timeout 600;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_temp_path /usr/local/nginx/nginx_tmp;
fastcgi_intercept_errors on;
fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=cache_fastcgi:128m
inactive=1d max_size=10g;
转发调优如下:
location ~ \.php$ {
root /wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_cache cache_fastcgi;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;
fastcgi_cache_key http://$host$request_uri;
include fastcgi_params; #引用fastcgi_params目录
}
注释:
fastcgi_connect_timeout 600; #指定连接到后端 FastCGI 的超时时间。
fastcgi_send_timeout 600; #向 FastCGI 传送请求的超时时间。
fastcgi_read_timeout 600; #指定接收 FastCGI 应答的超时时间。
fastcgi_buffer_size 64k; #指定读取 FastCGI 应答第一部分需要用多大的缓冲区,默认的缓冲区大小为 fastcgi_buffers 指令中的每块大小,可以将这个值设置更小。
fastcgi_buffers 4 64k; #指定本地需要用多少和多大的缓冲区来缓冲 FastCGI 的应答请求
fastcgi_busy_buffers_size 128k; #建议设置为 fastcgi_buffers 的两倍,繁忙时候的 buffer
fastcgi_temp_file_write_size 128k; #在写入 fastcgi_temp_path 时将用多大的数据块,默认值是 fastcgi_buffers 的两倍
fastcgi_temp_path #缓存临时目录
fastcgi_intercept_errors on;# 这个指令指定是否传递 4xx 和 5xx 错误信息到客户端,或者允许nginx 使用 error_page 处理错误信息
fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=cache_fastcgi:128m
inactive=1d max_size=10g; # fastcgi_cache 缓存目录
fastcgi_cache cache_fastcgi; #表示开启 FastCGI 缓存并为其指定一个名称.inactive 表示默认
失效时间,如果缓存数据在失效时间内没有被访问,将被删除,max_size 表示最多用多少硬盘空间
fastcgi_pass #指定 FastCGI 服务器监听端口与地址
fastcgi_cache_valid 200 302 1h; #用来指定应答代码的缓存时间,实例中的值表示将 200 和302 应答缓存一小时,要和 fastcgi_cache 配合使用
fastcgi_cache_valid 301 1d; #将 301 应答缓存一天
fastcgi_cache_valid any 1m; #将其他应答缓存为 1 分钟
fastcgi_cache_min_uses 3; #该指令用于设置经过多少次请求的相同 URL 将被缓存
fastcgi_cache_key http://$host$request_uri; #该指令用来设置web缓存的Key值,nginx根据Key
值 md5 哈希存 储 .一 般根 据$host( 域名 )、$request_uri(请 求的路 径 ) 等变 量组 合成
proxy_cache_key
gzip 调优
使用 gzip 压缩功能,可能为我们节约带宽,加快传输速度,有更好的体验
同时也要注意,我们使用 gzip 的功能是需要消耗 CPU 的!
Nginx 启用压缩功能需要你来 ngx_http_gzip_module 模块,一般我们需要压缩的内容有:文本,js,html,css,对于图片,视频,flash 什么的不压缩;
gzip on;
gzip_min_length 2k;
gzip_buffers 4 32k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain text/css text/javascript application/json application/javascript
application/x-javascript application/xml;
gzip_vary on;
gzip_proxied any;
gzip on; #开启压缩功能
gzip_min_length 1k; #设置允许压缩的页面最小字节数
gzip_buffers 4 32k; #压缩缓冲区大小
gzip_http_version 1.1; #压缩版本
gzip_comp_level 6; #压缩比例,1 压缩比最小,处理速度最快,9 压缩比最大,传输速度快,但是处理慢,也比较消耗 CPU 资源;6比较中性
gzip_types text/css text/xml application/javascript; #用来指定压缩的类型
gzip_vary on; #vary header 支持
expires 缓存调优
缓存,主要针对于图片,css,js 等元素更改机会比较少的情况下使用,特别是图片,占用
带宽大,我们完全可以设置图片在浏览器本地缓存 30d,css,js,html 可以缓存个 10 来天,
这样用户第一次打开加载慢一点,第二次,就非常快了!缓存的时候,我们需要将需要缓存
的拓展名列出来, Expires 缓存配置在 server 字段里面
location ~* \.(ico|jpe?g|gif|png|bmp|swf|flv)$ {
expires 30d;
log_not_found off;
access_log off;
}
location ~* \.(js|css)$ {
expires 7d;
log_not_found off;
access_log off;
}
log_not_found off;是否在 error_log 中记录不存在的错误,默认是
nginx整体配置优化
关于系统连接数的优化
linux 默认值 open files 为 1024
修改open files 最大连接数
如下图:
编辑/etc/security/limits.conf
* soft nifile 65535
* hard nifile 65535
* soft noproc 65535
* hard noproc 65535
ulimit -n 65535 / reboot 保证本次会话生效
验证生效
[root@localhost ~]# ps -aux | grep nginx
root 1006 0.0 0.0 177032 1288 ? Ss 08:31 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 1009 0.3 1.5 206252 28804 ? S 08:31 0:00 nginx: worker process
nginx 1010 0.2 1.5 206252 28804 ? S 08:31 0:00 nginx: worker process
nginx 1011 0.0 0.0 179272 1776 ? S 08:31 0:00 nginx: cache manager process
root 3365 0.0 0.0 112656 960 pts/0 S+ 08:33 0:00 grep --color=auto nginx
[root@localhost ~]# cat /proc/1009/limits
Limit Soft Limit Hard Limit Units
Max cpu time unlimited unlimited seconds
Max file size unlimited unlimited bytes
Max data size unlimited unlimited bytes
Max stack size 8388608 unlimited bytes
Max core file size 0 unlimited bytes
Max resident set unlimited unlimited bytes
Max processes 7179 7179 processes
Max open files 65535 65535 files
Max locked memory 65536 65536 bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 7179 7179 signals
Max msgqueue size 819200 819200 bytes
Max nice priority 0 0
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us
可见,openfile已经更改