{"id":6012,"date":"2015-04-08T13:12:27","date_gmt":"2015-04-08T05:12:27","guid":{"rendered":"https:\/\/webhostinggeeks.com\/howto\/?p=6012"},"modified":"2023-04-28T09:47:15","modified_gmt":"2023-04-28T09:47:15","slug":"nginx-ddos-attack-tutorial","status":"publish","type":"post","link":"https:\/\/webhostinggeeks.com\/howto\/nginx-ddos-attack-tutorial\/","title":{"rendered":"How to Implement Basic Protection Against DDoS Attacks for Nginx"},"content":{"rendered":"<p><a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-a-ddos-attack\/\">Distributed Denial of Service (DDoS) attacks<\/a> can overwhelm a server&#8217;s resources, causing a website or application to become unresponsive or unavailable. Implementing basic protection against DDoS attacks is essential for maintaining server stability and performance.<\/p>\n<p>Here&#8217;s a step-by-step guide on implementing basic protection against DDoS attacks for Nginx.<\/p>\n<h2>Step 1: Update and Secure Your Server<\/h2>\n<p>Ensure your server and software are up-to-date and properly secured. This includes installing the latest security patches, hardening SSH access, and configuring firewalls.<\/p>\n<pre>\r\nsudo apt update && sudo apt upgrade -y\r\n<\/pre>\n<h2>Step 2: Limit Request Rate<\/h2>\n<p>Limit the rate at which clients can send requests to your server. This can help mitigate DDoS attacks by slowing down the rate of incoming requests.<\/p>\n<p>Open your Nginx configuration file:<\/p>\n<pre>\r\nsudo nano \/etc\/nginx\/nginx.conf\r\n<\/pre>\n<p>In the http block, add the following lines:<\/p>\n<pre>\r\nlimit_req_zone $binary_remote_addr zone=one:10m rate=30r\/m;\r\n<\/pre>\n<p>This configuration creates a shared memory zone called &#8220;one&#8221; with a maximum size of 10 MB and allows 30 requests per minute from a single IP address.<\/p>\n<p>Next, open your Nginx server block configuration file:<\/p>\n<pre>\r\nsudo nano \/etc\/nginx\/sites-available\/default\r\n<\/pre>\n<p>Inside the server block, add the following lines within the appropriate location block:<\/p>\n<pre>\r\nlimit_req zone=one burst=30 nodelay;\r\n<\/pre>\n<p>This configuration allows for a burst of 30 requests, which are processed without delay.<\/p>\n<p>Save the file and restart Nginx:<\/p>\n<pre>\r\nsudo systemctl restart nginx\r\n<\/pre>\n<h2>Step 3: Limit Connections<\/h2>\n<p>Limit the number of connections from a single IP address to prevent a single client from consuming too many resources.<\/p>\n<p>In the http block of the Nginx configuration file, add the following lines:<\/p>\n<pre>\r\nlimit_conn_zone $binary_remote_addr zone=addr:10m;\r\n<\/pre>\n<p>This configuration creates a shared memory zone called &#8220;addr&#8221; with a maximum size of 10 MB to store connection information.<\/p>\n<p>In the server block of the Nginx server block configuration file, add the following lines within the appropriate location block:<\/p>\n<pre>\r\nlimit_conn addr 10;\r\n<\/pre>\n<p>This configuration limits the number of concurrent connections from a single IP address to 10.<\/p>\n<p>Save the file and restart Nginx:<\/p>\n<pre>\r\nsudo systemctl restart nginx\r\n<\/pre>\n<h2>Step 4: Enable Connection Timeouts<\/h2>\n<p>Set timeouts for connections to mitigate slow HTTP attacks.<\/p>\n<p>In the http block of the Nginx configuration file, add the following lines:<\/p>\n<pre>\r\nclient_body_timeout 10;\r\nclient_header_timeout 10;\r\nkeepalive_timeout 5 5;\r\nsend_timeout 10;\r\n<\/pre>\n<p>These settings configure various timeouts for client connections, helping to mitigate slow HTTP attacks.<\/p>\n<p>Save the file and restart Nginx:<\/p>\n<pre>\r\nsudo systemctl restart nginx\r\n<\/pre>\n<h2>Commands Mentioned:<\/h2>\n<ul>\n<li><span class=\"fw-bold\">apt update &#038;&#038; apt upgrade -y<\/span> &#8211; Updates the system packages to their latest versions.<\/li>\n<li><span class=\"fw-bold\">nano \/etc\/nginx\/nginx.conf<\/span> &#8211; Opens the main Nginx configuration file for editing.<\/li>\n<li><span class=\"fw-bold\">nano \/etc\/nginx\/sites-available\/default<\/span> &#8211; Opens the default Nginx server block configuration file for editing.<\/li>\n<li><span class=\"fw-bold\">systemctl restart nginx<\/span> &#8211; Restarts the Nginx service to apply the configuration changes.<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>By implementing these basic <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-a-ddos-attack\/\">DDoS<\/a> protection techniques, you can increase the resilience of your Nginx server against DDoS attacks. While these measures can help, they may not be enough to stop all DDoS attacks. For comprehensive protection, consider using a DDoS mitigation service, like Cloudflare or AWS Shield, and closely monitor your server&#8217;s performance and resource usage.<\/p>\n<p>Remember to always keep your server up-to-date, regularly review your Nginx configuration, and implement additional security measures as needed. These practices will help you maintain a secure and stable server environment, reducing the likelihood of successful DDoS attacks.<\/p>\n<p>We encourage you to share your thoughts, comments, and suggestions for improvements to this guide. Your feedback is invaluable in helping us provide the most accurate and useful information possible.<\/p>\n<p>In addition to the basic DDoS protection techniques discussed in this guide, you might also consider implementing:<\/p>\n<p><strong>Geo-blocking:<\/strong> If your website or application has a specific target audience, you can block traffic from other countries to reduce the attack surface.<\/p>\n<p><strong>HTTP flood protection:<\/strong> Tools like ModSecurity or Fail2ban can help protect your server against HTTP flood attacks by detecting and blocking malicious traffic.<\/p>\n<p><strong>Web Application Firewall (WAF):<\/strong> A WAF can help protect your server from various types of attacks, including DDoS attacks, by filtering and monitoring HTTP traffic.<\/p>\n<p>By staying vigilant and implementing a range of security measures, you can better protect your Nginx server against DDoS attacks and ensure the continued availability of your website or application.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Distributed Denial of Service (DDoS) attacks can overwhelm a server&#8217;s resources, causing a website or application to become unresponsive or unavailable. Implementing basic protection against DDoS attacks is essential for&#8230;<\/p>\n","protected":false},"author":6,"featured_media":6240,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wds_primary_category":0,"footnotes":""},"categories":[1014,1066],"tags":[1325,1605,2085],"class_list":["post-6012","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nginx","category-system-performance","tag-ddos-protection","tag-nginx","tag-performance"],"_links":{"self":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/6012","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/comments?post=6012"}],"version-history":[{"count":0,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/6012\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media\/6240"}],"wp:attachment":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media?parent=6012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/categories?post=6012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/tags?post=6012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}