20步打造最安全的Nginx Web服务器

原文链接

Nginx是一个轻量级的,高性能的Web服务器以及反向代理和邮箱(IMAP/POP3)代理服务器。它运行在UNIX,GNU /linux,BSD 各种版本,Mac OSX,Solaris和Windows。根据调查统计,6%的网站使用NginxWeb服务器。Nginx是少数能处理C10K问题的服务器之一。跟传统的服务器不同,Nginx不依赖线程来处理请求。相反,它使用了更多的可扩展的事件驱动(异步)架构。Nginx为一些高流量的网站提供动力,比如WordPress,人人网,腾讯,网易等。这篇文章主要是介绍如何提高运行在Linux或UNIX系统的Nginx Web服务器的安全性。 
 
 
 
默认配置文件和Nginx端口

  • /usr/local/nginx/conf/ – Nginx配置文件目录,/usr/local/nginx/conf/nginx.conf是主配置文件
  • /usr/local/nginx/html/ – 默认网站文件位置
  • /usr/local/nginx/logs/ – 默认日志文件位置
  • Nginx HTTP默认端口 : TCP 80
  • Nginx HTTPS默认端口: TCP 443
 
你可以使用以下命令来测试Nginx配置文件准确性。  
  1. /usr/local/nginx/sbin/nginx -t
 
将会输出。  
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok  
configuration file /usr/local/nginx/conf/nginx.conf test is successful  
执行以下命令来重新加载配置文件。  
  1. /usr/local/nginx/sbin/nginx -s reload
 
执行以下命令来停止服务器。  
  1. /usr/local/nginx/sbin/nginx -s stop
 
 
一、配置SELinux  
 
 
安全增强型Linux(SELinux)的是一个Linux内核的功能,它提供支持访问控制的安全政策保护机制。它可以大部分的攻击。下面我们来看如何启动基于centos/RHEL系统的SELinux。  
安装SELinux  
  1. rpm -qa | grep selinux
 
libselinux-1.23.10-2  
selinux-policy-targeted-1.23.16-6  
如果没有返回任何结果,代表没有安装 SELinux,如果返回了类似上面的结果,则说明系统安装了 SELinux。  
布什值锁定  
运行命令getsebool -a来锁定系统。  
  1. getsebool -a | less
  2. getsebool -a | grep off
  3. getsebool -a | grep o
 
 
二、通过分区挂载允许最少特权  
 
 
服务器上的网页/html/php文件单独分区。例如,新建一个分区/dev/sda5(第一逻辑分区),并且挂载在/nginx。确保/nginx是以noexec, nodev and nosetuid的权限挂载。以下是我的/etc/fstab的挂载/nginx的信息:  
LABEL=/nginx /nginx ext3 defaults,nosuid,noexec,nodev 1 2  
注意:你需要使用fdisk和mkfs.ext3命令创建一个新分区。  
 
