Skip to content
This repository was archived by the owner on Jul 12, 2025. It is now read-only.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

153 Commits

Repository files navigation

Reverse proxy

Installs and configure nginx as reverse proxy. Redirects all http requests to https, certificates are automatically issued by Let's Encrypt.

If necessary, certificates are automatically obtained or renewed, any time ansible-playbook is executed. Support for Unix-PAM authentication, by setting auth to true at target server. Ability to restrict target domains by ip ranges and addresses. Options to copy obtained certificates to target servers, when requested by them.

When multiple names are given for a served_domain, only the first name will proxy, while the other names will redirect to the first name.

Requirements

A Debian based distribution with certbot available in current apt sources. Correctly configured DNS server.

Role Variables

Primary

OptionTypeDefaultDescriptionRequired
proxy_domainslist of dictsList of all target serversY
default_urlstringhttps://github.com/stuvusIT/reverse_proxyUrl to redirect to if no target with requested domain is configuredN
letsencrypt_emailstringE-Mail address to use to request certificatesY
default_cert_modestring0400Default file access mode on certificates at target serversN
default_cert_groupstringrootDefault owner group for certificates at target serversN
default_cert_ownerstringrootDefault owner user for certificates at target serversN
default_cryptobooleanTrueUse https as default to forward trafficN
reverse_proxy_default_proxy_ssl_verifybooleanFalseWhether the SSL certificate of the proxied host should be verified by defaultN
domain_suffixeslist of strings['']Domain suffixes to append to every not full qualified domain nameN
domain_prefixeslist of strings['']Domain prefixes to append to every not full qualified domain nameN
letsencrypt_stagingbooleanFalseUse letsencrypt staging serversN
client_max_body_sizestring1mSet the maximum upload size at the http contextN
hsts_max_ageinteger300Strict Transport Security (HSTS) time of lifeN (but recommended)
reverse_proxy_use_dhparamboolTrueUse and generate Diffie-Hellman parametersN
reverse_proxy_dhparam_sizeinteger2048Size of Diffie-Hellman parameterN
reverse_proxy_dhparam_pathstring/etc/ssl/dhparam.pemPath to store the Diffie-Hellman parametersN
reverse_proxy_ssl_session_timeoutstring1dnginx ssl_session_timeout optionN
reverse_proxy_ssl_session_cachestringshared:SSL:50mnginx ssl_session_cache optionN
reverse_proxy_ssl_session_ticketsbooleanFalsenginx ssl_session_tickets optionN
reverse_proxy_ssl_protocolslist of stringsTLSv1 TLSv1.1 TLSv1.2nginx ssl_protocols optionN
reverse_proxy_ssl_cipherslist of stringssee defaults/main.ymlnginx ssl_ciphers optionN
reverse_proxy_ssl_prefer_server_ciphersbooleanTruenginx ssl_prefer_server_ciphers optionN
reverse_proxy_ssl_staplingbooleanTrueEnable OCSP StaplingN
reverse_proxy_ssl_trusted_certificatestringnginx ssl_trusted_certificate option, path to the intermediate certificate of your CAN
reverse_proxy_redirect_to_first_domainbooleanTrueRedirect to first domain from every served_domains[].domains list by defaultN
reverse_proxy_redirect_to_first_domain_codeinteger302Specify default HTTP redirect code for redirects to first(default) domainN
reverse_proxy_https_redirect_codeinteger302HTTP status code used to direct users to the https version of a pageN
reverse_proxy_error_logstring/var/log/nginx/error.logLocation of the error logN
reverse_proxy_redirect_codeinteger302Default HTTP status code used for custom domain redirectsN
reverse_proxy_upstreamsdict of strings{}Name-Content dict of upstreams to add to nginx.confN
reverse_proxy_cache_pathsdict of strings{}Path-Options dict of cache paths to add to nginx.conf (see example below)N
reverse_proxy_additional_http_locationsdict of strings{}Match-Content dict of additional location blocks to add to the server on port 80N
reverse_proxy_keep_until_expiringbooleanTrueIf the requested certificate matches an existing certificate, always keep the existing one until it is due for renewalN
reverse_proxy_force_renewbooleanFalseForce certificate renew, regardless of whether it is near expiryN
reverse_proxy_additional_configstringAdditional config to be written into the nginx http section once.N

proxy_domains

OptionTypeDefaultDescriptionRequired
target_descriptionstringA short description of the target serverY
target_hoststringAnsible host name (will be used to copy requested certificates)Y
target_ipstringIP address of target server (will be used as target address to redirect to)Y
keep_until_expiringbooleanIf the requested certificate matches an existing certificate, always keep the existing one until it is due for renewalN
force_renewbooleanForce certificate renew, regardless of whether it is near expiryN
served_domainslist of dictsList of all domain lists served by this target serverY

served_domains

