|
如何最大程度保证服务器安全(六)
创建好密码文件后,后面的用户可以使用下面的命令进行追加: # htpasswd -s /usr/local/nginx/conf/.htpasswd/passwd userName 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虚拟化,它们也使用了容器的概念。 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或其它用户,一个典型的权限设置如下:
|
|||||||||||||||
|
|
| 最新文章 |
| 图片主题 | ||||
| |
||||
| 热门文章 |
| 推荐文章 | ||||
| |
||||
| 相关文章 |
| 便民服务 | ||||||||||||||||||||||||||||||||
| |
||||||||||||||||||||||||||||||||