三、配置/etc/sysctl.conf强化Linux安全  
 
 
你可以通过编辑/etc/sysctl.conf来控制和配置Linux内核、网络设置。  
  1. # Avoid a smurf attack
  2. net.ipv4.icmp_echo_ignore_broadcasts = 1
  3. # Turn on protection for bad icmp error messages
  4. net.ipv4.icmp_ignore_bogus_error_responses = 1
  5. # Turn on syncookies for SYN flood attack protection
  6. net.ipv4.tcp_syncookies = 1
  7. # Turn on and log spoofed, source routed, and redirect packets
  8. net.ipv4.conf.all.log_martians = 1
  9. net.ipv4.conf.default.log_martians = 1
  10. # No source routed packets here
  11. net.ipv4.conf.all.accept_source_route = 0
  12. net.ipv4.conf.default.accept_source_route = 0
  13. # Turn on reverse path filtering
  14. net.ipv4.conf.all.rp_filter = 1
  15. net.ipv4.conf.default.rp_filter = 1
  16. # Make sure no one can alter the routing tables
  17. net.ipv4.conf.all.accept_redirects = 0
  18. net.ipv4.conf.default.accept_redirects = 0
  19. net.ipv4.conf.all.secure_redirects = 0
  20. net.ipv4.conf.default.secure_redirects = 0
  21. # Don’t act as a router
  22. net.ipv4.ip_forward = 0
  23. net.ipv4.conf.all.send_redirects = 0
  24. net.ipv4.conf.default.send_redirects = 0
  25. # Turn on execshild
  26. kernel.exec-shield = 1
  27. kernel.randomize_va_space = 1
  28. # Tuen IPv6
  29. net.ipv6.conf.default.router_solicitations = 0
  30. net.ipv6.conf.default.accept_ra_rtr_pref = 0
  31. net.ipv6.conf.default.accept_ra_pinfo = 0
  32. net.ipv6.conf.default.accept_ra_defrtr = 0
  33. net.ipv6.conf.default.autoconf = 0
  34. net.ipv6.conf.default.dad_transmits = 0
  35. net.ipv6.conf.default.max_addresses = 1
  36. # Optimization for port usefor LBs
  37. # Increase system file descriptor limit
  38. fs.file-max = 65535
  39. # Allow for more PIDs (to reduce rollover problems); may break some programs 32768
  40. kernel.pid_max = 65536
  41. # Increase system IP port limits
  42. net.ipv4.ip_local_port_range = 2000 65000
  43. # Increase TCP max buffer size setable using setsockopt()
  44. net.ipv4.tcp_rmem = 4096 87380 8388608
  45. net.ipv4.tcp_wmem = 4096 87380 8388608
  46. # Increase Linux auto tuning TCP buffer limits
  47. # min, default, and max number of bytes to use
  48. # set max to at least 4MB, or higher if you use very high BDP paths
  49. # Tcp Windows etc
  50. net.core.rmem_max = 8388608
  51. net.core.wmem_max = 8388608
  52. net.core.netdev_max_backlog = 5000
  53. net.ipv4.tcp_window_scaling = 1
 
 
四、删除所有不需要的Nginx模块  
 
 
你需要直接通过编译Nginx源代码使模块数量最少化。通过限制只允许web服务器访问模块把风险降到最低。你可以只配置安装nginx你所需要的模块。例如,禁用SSL和autoindex模块你可以执行以下命令:  
  1. ./configure –without-http_autoindex_module –without-http_ssi_module
  2. make
  3. make install
 
通过以下命令来查看当编译nginx服务器时哪个模块能开户或关闭:  
  1. ./configure –help | less
 
禁用你用不到的nginx模块。  
(可选项)更改nginx版本名称。  
编辑文件/http/ngx_http_header_filter_module.c:  
  1. vi +48 src/http/ngx_http_header_filter_module.c
 
找到行:  
  1. static char ngx_http_server_string[] = “Server: nginx” CRLF;
  2. static char ngx_http_server_full_string[] = “Server: ” NGINX_VER CRLF;
 
按照以下行 修改  
  1. static char ngx_http_server_string[] = “Server: Ninja Web Server” CRLF;
  2. static char ngx_http_server_full_string[] = “Server: Ninja Web Server” CRLF;
 
保存并关闭文件。现在你可以编辑服务器了。增加以下代码到nginx.conf文件来关闭nginx版本号的显示。  
  1. server_tokens off
 
 
五、使用mod_security(只适合后端Apache服务器)  
 
 
mod_security为Apache提供一个应用程序级的防火墙。为后端Apache Web服务器安装mod_security,这会阻止很多注入式攻击。  
 
六、安装SELinux策略以强化Nginx Web服务器  
 
 
默认的SELinux不会保护Nginx Web服务器,但是你可以安装和编译保护软件。  
1、安装编译SELinux所需环境支持  
  1. yum -y install selinux-policy-targeted selinux-policy-devel
 
2、下载SELinux策略以强化Nginx Web服务器。  
[list=1]
  • cd /opt
  • wget ‘[url]http://downloads.sourceforge.net/project/selinuxnginx/se-ngix_1_0_10.tar.gz?use_mirror=nchc 


原文链接



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值