OptionTypeDefaultDescriptionRequired
no_proxybooleanfalseDon't actually configure anything target-related (use extra_* variables from below)N
portintegerTarget port to redirect toN
cryptoboolean{{ default_crypto }}Use https to forward trafficN
authbooleanfalserestrict access to system usersN
reverse_proxy_ssl_verifyboolean{{ reverse_proxy_default_proxy_ssl_verify }}Whether the SSL certificate of the proxied host should be verifiedN
domainslist of stringsA list of domains to proxy (see below for more information)¹Y
redirect_to_first_domainboolean{{ reverse_proxy_redirect_to_first_domain }}Redirect to first domain from every served_domains[].domains listN
redirect_to_first_domain_codeinteger{{ reverse_proxy_redirect_to_first_domain_code }}Specify HTTP redirect code for redirects to first(default) domainN
access_controllist of dictsA list of dicts to restrict access to given set of ip rangesN
fullchain_pathstringDestination path² for fullchain.pem at target_hostN
cert_pathstringDestination path² for cert.pem at target_hostN
chain_pathstringDestination path² for chain.pem at target_hostN
privkey_pathstringDestination path² for privkey.pem at target_hostN
fullchain_modestring{{ default_cert_mode }}File access mode for fullchain.pwm at target_hostN
cert_modestring{{ default_cert_mode }}File access mode for cert.pwm at target_hostN
chain_modestring{{ default_cert_mode }}File access mode for chain.pwm at target_hostN
privkey_modestring{{ default_cert_mode }}File access mode for privkey.pwm at target_hostN
fullchain_groupstring{{ default_cert_group }}Owner group of fullchain.pwm at target_hostN
cert_groupstring{{ default_cert_group }}Owner group of cert.pwm at target_hostN
chain_groupstring{{ default_cert_group }}Owner group of chain.pwm at target_hostN
privkey_groupstring{{ default_cert_group }}Owner group of privkey.pwm at target_hostN
fullchain_ownerstring{{ default_cert_owner }}Owner of fullchain.pwm at target_hostN
cert_ownerstring{{ default_cert_owner }}Owner of cert.pwm at target_hostN
chain_ownerstring{{ default_cert_owner }}Owner of chain.pwm at target_hostN
privkey_ownerstring{{ default_cert_owner }}Owner of privkey.pwm at target_hostN
client_max_body_sizestringSet the maximum upload size at server contextN
extra_server_configstringAdditional configuration items to add to the server block before location blocksN
extra_location_configstringAdditional configuration items to add to the default location block (location /)N
extra_locationslist of key value dicts[]Add custom locations to this server block, the key should be a location string, the value defines the location bodyN
redirectstringInstead of proxying the request, redirect to this URL. The request URI is automatically appended.N
redirect_codeinteger{{ reverse_proxy_redirect_code }}HTTP status code used to redirect the user to the URL specified by redirectN
enable_http2_proxybooleantrueEnable http2 for this hostN
use_keepalivebooleanfalseEnable keepalive for this hostN
proxy_keepaliveinteger1024Number of connections to keepalive between the proxy and the hostN
keep_until_expiringbooleanIf the requested certificate matches an existing certificate, always keep the existing one until it is due for renewalN
force_renewbooleanForce certificate renew, regardless of whether it is near expiryN

¹ Can be either a fully qualified domain name(with following dot ex. www.example.com.) or a short internal domain(will be expanded by domain_suffixes and domain_prefixes ex. wiki or static.media)

² Path must point to a file in a already existing directory. The file will be either overwritten or created.

access_control

OptionTypeDefaultDescriptionRequired
allowstringIP address or subnet to allowN
denystringIP address or subnet to denyN

These dicts are evaluated in given order, so a complete subnet can be allowed with the exception of a given ip, see: nginx doku for future information.

Example Playbook

Vars:

letsencrypt_email: hostmaster@example.comdefault_url: https://stuvus.uni-stuttgart.dedomain_suffixes:
- "example.com"reverse_proxy_cache_paths:
/var/mycache:
- keys_zone=my_cache:50m
- max_size=500gproxy_domains:
- target_description: music playertarget_host: mpd01target_ip: 172.27.10.66served_domains:
- access_control:
- allow: 172.27.0.0/16
- deny: allport: 6680https: falsedomains:
- mpd
- target_description: DokuWikitarget_host: validatortarget_ip: 172.27.10.101served_domains:
- auth: truedomains:
- wiki
- www.wiki
- wiki.wiki.de.

Result:

This example playbook proxies as follows:

DomainProxies toRedirects toRestrictions
mpd.example.comhttp://172.27.10.66:6680-allow: 172.27.0.0/16, deny all other
wiki.example.comhttps://172.27.10.101-only system users
www.wiki.example.comhttps://172.27.10.101https://wiki.example.comonly system users
wiki.wiki.dehttps://172.27.10.101https://wiki.example.comonly system users

License

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Author Information

About

Configure a nginx reverse proxy with Ansible

Topics

Resources

Stars

5 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages