1. 隐藏 nginx 版本信息优化 (安全优化)
官方参考链接: http://nginx.org/en/docs/http/ngx_http_core_module.html#server_tokens
- Syntax: server_tokens on | off | build | string;
- Default: server_tokens on;(默认显示 nginx 服务版本)
- Context: http, server, location
实践配置:
- server {
- listen 80;
- server_name www.etiantian.org;
- server_tokens off;
- root html/www;
- index index.html index.htm;
- }
- [root@web01 conf]# curl -I www.etiantian.org
- HTTP/1.1 200 OK
Server: nginx <-- 版本号信息已经隐藏
- Date: Thu, 08 Mar 2018 06:15:18 GMT
- Content-Type: text/html
- Content-Length: 19
- Last-Modified: Mon, 05 Feb 2018 00:58:31 GMT
- Connection: keep-alive
- ETag: "5a77ac37-13"
- Accept-Ranges: bytes
2 修改 nginx 服务名称信息优化 (安全优化)
可以通过修改程序源代码, 实现修改 nginx 服务名称
第一个文件: /server/tools/nginx-1.12.2/src/core/nginx.h
13 行 #define NGINX_VERSION "6.6.6" #<== 已修改为想要显示的版本号
14 行 #define NGINX_VER oldboy/" NGINX_VERSION #<== 已修改为想要显示的名字
22 行 #define NGINX_VAR oldboy" #<== 已修改为想要显示的名字
将以上源码文件中三行内容进行修改
第二个文件:/server/tools/nginx-1.12.2/src/http/ngx_http_header_filter_module.c
49 行 static u_char ngx_http_server_string[] = "Server: oldboy" CRLF; #<=== 将 nginx 名称改为想要显示的名字
第三个文件: nginx1.xxx/src/http/ngx_http_special_response.c
改动之前配置信息
- static u_char ngx_http_error_full_tail[] =
- "<hr><center>" NGINX_VER "</center>" CRLF
- "</body>" CRLF
- "</html>" CRLF
- ;
改动之后配置信息
- static u_char ngx_http_error_full_tail[] =
- "<hr><center>" NGINX_VER "(http://oldboy.blog.51cto.com)</center>" CRLF
- "</body>" CRLF
- "</html>" CRLF
- ;
改动之前配置信息:
- static u_char ngx_http_error_tail[] =
- "<hr><center>nginx</center>" CRLF
- "</body>" CRLF
- "</html>" CRLF
- ;
改动之后配置信息:
- static u_char ngx_http_error_tail[] =
- "<hr><center>oldboy</center>" CRLF
- "</body>" CRLF
- "</html>" CRLF
3. 修改 nginx 软件 worker_processes 进程用户信息 (安全优化)
第一种方法:
编译安装软件时, 指定配置参数
--user=www --group=www
第二种方法:
修改服务配置文件, 实现修改 worker 进程用户
官方链接说明: http://nginx.org/en/docs/ngx_core_module.html#user
- Syntax:user user [group];
- Default:user nobody nobody;
- Context:main
实践配置:
- user oldboy oldboy;
- worker_processes 1;
- error_log /tmp/error.log error;
- [root@web01 nginx-1.12.2]# ps -ef|grep nginx
- root 47630 1 0 14:48 ? 00:00:00 nginx: master process nginx
- oldboy 47713 47630 0 15:01 ? 00:00:00 nginx: worker process
- 4
修改 nginx 软件 worker_processes 进程数量 (性能优化)
官方参考链接: http://nginx.org/en/docs/ngx_core_module.html#worker_processes
- Syntax: worker_processes number | auto;
- Default: worker_processes 1;
- Context: main
worker_processes 进程数据建议:(
一般和 CPU 的核数设置一致; 高并发可以和 CPU 核数 2 倍
)
1) 建议数量和你的服务器 CPU 核数一致
a. grep processor /proc/cpuinfo|wc -l
b. 如何通过 top 命令获取 cpu 核数: 按键盘数字 1 获取到
2) 建议数量和你的服务器 cpu 核数两倍一致
5 优化 nginx 服务进程均匀分配到不同 CPU 进行处理 (性能优化)
官方参考链接: http://nginx.org/en/docs/ngx_core_module.html#worker_cpu_affinity
- Syntax: worker_cpu_affinity cpumask ...;
- worker_cpu_affinity auto [cpumask];
- Default:
- Context: main
4 核 4 路 CPU 配置方法
- worker_processes 4;
- worker_cpu_affinity 0001 0010 0100 1000;
8 核 4 路 CPU 配置方法:
- worker_processes 8;
- worker_cpu_affinity 0001 0010 0100 1000 0001 0010 0100 1000;
- worker_processes 8;
- worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
4 核心 2 路 CPU 配置方法:
- worker_processes 4;
- worker_cpu_affinity 0001 0010 0100 1000;
- worker_processes 4;
- worker_cpu_affinity 0101 1010;
6 优化 nginx 事件处理模型 (性能优化)
官方参考链接: http://nginx.org/en/docs/ngx_core_module.html#use
- Syntax: use method;
- Default:
- Context: events
实践优化配置:
- events {
- worker_connections 1024;
- use epoll;
- }
7 调整 Nginx 单个进程允许的客户端最大连接数 (性能优化)
官方参考链接: http://nginx.org/en/docs/ngx_core_module.html#worker_connections
- Syntax:worker_connections number;
- Default:worker_connections 512;
- Context:events
注意事项:
worker_connections*worker_processes <= 系统的最大打开文件数
- # 加大文件描述
- echo '* - nofile 65535'>>/etc/security/limits.conf
8 配置 Nginx worker 进程最大打开文件数 (性能优化)
官方参考链接: http://nginx.org/en/docs/ngx_core_module.html#worker_rlimit_nofile
- Syntax:worker_rlimit_core size;
- Default:
- Context:main
实践配置:
- worker_rlimit_nofile 65535;
- #<== 最大打开文件数, 可设置为系统优化后的 ulimit -HSn 的结果, 在第 3 章中, 调整系统文件描述符和这个问题有相同之处
9 优化 nginx 服务数据高效传输模式 (性能优化)
官方链接参考: http://nginx.org/en/docs/http/ngx_http_core_module.html#sendfile
- Syntax:sendfile on | off;
- Default:sendfile off;
- Context:http, server, location, if in location
sendfile on 开启底层高效传输数据模式
官方链接参考: http://nginx.org/en/docs/http/ngx_http_core_module.html#tcp_nopush
- Syntax:tcp_nopush on | off;
- Default:tcp_nopush off;
- Context:http, server, location
tcp_nopush on 对快递员有利 (自身服务器有利)
让数据不着急传输
网络中传输数据的车 == 数据包 1500 3100k 1500 1500 100 1400
官方链接参考: http://nginx.org/en/docs/http/ngx_http_core_module.html#tcp_nodelay
- Syntax:tcp_nodelay on | off;
- Default:tcp_nodelay on;
- Context:http, server, location
tcp_nodelay on 对用户感知更好
尽快将数据传输出去
实践配置:
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- tcp_nopush on;
- tcp_nodelay off;
- 10
优化 Nginx 连接参数, 调整连接超时时间 (安全优化)
keepalive_timeout <-- 表示传输双方在指定时间内, 没有数据传输就超时断开连接
官方链接参考: https://nginx.org/en/docs/http/ngx_http_core_module.html#keepalive_timeout
- Syntax: keepalive_timeout timeout [header_timeout];
- Default: keepalive_timeout 75s;
- Context: http, server, location
client_header_timeout <-- 表示客户端发送请求报文的请求头部信息间隔超时时间
官方链接参考: https://nginx.org/en/docs/http/ngx_http_core_module.html#client_header_timeout
- Syntax:client_header_timeout time;
- Default:
- client_header_timeout 60s;
- Context:http, server
client_body_timeout <-- 表示客户端发送请求报文的请求主体信息间隔超时时间
官方链接参考: https://nginx.org/en/docs/http/ngx_http_core_module.html#client_body_timeout
- Syntax:client_body_timeout time;
- Default:
- client_body_timeout 60s;
- Context:http, server, location
send_timeout <-- 表示服务端发送响应报文的间隔超时时间
官方链接参考: https://nginx.org/en/docs/http/ngx_http_core_module.html#send_timeout
- Syntax:send_timeout time;
- Default:
- send_timeout 60s;
- Context:http, server, location
11 优化 nginx 服务上传文件限制 (安全优化)
client_max_body_size 控制上传数据大小限制参数
官方链接参考: https://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size
- Syntax:client_max_body_size size;
- Default:client_max_body_size 1m;
- Context:http, server, location
来源: http://www.bubuko.com/infodetail-2528420.html