diff --git a/letsencrypt-nginx/letsencrypt_nginx/parser.py b/letsencrypt-nginx/letsencrypt_nginx/parser.py index fb79703dc..93cda2c41 100644 --- a/letsencrypt-nginx/letsencrypt_nginx/parser.py +++ b/letsencrypt-nginx/letsencrypt_nginx/parser.py @@ -257,6 +257,8 @@ class NginxParser(object): ..note :: If replace is True, this raises a misconfiguration error if the directive does not already exist. + ..note :: If replace is False nothing gets added if an identical + block exists already. ..todo :: Doesn't match server blocks whose server_name directives are split across multiple conf files. @@ -480,7 +482,9 @@ def _add_directives(block, directives, replace=False): if not replace: # We insert new directives at the top of the block, mostly # to work around https://trac.nginx.org/nginx/ticket/810 - block.insert(0, directive) + # Only add directive if its not already in the block + if directive not in block: + block.insert(0, directive) else: changed = False if len(directive) == 0: diff --git a/letsencrypt-nginx/letsencrypt_nginx/tests/parser_test.py b/letsencrypt-nginx/letsencrypt_nginx/tests/parser_test.py index b28640d7f..2d6156429 100644 --- a/letsencrypt-nginx/letsencrypt_nginx/tests/parser_test.py +++ b/letsencrypt-nginx/letsencrypt_nginx/tests/parser_test.py @@ -133,11 +133,11 @@ class NginxParserTest(util.NginxTest): self.assertEqual(1, len(re.findall(ssl_re, dump))) server_conf = nparser.abs_path('server.conf') - nparser.add_server_directives(server_conf, - set(['alias', 'another.alias', - 'somename']), + names = set(['alias', 'another.alias', 'somename']) + nparser.add_server_directives(server_conf, names, [['foo', 'bar'], ['ssl_certificate', '/etc/ssl/cert2.pem']]) + nparser.add_server_directives(server_conf, names, [['foo', 'bar']]) self.assertEqual(nparser.parsed[server_conf], [['ssl_certificate', '/etc/ssl/cert2.pem'], ['foo', 'bar'],