diff --git a/defaults/main.yml b/defaults/main.yml index 19b00ff..9df145d 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -7,8 +7,11 @@ nginx_http_params: tcp_nopush: "on" tcp_nodelay: "on" keepalive_timeout: "65" - access_log: "/var/log/nginx/access.log" - error_log: "/var/log/nginx/error.log" + +nginx_log_dir: "/var/log/nginx" +nginx_access_log_name: "access.log" +nginx_error_log_name: "error.log" +nginx_separete_logs_per_site: False nginx_sites: - server: @@ -16,12 +19,30 @@ nginx_sites: listen: 8080 server_name: localhost root: "/tmp/site1" - location1: {name: /, try_files: "$uri $uri/ /index.html"} - location2: {name: /images/, try_files: "$uri $uri/ /index.html"} + locations: + - name: / + try_files: + - $uri + - $uri/ + - /index.html + - name: /images/ + try_files: + - $uri + - $uri/ + - /index.html - server: file_name: bar listen: 9090 server_name: ansible root: "/tmp/site2" - location1: {name: /, try_files: "$uri $uri/ /index.html"} - location2: {name: /images/, try_files: "$uri $uri/ /index.html"} + locations: + - name: / + try_files: + - $uri + - $uri/ + - /index.html + - name: /images/ + try_files: + - $uri + - $uri/ + - /index.html diff --git a/templates/nginx.conf.j2 b/templates/nginx.conf.j2 index e1d3854..5c404b7 100644 --- a/templates/nginx.conf.j2 +++ b/templates/nginx.conf.j2 @@ -19,6 +19,10 @@ http { include /etc/nginx/mime.types; default_type application/octet-stream; + + access_log {{ nginx_log_dir}}/{{ nginx_access_log_name}}; + error_log {{ nginx_log_dir}}/{{ nginx_error_log_name}}; + {% for k,v in nginx_http_params.iteritems() %} {{ k }} {{ v }}; {% endfor %} diff --git a/templates/site.j2 b/templates/site.j2 index 60f3bf7..5e7e1e7 100644 --- a/templates/site.j2 +++ b/templates/site.j2 @@ -1,17 +1,25 @@ server { +{% if nginx_separete_logs_per_site == True %} + access_log {{ nginx_log_dir}}/{{ item.server.server_name}}-{{ nginx_access_log_name}}; + error_log {{ nginx_log_dir}}/{{ item.server.server_name}}-{{ nginx_error_log_name}}; +{% endif %} + {% for k,v in item.server.iteritems() %} -{% if k.find('location') == -1 and k != 'file_name' %} +{% if k != 'locations' and k != 'file_name' %} {{ k }} {{ v }}; {% endif %} -{% endfor %} +{% endfor %} -{% for k,v in item.server.iteritems() if k.find('location') != -1 %} - location {{ v.name }} { -{% for x,y in v.iteritems() if x != 'name' %} - {{ x }} {{ y }}; +{% for loc in item.server.locations %} + location {{ loc.name }} { +{% for x,y in loc.iteritems() if x != 'name' %} + {% if x == 'try_files' %} + {{ x }} {{ y | join(' ')}}; + {% else %} + {{ x }} {{ y }}; + {% endif %} {% endfor %} } {% endfor %} } -