diff --git a/bench/config/nginx.py b/bench/config/nginx.py index 66aedd10..8e62efb4 100644 --- a/bench/config/nginx.py +++ b/bench/config/nginx.py @@ -11,24 +11,30 @@ def make_nginx_conf(bench_path, yes=False): config = get_config(bench_path) sites = prepare_sites(config, bench_path) - bench_name = get_bench_name(bench_path) - bench_name_hash = hashlib.sha256(bench_name).hexdigest()[:16] - nginx_conf = template.render(**{ + allow_rate_limiting = config.get('allow_rate_limiting', False) + + template_vars = { "sites_path": sites_path, "http_timeout": config.get("http_timeout"), "sites": sites, "webserver_port": config.get('webserver_port'), "socketio_port": config.get('socketio_port'), "bench_name": bench_name, - "bench_name_hash": bench_name_hash, - "limit_conn_shared_memory": get_limit_conn_shared_memory(), "error_pages": get_error_pages(), - + "allow_rate_limiting": allow_rate_limiting, # for nginx map variable "random_string": "".join(random.choice(string.ascii_lowercase) for i in xrange(7)) - }) + } + + if allow_rate_limiting: + template_vars.update({ + 'bench_name_hash': hashlib.sha256(bench_name).hexdigest()[:16], + 'limit_conn_shared_memory': get_limit_conn_shared_memory() + }) + + nginx_conf = template.render(**template_vars) conf_path = os.path.join(bench_path, "config", "nginx.conf") if not yes and os.path.exists(conf_path): diff --git a/bench/config/templates/nginx.conf b/bench/config/templates/nginx.conf index 008987d7..bab700d4 100644 --- a/bench/config/templates/nginx.conf +++ b/bench/config/templates/nginx.conf @@ -21,7 +21,9 @@ server { root {{ sites_path }}; + {% if allow_rate_limiting %} limit_conn per_host_{{ bench_name_hash }} 8; + {% endif %} {% if ssl_certificate and ssl_certificate_key %} ssl on; @@ -141,7 +143,9 @@ upstream {{ bench_name}}-socketio-server { server 127.0.0.1:{{ socketio_port or 3000 }} fail_timeout=0; } +{% if allow_rate_limiting %} limit_conn_zone $host zone=per_host_{{ bench_name_hash }}:{{ limit_conn_shared_memory }}m; +{% endif %} # setup maps {%- set site_name_variable="$host" %}