【问题标题】:Preventing DDOS attack, for Django app with nginx reverse proxy + gunicorn使用 nginx 反向代理 + gunicorn 的 Django 应用程序防止 DDOS 攻击
【发布时间】:2016-05-11 05:29:40
【问题描述】:

我正在编写一个 Django 应用程序,它使用 nginx 反向代理 + gunicorn 作为生产中的网络服务器。

我想包含阻止来自某个 IP(或 IP 池)的 DDOS 攻击的功能。这是在 nginx 级别,而不是在代码中更深层次。我需要 Web 应用程序防火墙吗?如果是这样,我该如何整合它。

我的项目位于 sites-available 的 nginx 文件有:

server {
    listen 80;
    charset utf-8;
    underscores_in_headers on;
    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {

        root /home/sarahm/djangoproject/djangoapp;
    }

    location /static/admin {

        root /home/sarahm/.virtualenvs/myenv/local/lib/python2.7/site-packages/django/contrib/admin/static/;
    }

    location / {
        proxy_pass_request_headers on;
        proxy_buffering on;
        proxy_buffers 8 24k;
        proxy_buffer_size 2k;
        include proxy_params;
        proxy_pass          http://unix:/home/sarahm/djangoproject/djangoapp/djangoapp.sock;
    }


    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /home/sarahm/djangoproject/djangoapp/templates/;
   }
}

让我知道是否应该包含更多信息,以及这些信息应该是什么。

【问题讨论】:

    标签: django security nginx ddos


    【解决方案1】:

    如果您想阻止某些 IP 甚至子网访问您的应用,请将以下代码添加到您的 server 块中:

    #Specify adresses that are not allowed to access your server
        deny 192.168.1.1/24;
        deny 192.168.2.1/24;
        allow all;
    

    此外,如果您不使用 REST,那么您可能希望通过将以下内容添加到您的 server 块中来限制可能的 HTTP 动词:

    if ($request_method !~ ^(GET|HEAD|POST)$ ) {
        return 403;
    }
    

    为了减少 DoS 攻击的可能性,您可能希望通过将以下内容添加到 NGINX nginx.conf 来限制来自单个主机的可能请求数量(请参阅 http://nginx.org/en/docs/stream/ngx_stream_limit_conn_module.html):

    limit_conn_zone $binary_remote_addr zone=limitzone:1M;
    

    以及以下到您的server 块:

    limit_conn limitzone  20;
    

    nginx.conf 的其他一些有用设置,如果设置正确,有助于缓解 DoS:

    server_tokens off;
    autoindex off;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    client_body_timeout 10;
    client_header_timeout 10;
    send_timeout 10;
    keepalive_timeout  20 15;
    
    open_file_cache max=5000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;
    

    由于在这里解释这些内容过于广泛,建议您查看文档http://nginx.org/en/docs/ 了解详细信息。虽然选择正确的值是通过对特定设置的反复试验来实现的。

    Django 将错误页面本身作为模板提供,因此您应该删除:

    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /home/sarahm/djangoproject/djangoapp/templates/;
    

    如果您真的不关心日志记录,也可以选择将 access_log off; log_not_found off; 添加到 static

    location /static/ {
        access_log off;
        log_not_found off;
        root /home/sarahm/djangoproject/djangoapp;
    }
    

    这将降低文件系统请求的频率,从而提高性能。

    NGINX 是一个很棒的网络服务器,设置它是一个广泛的话题,所以最好要么阅读文档(至少是 HOW-TO 部分),要么找到一篇文章来描述适合你的情况的设置。

    【讨论】:

    • 感谢您帮助我开始 Nikita。我会按照您的建议阅读文档,并检查您发布的设置。
    • 为了完整起见,您对 Naxsi (github.com/nbs-system/naxsi) 的回答还有什么要补充的吗?或者这与 DDOS 攻击无关?
    • 对不起,我对纳西族没有任何经验可言。总的来说,我会说,除非它真的很特别,否则你的应用程序不会让“坏”人发起攻击,除非它出名或会影响其他人的业务(竞争)。 DDoS 的主要目的是为了阻止网站所有者做生意,这是由不诚实的竞争支付的,或者是为了让网站所有者支付赎金,因为在 DDoS 攻击下所有者会损失金钱(即电子商务)。
    • 如果网站越来越大或者是主要的商业资产,那么值得聘请专业人士来组织防御,或者至少使用 cloudflare 等特殊服务。在此之前,您只需要制作一些简单的措施,其中一些是我建议的,以限制无意 DoS 效应的影响,这可能是由索引或解析您的网站等引起的。因为如果有人真的想要 DDoS 它,他们会成功,除非你有昂贵的服务器设置。
    • 据此:t37.net/… 似乎sendfile 在使用 nginx 作为反向代理时不是必需的(这就是我正在做的)。因此,在我的情况下,我是否应该单独设置 tcp_nopush on;tcp_nodelay on; 并保持 sendfile 关闭?
    猜你喜欢
    • 1970-01-01
    • 2020-04-07
    • 2020-10-27
    • 2016-03-19
    • 2017-07-17
    • 2019-01-04
    • 2019-10-18
    • 2019-08-09
    • 1970-01-01
    相关资源
    最近更新 更多