Created
August 27, 2015 13:00
-
-
Save technolo-g/9107dc6a64448fd82811 to your computer and use it in GitHub Desktop.
Dynamic Routing to your PaaS With NGINX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## -- Begin app1 Cluster -- ## | |
upstream app1 { | |
server 10.32.28.2:32851; | |
} | |
server { | |
listen 80; | |
server_name app1.paas.domain.com; | |
rewrite ^ https://$server_name$request_uri? permanent; | |
} | |
server { | |
listen 443 ssl; | |
ssl_certificate /etc/nginx/ssl/testn.crt; | |
ssl_certificate_key /etc/nginx/ssl/testn.key; | |
proxy_ssl_session_reuse off; | |
server_name app1.paas.domain.com; | |
location / { | |
proxy_set_header X-Real-IP $remote_addr; | |
proxy_set_header Host $host; | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
proxy_connect_timeout 300; | |
proxy_send_timeout 300; | |
client_max_body_size 5M; | |
proxy_read_timeout 300; | |
send_timeout 300; | |
proxy_pass https://app1; | |
} | |
} | |
## -- End app1 Cluster -- ## |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{range $key, $pairs := tree "apps" | byKey}} | |
## -- Begin {{$key}} Cluster -- ## | |
{{range $pair := $pairs}}{{if (regexMatch "main-app" .Key)}} | |
upstream {{$key}} { | |
server {{.Value}}; | |
}{{end}}{{end}} | |
server { | |
listen 80; | |
{{range $pair := $pairs}}{{if (regexMatch "metadata-.*" .Key)}}server_name {{(replaceAll "metadata-" "" .Key) | toLower}}.{{env "PAAS_DOMAIN"}}; | |
rewrite ^ https://$server_name$request_uri? permanent;{{end}}{{end}} | |
} | |
server { | |
listen 443 ssl; | |
{{range $pair := $pairs}}{{if (regexMatch "metadata-.*" .Key)}}ssl_certificate /etc/nginx/ssl/apps.crt; | |
ssl_certificate_key /etc/nginx/ssl/apps.key; | |
proxy_ssl_session_reuse off; | |
server_name {{(replaceAll "metadata-" "" .Key) | toLower}}.{{env "PAAS_DOMAIN"}}; | |
{{end}}{{end}} | |
location / { | |
{{range $pair := $pairs}}{{if (regexMatch "main-app" .Key)}} | |
proxy_set_header X-Real-IP $remote_addr; | |
proxy_set_header Host $host; | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
proxy_connect_timeout 300; | |
proxy_send_timeout 300; | |
client_max_body_size 5M; | |
proxy_read_timeout 300; | |
send_timeout 300; | |
proxy_pass https://{{$key}}; | |
{{end}}{{end}} | |
} | |
} | |
## -- End {{$key}} Cluster -- ## | |
{{end}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -ex | |
# Enable logging | |
export CONSUL_TEMPLATE_LOG="${CONSUL_TEMPLATE_LOG:-warn}" | |
# Write the initial (valid) template | |
/bin/consul-template \ | |
-consul ${CONSUL_HOST}:${CONSUL_PORT} \ | |
-log-level "${CONSUL_TEMPLATE_LOG}" \ | |
-template "/etc/nginx/consul.conf.ctmpl:/etc/nginx/conf.d/consul.conf:/usr/sbin/nginx -s reload || true" \ | |
-once 2>&1 |tee /var/log/consul-template-runonce.log | |
# Start the consul-template daemon | |
/bin/consul-template \ | |
-consul ${CONSUL_HOST}:${CONSUL_PORT} \ | |
-log-level "${CONSUL_TEMPLATE_LOG}" \ | |
-template "/etc/nginx/consul.conf.ctmpl:/etc/nginx/conf.d/consul.conf:/usr/sbin/nginx -s reload || true" \ | |
2>&1 |tee /var/log/consul-template.log & | |
# Start Nginx in foreground | |
/usr/sbin/nginx -c /etc/nginx/nginx.conf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment