Skip to content

Instantly share code, notes, and snippets.

@tomeduarte
Last active December 21, 2015 17:28
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomeduarte/6340205 to your computer and use it in GitHub Desktop.
Save tomeduarte/6340205 to your computer and use it in GitHub Desktop.
Sample (and simple) NGINX configuration as a load-balancer, using SaltStack's peer communication to retrieve list of backend servers.
# /srv/salt/shared/nginx/files/sites-available/default.lb
upstream app_nodes {
# Build list of backend servers
# using peer communication as the mine is cached and slow to pick up new hosts
{% for server,ips in
salt['publish.publish'](
'G@roles:appnode',
'network.ip_addrs', expr_form='compound').items() -%}
# host: {{ server }}
{% for ip in ips -%}
server {{ ip }}:80 ;
{% endfor -%}
{% endfor -%}
}
server {
listen 80 default_server;
server_name {{ realm }}.example.com www.{{ realm }}.example.com;
error_log /var/log/nginx/default/error.log;
location / {
proxy_pass http://app_nodes;
}
}
# /srv/salt/shared/nginx/init.sls
/etc/nginx/sites-available/default:
file.managed:
- source: salt://shared/nginx/files/sites-available/default.lb
- template: jinja
- require:
- pkg: shared.nginx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment