Presenting upstream feature for nginx
With this feature you will be able to set up nginx load balancer
Change-Id: Ib6628828a7c197b9fff4409e109f45324ccc9e34
diff --git a/README.rst b/README.rst
index 222dc69..f8c75b5 100644
--- a/README.rst
+++ b/README.rst
@@ -52,6 +52,31 @@
name: gitlab.domain.com
port: 80
+Simple load balancer
+
+.. code-block:: yaml
+
+ nginx:
+ server:
+ upstream:
+ horizon-upstream:
+ backend1:
+ address: 10.10.10.113
+ port: 8078
+ opts: weight=3
+ backend2:
+ address: 10.10.10.114
+ site:
+ nginx_proxy_openstack_web:
+ enabled: true
+ type: nginx_proxy
+ name: openstack_web
+ proxy:
+ upstream_proxy_pass: http://horizon-upstream
+ host:
+ name: 192.168.0.1
+ port: 31337
+
Static site with access policy
.. code-block:: yaml
diff --git a/nginx/files/nginx.conf b/nginx/files/nginx.conf
index 130e664..703fc2e 100644
--- a/nginx/files/nginx.conf
+++ b/nginx/files/nginx.conf
@@ -67,6 +67,11 @@
#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;
+{% if server.upstream is defined %}
+ ## Upstreams
+ include /etc/nginx/upstream.conf;
+{% endif %}
+
##
# Virtual Host Configs
##
diff --git a/nginx/files/proxy.conf b/nginx/files/proxy.conf
index 1b56ef3..845d781 100644
--- a/nginx/files/proxy.conf
+++ b/nginx/files/proxy.conf
@@ -16,7 +16,11 @@
{%- include "nginx/files/_auth.conf" %}
location / {
+ {%- if site.proxy.upstream_proxy_pass is defined %}
+ proxy_pass {{ site.proxy.upstream_proxy_pass }};
+ {%- else %}
proxy_pass {{ site.proxy.protocol }}://{{ site.proxy.host }}:{{ site.proxy.port }};
+ {%- endif %}
{%- include "nginx/files/_access_policy.conf" %}
diff --git a/nginx/files/upstream.conf b/nginx/files/upstream.conf
new file mode 100644
index 0000000..15c42c9
--- /dev/null
+++ b/nginx/files/upstream.conf
@@ -0,0 +1,12 @@
+{%- from "nginx/map.jinja" import server with context %}
+
+{%- for upstream_name, backend in server.upstream.iteritems() %}
+upstream {{ upstream_name }} {
+
+{%- for backend_name, backend_item in backend.iteritems() %}
+ server {{ backend_item.get('address', '127.0.0.1') }}:{{ backend_item.get('port', '31337') }} {{ backend_item.get('opts', '') }};
+{%- endfor %}
+
+}
+
+{%- endfor %}
diff --git a/nginx/server.sls b/nginx/server.sls
index 1ca2788..18e0dd9 100644
--- a/nginx/server.sls
+++ b/nginx/server.sls
@@ -45,6 +45,17 @@
- service: nginx_service
{%- endif %}
+{%- if server.upstream is defined %}
+/etc/nginx/upstream.conf:
+ file.managed:
+ - source: salt://nginx/files/upstream.conf
+ - template: jinja
+ - require:
+ - pkg: nginx_packages
+ - watch_in:
+ - service: nginx_service
+{%- endif %}
+
nginx_service:
service.running:
- name: {{ server.service }}
diff --git a/tests/pillar/proxy.sls b/tests/pillar/proxy.sls
index 934c4e6..959c9c9 100644
--- a/tests/pillar/proxy.sls
+++ b/tests/pillar/proxy.sls
@@ -25,6 +25,14 @@
bind:
address: 127.0.0.1
protocol: tcp
+ upstream:
+ horizon-upstream:
+ backend1:
+ address: 10.10.10.113
+ port: 8078
+ opts: weight=3
+ backend2:
+ address: 10.10.10.114
site:
nginx_proxy_site01:
enabled: true
@@ -37,4 +45,12 @@
host:
name: cloudlab.domain.com
port: 80
-
+ nginx_proxy_site02:
+ enabled: true
+ type: nginx_proxy
+ name: site02
+ proxy:
+ upstream_proxy_pass: http://horizon-upstream
+ host:
+ name: cloudlab.domain.com
+ port: 31337