一、Nginx 优化分类
安全优化(提升网站安全性配置)
性能优化(提升用户访问网站效率)
二、Nginx 安全优化
2.1 隐藏 nginx 版本信息优化
官方参数:
Syntax: server_tokens on | off | build | string;
Default: server_tokens on;
Context: http, server, location
配置举例:
[root@web01 ~]# cat /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile off;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$requ
est" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server {
listen 80;
server_name www.oldboyedu.com;
server_tokens off;
location / {
root html/www;
index index.html index.htm;
}
access_log logs/access_www.log main;
}
}
测试结果:
[root@web01 ~]# curl -I 10.0.0.8
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 01 Nov 2017 18:32:40 GMT
Content-Type: text/html
Content-Length: 10
Last-Modified: Wed, 25 Oct 2017 01:20:56 GMT
Connection: keep-alive
ETag: "59efe6f8-a"
Accept-Ranges: bytes
2.2 修改 nginx 版本信息
修改版本信息需要修改程序源文件信息
修改内核信息
[root@web01 nginx-1.10.2]# vim src/core/nginx.h
···
13 #define NGINX_VERSION "1.0"
14 #define NGINX_VER "oldboy/" NGINX_VERSION
22 #define NGINX_VAR "oldboy"
···
修改头部信息
[root@web01 nginx-
1.10.2]# vim src/http/ngx_http_header_filter_module.c
···
49 static char ngx_http_server_string[] = "Server: oldboy" CRLF;
···
修改错误页显示
[root@web01 nginx-1.10.2]# vim src/http/ngx_http_special_response.c
···
此处可以不修改
21 static u_char ngx_http_error_full_tail[] =
22 "
23 "