New nginx module: ngx_softlimit

Zeljko Tomic scyth at
Wed Jun 30 20:28:51 MSD 2010


I wrote a small, but sometimes very useful module for limiting the number of active connections, in a "soft" way.

Description and usage:
In situation when using nginx for serving static files and primarily as a proxy/proxy_cache with some slower backend (or even php fastcgi), limits of connections and requests that nginx can handle are much higher than they are on backend server(s). When you know the limits of your backend, it is bad practice to apply those limits on nginx's worker_connections, because it's obvious that when limits are met, new requests won't come through and this leaves you with bad visitor experience.

Better way to approach this problem would be to have the means to manipulate content which gets returned to visitor in such scenario. When limits are met, a simple variable is set to TRUE, which can be manipulated through nginx.conf. Not to speak further, here's an example:

 location / {
	softlimit_enable on;
	softlimit_max_conn 50;

	if ( $softlimit_alert ) {
		return 599;
	error_page 598 = @maintenance;

 location @maintenance {
	rewrite  ^(.*)$  /maintenance.html last;

Download and install:
# wget
# tar xzf ngx_softlimit-0.1.tar.gz
# ./configure --add-module=/path/to/ngx_softlimit-0.1
# make
# make install

Future development:
As the module relies on ngx counters, it can be easely expanded to support counters other than ngx_stat_active. If some day you decide to put ngx_stat_backends (number of connections to backends), it can be even more useful.
Also, this is my first contribution to the project. I don't know if there's a code review procedure or something like that.. to list this module as a 3rd party one. Let me know if you'd like to put it there.


Zeljko Tomic <scyth at>

More information about the nginx-devel mailing list