{"id":100,"date":"2012-12-19T14:51:34","date_gmt":"2012-12-19T14:51:34","guid":{"rendered":"http:\/\/www.nicovs.be\/?p=100"},"modified":"2013-07-11T11:18:56","modified_gmt":"2013-07-11T11:18:56","slug":"nginx-rate-limiting","status":"publish","type":"post","link":"https:\/\/www.nicovs.be\/?p=100","title":{"rendered":"Nginx Rate Limiting"},"content":{"rendered":"<p>If you ever encounter problems on your Nginx-PHP FPM server with all of the sudden increased load, very slow page load, PHP processes being filled for no particular reason&#8230; check php-fpm logs:<\/p>\n<pre class=\"lang:default decode:true\">root@webserver:~# tail \/var\/log\/php5-fpm\/php-fpm.log\r\n[19-Dec-2012 13:53:07] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min\/max_spare_servers), spawning 8 children, there are 0 idle, and 33 total children\r\n[19-Dec-2012 13:53:08] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min\/max_spare_servers), spawning 16 children, there are 0 idle, and 38 total children\r\n[19-Dec-2012 13:53:09] WARNING: [pool www] server reached pm.max_children setting (40), consider raising it<\/pre>\n<p>If you have server monitoring\/google anayltics on your site, and you see no increase of visitors, pages\/second or hits\/second&#8230; then there&#8217;s probably one or another bot crawling your site (I&#8217;ve noticed that robots.txt is not always working&#8230;), or in my case somebody requesting 500 or more pages in 3 seconds from 1 specific IP address. Not accusing anybody from attacking my servers&#8230;<\/p>\n<p>This mass page requests thingie, caused my nginx to throw 499 error to my webserver&#8217;s clients.<\/p>\n<p>I&#8217;ve resolved this by enabled rate limiting inside Nginx.<\/p>\n<p>This is how:<\/p>\n<p>Add the following line to your \/etc\/nginx\/nginx.conf:<\/p>\n<pre class=\"lang:default decode:true\">limit_req_zone  $binary_remote_addr  zone=onerps:10m   rate=1r\/s;<\/pre>\n<p>This area is called\u00a0onerps and is allocated 10MB of storage.Using the $binary_remote_addr variable reduces the size of the states to 64 bytes. Since there can be about 16,000 states in a 1MB zone,10MB allows for about 160000 states. This should be sufficient your your visitors.<\/p>\n<p>The connection rate is limited to one request per second. You can also use something like 30r\/m, which putts a rate limit in place of 30 requests per minute.<\/p>\n<p>Works if not finished yet: To put this limit to work, we use the\u00a0limit_req\u00a0directive. You can use this directive in\u00a0http {},\u00a0server {}, and\u00a0location {}\u00a0containers.<\/p>\n<p>However the best option is to use this in the location {}\u00a0containers that pass requests to your app servers, eg PHP-FPM. because otherwise, if you load a single page with lots of images, CSS, and JavaScript files, you would probably exceed the given rate limit with a single page request :-p<\/p>\n<p>So let&#8217;s put this in a\u00a0location ~ \\.php$ {}\u00a0container:<\/p>\n<pre class=\"lang:default decode:true\">limit_req zone=onerps burst=5;<\/pre>\n<p>If your visitor is doing more than 1 request per second (or 5 in burst mode), the next request will be queued and executed the next second.\u00a0If the number of waiting requests exceed burst, the request is completed with the code 503 &#8220;Service Temporarily Unavailable&#8221;. By default, the burst is zero.<\/p>\n<p>If delaying excess requests within a burst is not necessary, you should use the option\u00a0nodelay:<\/p>\n<pre>limit_req zone=onerps burst=5 nodelay;<\/pre>\n<p>A special thanks to:\u00a0<a href=\"http:\/\/www.howtoforge.com\/rate-limiting-with-nginx\">HowToForge<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you ever encounter problems on your Nginx-PHP FPM server with all of the sudden increased load, very slow page load, PHP processes being filled for no particular reason&#8230; check php-fpm logs: root@webserver:~# tail \/var\/log\/php5-fpm\/php-fpm.log [19-Dec-2012 13:53:07] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min\/max_spare_servers), spawning 8 children, there are [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[31,1],"tags":[30,26,27,28,29],"class_list":["post-100","post","type-post","status-publish","format-standard","hentry","category-php-fpm-2","category-uncategorized","tag-crawl","tag-nginx","tag-php-fpm","tag-rate","tag-rate-limiting"],"_links":{"self":[{"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/posts\/100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=100"}],"version-history":[{"count":6,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/posts\/100\/revisions"}],"predecessor-version":[{"id":104,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/posts\/100\/revisions\/104"}],"wp:attachment":[{"href":"https:\/\/www.nicovs.be\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}