基于网上的技术文章思路,自己加以整理,有常用的和不常用的策略,供参考。
nginx的优化
1. gzip压缩优化 2. expires缓存有还 3. 网络IO事件模型优化 4. 隐藏软件名称和版本号 5. 防盗链优化 6. 禁止恶意域名解析 7. 禁止通过IP地址访问网站 8. HTTP请求方法优化 9. 防DOS攻击单IP并发连接的控制,与连接速率控制 10. 严格设置web站点目录的权限 11. 将nginx进程以及站点运行于监牢模式 12. 通过robot协议以及HTTP_USER_AGENT防爬虫优化 13. 配置错误页面根据错误码指定网页反馈给用户 14. nginx日志相关优化访问日志切割轮询,不记录指定元素日志、最小化日志目录权限 15. 限制上传到资源目录的程序被访问,防止木马入侵系统破坏文件 16. FastCGI参数buffer和cache配置文件的优化 17. php.ini和php-fpm.conf配置文件的优化 18. 有关web服务的Linux内核方面深度优化(网络连接、IO、内存等) 19. nginx加密传输优化(SSL) 20. web服务器磁盘挂载及网络文件系统的优化 21. 使用nginx cache
1、基本安全优化
1.1 隐藏版本信息
一般来说,软件的漏洞都和版本相关,所以我们要隐藏或消除web服务对访问用户显示的各种敏感信息。
1 [root@db01 rpm]# curl -I 10.0.0.8 2 HTTP/1.1 401 Unauthorized 3 Server: nginx #隐藏版本号 4 Date: Thu, 21 Jul 2016 03:23:38 GMT 5 Content-Type: text/html 6 Content-Length: 188 7 Connection: keep-alive 8 WWW-Authenticate: Basic realm="oldboy training" 9 过程: 10 vim /application/nginx/conf/nginx.conf 11 在http模块下加入: 12 server_tokens off; 13 /application/nginx/sbin/nginx -t 14 /application/nginx/sbin/nginx -s reload
1.2 隐藏nginx要修改源代码
要修改内容的路径:
第一路径:
1 /home/oldboy/tools/nginx-1.6.3/src/core/nginx.h 第14,16行 2 #define NGINX_VERSION "1.6.2" 修改为想要的版本号如2.4.3 3 #define NGINX_VER "nginx/" NGINX_VERSION 将nginx修改为想要修改的软件名称,如Apache。
第二路径
1 /home/oldboy/tools/nginx-1.6.3/src/http/ngx_http_header_filter_module.c第49行 2 grep 'Server:nginx' ngx_http_header_filter_module.cstatic 3 sed -i 's#Server:nginx#Server:Apache#g' ngx_http_header_filter_module.c
第三路径
/home/oldboy/tools/nginx-1.6.3/src/http/ngx_http_special_response.c第21,30行 "<hr><center>"NGINX_VER "(http://oldboy.blog.51cto.com)</center>" CRLF "<hr><center>OWS</center>" CRLF
然后重新编译
1.3 更改nginx服务的默认用户
第一种方法:
直接更改配置文件nginx.conf.default参数,将默认的#user nobody;改为user nginx.nginx;
第二种方法:
直接在编译nginx的时候指定用户和用户组命令如下:
./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
1.4 降权启动nginx
1 useradd inca 2 cd /home/inca/ 3 mkdir conf logs www 4 echo inca >www/index.html 5 chown -R inca.inca * 6 ln -s /application/nginx/conf/mime.types conf/mime.types #mime.types媒体类型文件
egrep -v "#|^$" /application/nginx/conf/nginx.conf.default >conf/nginx.conf
nginx.conf配置文件
worker_processes 1; error_log /home/inca/logs/error.log; pid /home/inca/logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 8080; server_name localhost; location / { root /home/inca/www;