1. 概述
自己搭建过网站的同学应该知道:网络攻击是常有的事,如:CGI工具、DDos工具、SQL注入、XSS攻击、CSRF攻击等等。这些网络轻则导致服务器宕机,重则入侵你的服务器系统,盗取或破坏服务器的数据。
网站安全建设,就是要对网站的网络攻击进行防护,提高网站的安全性,这是保证我们自建网站安全稳定运行的关键。下面就介绍一下网站安全建设的一些方法。
2. 环境说明
3. 设置云服务器的安全防火墙
产品与服务 -> 云安全中心 -> 总览
这里能看到自己云服务器的安全评分和待处理风险项。
点击“立即处理”,按提示进行相关的操作。
4. 设置web服务器的安全配置
4.1. 防止信息暴露
- 删除服务器标识:防止泄露Nginx版本等信息。
- 禁止目录索引:防止Nginx在无默认文档时列出目录内容。
1 2 3 4 5
|
location / { autoindex off; }
|
4.2. 限制访问
IP黑名单配置: blacklist.conf
1 2 3 4 5 6
| deny 31.220.1.83; deny 197.58.33.0/24;
allow all;
|
引用 blacklist.conf 配置
1 2 3 4 5 6
|
location / { include /etc/nginx/sites-available/blacklist.conf; }
|
4.3. 防止DDoS攻击
- 限制请求速率
1 2 3 4 5 6 7 8 9 10 11
|
limit_req_zone $binary_remote_addr zone=req_limit:10m rate=1r/s;
location / { limit_req zone=req_limit burst=5; limit_req _log_level error; limit_req_status 503; }
|
- 限制并发连接数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
limit_conn_zone $binary_remote_addr zone=conn_limit_per_ip:10m;
limit_conn_zone $server_name zone=conn_limit_per_server:10m;
location / { limit_conn conn_limit_per_ip 2; limit_conn conn_limit_per_server 50; }
|
- 限制下载速度
1 2 3 4 5 6 7
| location / { limit_rate_after 500k; limit_rate 100k; }
|
4.4. 防止爬虫抓取
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
server { if ($http_user_agent ~* (Scrapy|Curl|HttpClient)) { return 444; }
if ($http_user_agent ~ "Bytespider|FeedDemon|JikeSpider|Indy Library|Alexa Toolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|CoolpadWebkit|Java|Feedly|UniversalFeedParser|ApacheBench|Microsoft URL Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|python-requests|lightDeckReports Bot|YYSpider|DigExt|YisouSpider|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms|^$" ) { return 444; } if ($request_method !~ ^(GET|HEAD|POST)$) { return 403; } }
|
4.5. 防非法引用(防盗链)
1 2 3 4 5 6
| location ~ \.(jpg|jpeg|png|gif)$ { valid_referers none blocked yourdomain.com *.yourdomain.com; if ($invalid_referer) { return 403; } }
|
4.6. 路径错误时跳转到404页面
1 2 3 4 5
|
proxy_intercept_errors on; fastcgi_intercept_errors on; error_page 404 /404.html;
|