20个Nginx Web服务器最佳安全实践

原创
安全 黑客攻防
本文的主要目是介绍如何提高运行在Linux或UNIX类操作系统上的Nginx Web服务器的安全性。

【51CTO.com精选译文】20个Nginx Web服务器最佳安全实践

Nginx是一个轻量级,高性能的Web服务器/反向代理和电子邮件代理(IMAP/POP3),它可以运行在UNIX,GNU/Linux,BSD变种,MAC OS X,Solaris和Microsoft Windows上。根据Netcraft的调查数据显示,互联网上6%的域名都使用了Nginx Web服务器。Nginx是解决C10K问题的服务器之一,与传统服务器不一样,Nginx不依赖于线程处理请求,相反,它使用了一个更具扩展性的事件驱动(异步)架构。Nginx在很多高流量网站上得到了应用,如WordPress,Hulu,Github和SourceForge。

本文的主要目是介绍如何提高运行在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的配置是否正确:

# /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

要让修改后的配置生效,执行下面的命令:

# /usr/local/nginx/sbin/nginx -s reload

如果要停止服务器,运行:

# /usr/local/nginx/sbin/nginx -s stop

1、开启SELinux

SELinux(安全增强的Linux)是一个Linux内核功能,它提供了一个机制支持访问控制安全策略,提供了巨大的安全保护能力,它可以防止大多数系统root级攻击,请参考“如何在CentOS/Red Hat系统上开启SELinux”(http://www.cyberciti.biz/faq/rhel-fedora-redhat-selinux-protection/)。

运行getsebool –a命令查看SELinux设置项:

getsebool -a | less
getsebool -a | grep off
getsebool -a | grep o

然后使用setsebool命令开启需要的配置项,注意:开启SELinux后,在RHEL或CentOS上通常会增加2-8%的系统开销。

2、通过mount参数提供最低权限

为你的/html/php文件创建独立的分区,例如,创建一个/dev/sda5分区挂载在/ngnix上,确定/ngnix使用了noexec,nodev和nosetuid权限挂载。下面是我的一个挂载实例:

LABEL=/nginx     
/nginx          
ext3   
defaults,nosuid,noexec,nodev 1 2

注意你需要使用fdisk和mkfs.ext3命令创建一个新分区。

3、通过/etc/sysctl.conf加固

可以通过/etc/sysctl.conf控制和配置Linux内核及网络设置。

另外,请参考:

# 避免放大攻击
net.ipv4.icmp_echo_ignore_broadcasts = 1
# 开启恶意icmp错误消息保护
net.ipv4.icmp_ignore_bogus_error_responses = 1
# 开启SYN洪水攻击保护
net.ipv4.tcp_syncookies = 1
# 开启并记录欺骗,源路由和重定向包
net.ipv4.conf.all.log_martians = 1
net.ipv4.conf.default.log_martians = 1
# 处理无源路由的包
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
 
# 开启反向路径过滤

net.ipv4.conf.all.rp_filter = 1

net.ipv4.conf.default.rp_filter = 1

# 确保无人能修改路由表

net.ipv4.conf.all.accept_redirects = 0

net.ipv4.conf.default.accept_redirects = 0

net.ipv4.conf.all.secure_redirects = 0

net.ipv4.conf.default.secure_redirects = 0

# 不充当路由器

net.ipv4.ip_forward = 0

net.ipv4.conf.all.send_redirects = 0

net.ipv4.conf.default.send_redirects = 0

# 开启execshild

kernel.exec-shield = 1

kernel.randomize_va_space = 1

# IPv6设置

net.ipv6.conf.default.router_solicitations = 0

net.ipv6.conf.default.accept_ra_rtr_pref = 0

net.ipv6.conf.default.accept_ra_pinfo = 0

net.ipv6.conf.default.accept_ra_defrtr = 0

net.ipv6.conf.default.autoconf = 0

net.ipv6.conf.default.dad_transmits = 0

net.ipv6.conf.default.max_addresses = 1

# 优化LB使用的端口

# 增加系统文件描述符限制

fs.file-max = 65535

# 允许更多的PIDs (减少滚动翻转问题); may break some programs 32768

kernel.pid_max = 65536

# 增加系统IP端口限制

net.ipv4.ip_local_port_range = 2000 65000

# 增加TCP最大缓冲区大小

net.ipv4.tcp_rmem = 4096 87380 8388608

net.ipv4.tcp_wmem = 4096 87380 8388608

# 增加Linux自动调整TCP缓冲区限制

# 最小,默认和最大可使用的字节数

# 最大值不低于4MB,如果你使用非常高的BDP路径可以设置得更高

# Tcp窗口等

net.core.rmem_max = 8388608

net.core.wmem_max = 8388608

net.core.netdev_max_backlog = 5000

net.ipv4.tcp_window_scaling = 1

◆ Linux VM调优(内存)子系统(http://www.cyberciti.biz/faq/linux-kernel-tuning-virtual-memory-subsystem/

◆ Linux网络堆栈调优(缓冲区大小)提高网络性能(http://www.cyberciti.biz/faq/linux-tcp-tuning/)#p#

4、移除所有不需要的Nginx模块

你需要最大限度地将Nginx加载的模块最小化,我的意思是满足Web服务器需要就可以了,多余的模块一个不留,例如,禁用SSI和autoindex模块的命令如下:

# ./configure --without-http_autoindex_module --without-http_ssi_module
# make
# make install

在编译Nginx服务器时,使用下面的命令查看哪些模块应该启用,哪些模块应该禁用:

# ./configure --help | less

禁用你不需要的Nginx模块。

修改Nginx版本头信息(可选),编辑src/http/ngx_http_header_filter_module.c:

# vi +48 src/http/ngx_http_header_filter_module.c

找到下面两行:

static char ngx_http_server_string[] = "Server: nginx" CRLF;
static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;

将其修改为:

static char ngx_http_server_string[] = "Server: Ninja Web Server" CRLF;
static char ngx_http_server_full_string[] = "Server: Ninja Web Server" CRLF;

保存并关闭文件。现在可以开始编译服务器了,将下面的配置代码添加到nginx.conf中,禁止在所有自动产生的错误页面中显示Nginx版本号:

server_tokens off

5、使用mod_security(仅适用于后端Apache服务器)

Mod_security为Apache提供了一个应用程序级防火墙,为所有后端Apache Web服务器安装mod_security模块,可以阻止许多注入攻击。#p#

6、配置SELinux策略加固Nginx

默认情况下,SELinux没有保护Nginx Web服务器,可以手动配置进行保护,首先安装SELinux编译时需要的支持包:

# yum -y install selinux-policy-targeted selinux-policy-devel

从项目主页(http://sourceforge.net/projects/selinuxnginx/)下载SELinux策略:

# cd /opt
# wget 'http://downloads.sourceforge.net/project/selinuxnginx/se-ngix_1_0_10.tar.
gz?use_mirror=nchc'

 

解压:

# tar -zxvf se-ngix_1_0_10.tar.gz

编译:

# cd se-ngix_1_0_10/nginx
# make

输出示例:

Compiling targeted nginx module
/usr/bin/checkmodule:  loading policy configuration from tmp/nginx.tmp
/usr/bin/checkmodule:  policy configuration loaded
/usr/bin/checkmodule:  writing binary representation (version 6) to tmp/nginx.mod
Creating targeted nginx.pp policy package
rm tmp/nginx.mod.fc tmp/nginx.mod

安装生成的nginx.pp SELinux模块:

# /usr/sbin/semodule -i nginx.pp

#p#

7、通过iptables防火墙设置限制

下面的防火墙脚本可以阻止一切请求,只允许:

◆ 入站HTTP请求(TCP 80端口)

◆ 入站ICMP ping请求

◆ 出站NTP请求(端口123)

◆ 出站SMTP请求(TCP端口25)

#!/bin/bash
IPT="/sbin/iptables"
#### IPS ######
# 获得服务器公共IP
SERVER_IP=$(ifconfig eth0 | grep 'inet addr:' | awk -F'inet addr:' '{ print $2}' | awk '{ print $1}')
LB1_IP="204.54.1.1"
LB2_IP="204.54.1.2"
# 实现某些智能逻辑,以便我们可以在LB2上使用damm脚本
OTHER_LB=""
SERVER_IP=""
[[ "$SERVER_IP" == "$LB1_IP" ]] && OTHER_LB="$LB2_IP" || OTHER_LB="$LB1_IP"
[[ "$OTHER_LB" == "$LB2_IP" ]] && OPP_LB="$LB1_IP" || OPP_LB="$LB2_IP"
### IPs ###
PUB_SSH_ONLY="122.xx.yy.zz/29" #### 文件 #####

BLOCKED_IP_TDB=/root/.fw/blocked.ip.txt

SPOOFIP="127.0.0.0/8 192.168.0.0/16 172.16.0.0/12 10.0.0.0/8 169.254.0.0/16 0.0.0.0/8 240.0.0.0/4 255.255.255.255/32 168.254.0.0/16 224.0.0.0/4 240.0.0.0/5 248.0.0.0/5 192.0.2.0/24"

BADIPS=$( [[ -f ${BLOCKED_IP_TDB} ]] && egrep -v "^#|^$" ${BLOCKED_IP_TDB})

### 接口 ###

PUB_IF="eth0"   # public interface

LO_IF="lo"      # loopback

VPN_IF="eth1"   # vpn / private net

### 启动防火墙 ###

echo "Setting LB1 $(hostname) Firewall..."

# 删除和关闭一切

$IPT -P INPUT DROP

$IPT -P OUTPUT DROP

$IPT -P FORWARD DROP

# 不受限制的lo访问

$IPT -A INPUT -i ${LO_IF} -j ACCEPT

$IPT -A OUTPUT -o ${LO_IF} -j ACCEPT

# 不受限制的vpn/pnet访问

$IPT -A INPUT -i ${VPN_IF} -j ACCEPT

$IPT -A OUTPUT -o ${VPN_IF} -j ACCEPT

# 删除sync

$IPT -A INPUT -i ${PUB_IF} -p tcp ! --syn -m state --state NEW -j DROP

# 删除碎片

$IPT -A INPUT -i ${PUB_IF} -f -j DROP

$IPT  -A INPUT -i ${PUB_IF} -p tcp --tcp-flags ALL FIN,URG,PSH -j DROP

$IPT  -A INPUT -i ${PUB_IF} -p tcp --tcp-flags ALL ALL -j DROP

# 删除空包

$IPT  -A INPUT -i ${PUB_IF} -p tcp --tcp-flags ALL NONE -m limit --limit 5/m --limit-burst 7 -j LOG --log-prefix " NULL Packets "

$IPT  -A INPUT -i ${PUB_IF} -p tcp --tcp-flags ALL NONE -j DROP

$IPT  -A INPUT -i ${PUB_IF} -p tcp --tcp-flags SYN,RST SYN,RST -j DROP

# 删除XMAS

$IPT  -A INPUT -i ${PUB_IF} -p tcp --tcp-flags SYN,FIN SYN,FIN -m limit --limit 5/m --limit-burst 7 -j LOG --log-prefix " XMAS Packets "

$IPT  -A INPUT -i ${PUB_IF} -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP

# 删除FIN包扫描

$IPT  -A INPUT -i ${PUB_IF} -p tcp --tcp-flags FIN,ACK FIN -m limit --limit 5/m --limit-burst 7 -j LOG --log-prefix " Fin Packets Scan "

$IPT  -A INPUT -i ${PUB_IF} -p tcp --tcp-flags FIN,ACK FIN -j DROP

$IPT  -A INPUT -i ${PUB_IF} -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP

# 记录并放弃广播/多播和无效数据包

$IPT  -A INPUT -i ${PUB_IF} -m pkttype --pkt-type broadcast -j LOG --log-prefix " Broadcast "

$IPT  -A INPUT -i ${PUB_IF} -m pkttype --pkt-type broadcast -j DROP

$IPT  -A INPUT -i ${PUB_IF} -m pkttype --pkt-type multicast -j LOG --log-prefix " Multicast "

$IPT  -A INPUT -i ${PUB_IF} -m pkttype --pkt-type multicast -j DROP

$IPT  -A INPUT -i ${PUB_IF} -m state --state INVALID -j LOG --log-prefix " Invalid "

$IPT  -A INPUT -i ${PUB_IF} -m state --state INVALID -j DROP

# 记录和阻止欺骗IP

$IPT -N spooflist

for ipblock in $SPOOFIP

do    

$IPT -A spooflist -i ${PUB_IF} -s $ipblock -j LOG --log-prefix " SPOOF List Block "       

$IPT -A spooflist -i ${PUB_IF} -s $ipblock -j DROP

done

$IPT -I INPUT -j spooflist

$IPT -I OUTPUT -j spooflist

$IPT -I FORWARD -j spooflist

# 只允许从选定的公共IP使用SSH

for ip in ${PUB_SSH_ONLY}

do        $IPT -A INPUT -i ${PUB_IF} -s ${ip} -p tcp -d ${SERVER_IP} --destination-port 22 -j ACCEPT       

$IPT -A OUTPUT -o ${PUB_IF} -d ${ip} -p tcp -s ${SERVER_IP} --sport 22 -j ACCEPT

done

# 允许入站ICMP ping

$IPT -A INPUT -i ${PUB_IF} -p icmp --icmp-type 8 -s 0/0 -m state --state NEW,ESTABLISHED,RELATED -m limit --limit 30/sec  -j ACCEPT

$IPT -A OUTPUT -o ${PUB_IF} -p icmp --icmp-type 0 -d 0/0 -m state --state ESTABLISHED,RELATED -j ACCEPT

# 允许入站HTTP端口80

$IPT -A INPUT -i ${PUB_IF} -p tcp -s 0/0 --sport 1024:65535 --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT

$IPT -A OUTPUT -o ${PUB_IF} -p tcp --sport 80 -d 0/0 --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT

# 允许出站NTP

$IPT -A OUTPUT -o ${PUB_IF} -p udp --dport 123 -m state --state NEW,ESTABLISHED -j ACCEPT

$IPT -A INPUT -i ${PUB_IF} -p udp --sport 123 -m state --state ESTABLISHED -j ACCEPT

# 允许出站SMTP

$IPT -A OUTPUT -o ${PUB_IF} -p tcp --dport 25 -m state --state NEW,ESTABLISHED -j ACCEPT

$IPT -A INPUT -i ${PUB_IF} -p tcp --sport 25 -m state --state ESTABLISHED -j ACCEPT

### 在这里添加其他规则 ####

#######################

# 删除并记录其它数据包

$IPT -A INPUT -m limit --limit 5/m --limit-burst 7 -j LOG --log-prefix " DEFAULT DROP "

$IPT -A INPUT -j DROP

exit 0

#p# 8、控制缓冲区溢出攻击

编辑nginx.conf设置所有客户端可用的缓冲区大小限制:

# vi /usr/local/nginx/conf/nginx.conf

具体设置如下:

## Start: Size Limits & Buffer Overflows ##  client_body_buffer_size  1K;
 
client_header_buffer_size 1k;  client_max_body_size 1k;  large_client_header_buffers 2 1k; 
## END: Size Limits & Buffer Overflows ##

说明:

client_body_buffer_size 1k:默认是8k或16k,指定客户端请求主体缓冲区大小。

client_header_buffer_size 1k:指定来自客户端请求头的headerbuffer大小,对于大多数请求,1k的缓冲区大小已经足够,如果你自定义了消息头或有更大的Cookie,你可以增加其大小。

client_max_body_size 1k:客户端请求中可接受的主体最大大小,由请求头中的Content-Length表示,如果大小大于给定的尺寸,客户端将会收到一条“Request Entity Too Large(413)”的错误,如果你要通过POST方法上传文件,可以将该值设大一些。

large_client_header_buffers 2 1k:为客户端请求中较大的消息头指定的缓存最大数量和大小,默认情况下,一个缓冲区的大小等于页面的大小,根据平台的不同可能是4K或8K,如果在请求连接的末尾状态转换为保持活动(keep-alive),这些缓冲区就被释放,2x1K将可以接收2KB数据的URI,这样有助于打击机器人攻击和DoS攻击。

另外,你还需要控制超时时间,提高服务器性能,降低客户端的等待时间,做如下修改:

## Start: Timeouts 
##  client_body_timeout   
10;  client_header_timeout 10;
 
keepalive_timeout    
5 5;  send_timeout          
10;

## End: Timeouts ##

client_body_timeout 10:设置客户端请求主体读取超时时间,如果在这个时间后客户端还没有发送任何数据,Nginx返回“Request time out(408)”错误,默认值是60。

client_header_timeout 10:设置客户端请求头读取超时时间,如果在这个时间后客户端还没有发送任何数据,Nginx返回“Request time out(408)”错误。

keepalive_timeout 5 5:第一个参数指定客户端连接保持活动的超时时间,在这个时间之后,服务器会关掉连接,第二个参数是可选的,它指定了消息头保持活动的有效时间,即响应中的timeout=time,它可以告诉某些浏览器关闭连接,因此服务器就不必关闭连接了,如果没有这个参数,Nginx不会发送Keep-Alive头。

send_timeout 10:指定响应客户端的超时时间,这个超时仅限于两个阅读活动之间的时间,如果这个时间后客户端没有任何活动,Nginx将会关闭连接。#p#

9、控制并发连接

你可以使用NginxHttpLimitZone模块限制指定会话,或某个IP的并发连接数,编辑nginx.conf:

### Directive describes the zone, in which the session states are stored i.e. store in slimits. 
### 1m can handle 32000 sessions with 32 bytes/session, set to 5m x 32000 session ###
      
limit_zone slimits $binary_remote_addr 5m;
 
### Control maximum number of simultaneous connections for one session i.e. ###

### restricts the amount of connections from a single ip address ###
       
limit_conn slimits 5;
 

上述设置可以限制远程客户端每IP地址不能超过5个同时打开的连接。

10、只允许访问指定的域名

如果有机器人程序在随机扫描所有域,那就阻止它访问,你必须配置只允许虚拟域或反向代理请求。

## Only requests to our Host are allowed i.e. nixcraft.in, images.nixcraft.in and www.nixcraft.in   
if ($host !~ ^(nixcraft.in|www.nixcraft.in|images.nixcraft.in)$ ) {         
return 444;      }

##

11、限制可用的方法

GET和POST是互联网上最常用的方法,RFC 2616定义了Web服务器可用的方法,如果一个Web服务器不要求实现所有方法,那些方法就应该被禁止掉,下面的代码将过滤所有方法,只允许GET,HEAD和POST方法:

## Only allow these request methods ##     
if ($request_method !~ ^(GET|HEAD|POST)$ ) {         
return 444;     }

## Do not accept DELETE, SEARCH and other methods ##

关于HTTP方法的更多信息:

GET方法用于请求文档,如http://www.cyberciti.biz/index.php

HEAD方法与GET相同,但服务器不会在响应中只返回消息主体。

POST方法功能就多了,如通过表单存储或更新数据,订购一个产品,发送电子邮件等,通常使用服务器端脚本(如PHP,Perl,Python等)处理,如果你要上传文件或在服务器上处理表单就必须用它。#p#

12a、如何阻止某些用户代理(User-Agents)?

你可以轻松阻止用户代理,如扫描器,机器人和垃圾邮件,它们可能会滥用你的服务器。

## Block download agents ##     
if ($http_user_agent ~* LWP::Simple|BBBike|wget) {         
return 403;     }

##
 

阻止msnbot和scrapbot机器人:

## Block some robots ##     
if ($http_user_agent ~* msnbot|scrapbot) {           
return 403;     }

12b、如何阻止被提名的垃圾邮件

被提名的垃圾邮件都很危险,它们可能会损害你的SEO排名,可以使用下面的代码阻止访问垃圾邮件发送者:

## Deny certain Referers ###   
if ( $http_referer ~* (babes|forsale|girl|jewelry|love|nudit|organic|poker|porn|sex|teen) )     
{        
# return 404;        
return 403;     }

##

13、如何停止图片热链

图片或HTML热链是指有人在他们的网站上引用了你网站的图片,你必须为其它网站的流量支付贷款费用,有点象是网站劫持,通常这种情况发生在博

客和论坛中,我强烈建议你在服务器级停止并阻止图片热链。

# Stop deep linking or hot linking
location /images/ {  valid_referers none blocked www.example.com example.com;   
if ($invalid_referer) {     
return   403;   }

}

例子:重写并显示禁令图片:

valid_referers blocked www.example.com example.com; 
if ($invalid_referer) { rewrite ^/images/uploads.*\.(gif|jpg|jpeg|png)$ 
http://www.examples.com/banned.jpg last

 }

另外,请参考“How-to:使用Nginx映射阻止图片热链”(http://nginx.org/pipermail/nginx/2007-June/001082.html)。

14、目录限制

你可以为特定目录设置访问控制,所有网页目录都应配置为按需访问。

通过IP地址限制访问,你可以限制访问/docs/目录的IP地址:

location /docs/ {  ## block one workstation  deny    192.168.1.1;

## allow anyone in 192.168.1.0/24  allow   192.168.1.0/24;

## drop rest of the world  deny    all;

}

通过密码保护目录,首先创建一个密码文件,再添加一个用户vivek:

# mkdir /usr/local/nginx/conf/.htpasswd/
# htpasswd -c /usr/local/nginx/conf/.htpasswd/passwd vivek

编辑nginx.conf添加需要保护的目录:

### Password Protect /personal-images/ and /delta/ directories ###
location ~ /(personal-images/.*|delta/.*) {  auth_basic  "Restricted";  
auth_basic_user_file   /usr/local/nginx/conf/.htpasswd/passwd;

}

创建好密码文件后,后面的用户可以使用下面的命令进行追加:

# htpasswd -s /usr/local/nginx/conf/.htpasswd/passwd userName

#p#15、Nginx SSL配置

HTTP是一个纯文本协议,很容易被窃听,你应该使用SSL加密传输的信息。

首先需要创建一个SSL证书,输入下面的命令:

# cd /usr/local/nginx/conf
# openssl genrsa -des3 -out server.key 1024
# openssl req -new -key server.key -out server.csr
# cp server.key server.key.org
# openssl rsa -in server.key.org -out server.key
# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

编辑nginx.conf,找到对应位置,做如下修改:

server {    
server_name example.com;    
listen 443;    
ssl on;    
ssl_certificate /usr/local/nginx/conf/server.crt;    
ssl_certificate_key /usr/local/nginx/conf/server.key;    
access_log /usr/local/nginx/logs/ssl.access.log;    
error_log /usr/local/nginx/logs/ssl.error.log;

重启Nginx:

# /usr/local/nginx/sbin/nginx -s reload

另外,请参考Nginx SSL文档(http://wiki.nginx.org/NginxHttpSslModule)。

16、Nginx和PHP安全技巧

PHP是流行的服务器端脚本语言,对/etc/php.ini做如下修改:

# 禁用危险的函数
disable_functions = phpinfo, system, mail, exec
## 限制资源  ##
# 每个脚本的最大执行时间,单位秒
max_execution_time = 30
# 每个脚本解析请求数据的最大时间
max_input_time = 60
# 每个脚本可以消耗的最大内存(8MB)
memory_limit = 8M
# PHP要接收的POST数据最大大小
post_max_size = 8M
# 是否允许HTTP文件上传
file_uploads = Off
# 允许上传的最大文件大小
upload_max_filesize = 2M
# 不将PHP错误消息暴露给外部用户
display_errors = Off
# 启用安全模式
safe_mode = On
# 只允许访问隔离目录中的可执行文件
safe_mode_exec_dir = php-required-executables-path
# 限制外部访问PHP资源
safe_mode_allowed_env_vars = PHP_
# 限制泄露PHP信息
expose_php = Off
# 记录所有错误
log_errors = On
# 不为输入数据注册全局
register_globals = Off
# 最小化允许的php post大小
post_max_size = 1K
# 确保PHP重定向正确
cgi.force_redirect = 0
# 禁止上传,除非必要
file_uploads = Off
# 启用SQL安全模式
sql.safe_mode = On
# 避免打开远程文件
allow_url_fopen = Off

另外,请参考“PHP安全:限制脚本使用的资源”(http://www.cyberciti.biz/faq/php-resources-limits/),“PHP.INI:禁用exec,shell_exec,system,popen和其它功能提高安全”(http://www.cyberciti.biz/faq/linux-unix-apache-lighttpd-phpini-disable-functions/)。

17、尽可能在Chroot Jail(容器)中运行Nginx

将Nginx放入Chroot Jail可以最大限度地减少被攻击的危险,它将Web服务器隔离到文件系统的专用区域,注意你不能使用传统的chroot方法设置Nginx,但你可以使用FreeBSD jails,Xen或OpenVZ虚拟化,它们也使用了容器的概念。#p#

18、在防火墙级限制每个IP的连接

Web服务器必须时刻关注连接和每秒的连接限制,pf和iptables都可以在访问Nginx服务器之前卡住最终用户。

Linux iptables:每秒卡住的Nginx连接

下面的例子表示如果某个IP在60秒尝试连接到80端口的次数超过了15,iptables将会丢掉来自它的入站连接:

/sbin/iptables -A INPUT -p tcp --dport 80 -i eth0 -m state --state NEW -m recent --set
/sbin/iptables -A INPUT -p tcp --dport 80 -i eth0 -m state --state NEW -m recent 
--update --seconds 60  --hitcount 15 -j DROP
service iptables save

BSD PF:每秒卡住的Nginx连接

编辑/etc/pf.conf,做如下更新,下面的命令限制了每个来源的最大连接数为100,15/5指定某时间跨度内的连接数限制,这里就是5秒内的最大连接数为

15,如果有人违背这条规则,将被加入到abusive_ips表,那么他以后就不能再连接了。最后刷新所有状态。

ebserver_ip="202.54.1.1"
table  persist
block in quick from 
pass in on $ext_if proto tcp to $webserver_ip port www flags S/SA keep state (max-src-conn 100, 
max-src-conn-rate 15/5, overload  flush)

请根据你的需要和通信流量调整所有的值(浏览器可能会打开多个连接)。

另外,请参考“PF防火墙脚本示例”(http://bash.cyberciti.biz/firewall/pf-firewall-script/),“iptables防火墙脚本示例”(http://bash.cyberciti.biz/firewall/linux-iptables-firewall-shell-script-for-standalone-server/)。

19、配置操作系统保护Web服务器

除了开启SELinux外,还要给/nginx目录设置正确的权限,运行Nginx的系统用户名是nginx,但在DocumentRoot(/nginx或/usr/local/nginx/html)中的文件不应该

属于该用户,他也不能进行修改。使用下面的命令找出权限设置不当的文件:

# find /nginx -user nginx
# find /usr/local/nginx/html -user nginx

请确保将文件的所有者修改为root或其它用户,一个典型的权限设置如下:

# ls -l /usr/local/nginx/html/

输出示例:

-rw-r--r-- 1 root root 925 Jan  3 00:50 error4xx.html
-rw-r--r-- 1 root root  52 Jan  3 10:00 error5xx.html
-rw-r--r-- 1 root root 134 Jan  3 00:52 index.html

另外,你必须删除由vi或其它文本编辑器创建的不必要的备份文件:

# find /nginx -name '.?*' -not -name .ht* -or -name '*~' -or -name '*.bak*' -or -name '*.old*'
# find /usr/local/nginx/html/ -name '.?*' -not -name .ht* -or -name '*~' 
-or -name '*.bak*' -or -name '*.old*'

给find命令传递-delete参数,它就会自动删除这些文件。

20、限制出站Nginx连接

攻击者可能要在你的Web服务器上使用如wget等工具下载文件,使用iptables阻止来自Nginx用户的出站连接,ipt_owner模块可以匹配各种包创建者的特征,只有在OUTPUT链中的才有效,在这里,允许vivek用户使用80端口连接外部资源(对RHN访问或通过仓库抓取CentOS更新特别有用)。

/sbin/iptables -A OUTPUT -o eth0 -m owner --uid-owner vivek -p tcp 
--dport 80 -m state --state NEW,ESTABLISHED  -j ACCEPT

将上述规则添加到你的iptables基础shell脚本中,不允许nginx Web服务器用户连接外部资源。

附送技巧:观察日志和审核

检查日志文件,可以从中找到攻击者的一些行踪和攻击手段。

# grep "/login.php??" /usr/local/nginx/logs/access_log
# grep "...etc/passwd" /usr/local/nginx/logs/access_log
# egrep -i "denied|error|warn" /usr/local/nginx/logs/error_log

Auditd服务提供了系统审核功能,启动SELinux事件,认证事件,文件修改,帐户修改等的审核服务,象往常一样首先关闭所有服务,然后打开我在“Linux服务器加固”(http://www.cyberciti.biz/tips/linux-security.html)一文中指出的服务。

总结

通过这些设置,你的Nginx服务器就可以对外提供服务了,但你应该根据应用程序安全需要进一步查看另外的资源。例如,WordPress或其它第三方程序都有其自身的安全要求。

原文:Top 20 Nginx WebServer Best Security Practices  作者:VIVEK GITE

【51CTO.COM 独家翻译,转载请注明出处及作者!】

【编辑推荐】

  1. Unix的Web服务器安全指南
  2. 高级Linux安全管理技巧
责任编辑:许凤丽 来源: 51CTO.com
相关推荐

2010-04-13 18:42:55

2011-06-02 10:25:10

Web服务器部署

2011-02-21 10:31:24

2022-09-26 23:13:52

服务器数据安全

2018-02-27 10:43:59

2011-08-30 11:19:25

远程服务器数据中心迁移服务器管理工具

2023-03-30 13:22:45

nginxweb服务器

2020-04-15 20:57:57

NginxWeb服务器

2017-12-27 10:18:09

ApacheNginx服务器

2018-02-28 17:27:28

2018-08-07 08:54:18

2021-05-19 14:14:29

服务器安全数据

2011-09-08 10:42:38

Web服务器Nginx

2010-03-30 18:26:07

Nginx Web服务

2021-11-24 08:00:00

服务器Web系统

2012-05-14 11:52:45

服务器角色

2011-07-26 10:27:07

分析器操作系统服务器

2011-03-23 13:59:43

2019-12-30 16:03:54

Web服务器Nginx

2022-09-07 08:47:50

NGINXLinuxWeb 服务器
点赞
收藏

51CTO技术栈公众号