Saltstack Official Apache Formula
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
6.2KB

  1. {# Define default values here so the template below can just focus on layout #}
  2. {% from "apache/map.jinja" import apache with context %}
  3. {% set sitename = site.get('ServerName', id) %}
  4. {% set vals = {
  5. 'interfaces': site.get('interface', '*').split(),
  6. 'port': site.get('port', '80'),
  7. 'ServerName': sitename,
  8. 'ServerAlias': site.get('ServerAlias', 'www.{0}'.format(sitename)),
  9. 'ServerAdmin': site.get('ServerAdmin', 'webmaster@{0}'.format(sitename)),
  10. 'UseCanonicalName': site.get('UseCanonicalName'),
  11. 'LogLevel': site.get('LogLevel', 'warn'),
  12. 'ErrorLog': site.get('ErrorLog', '{0}/{1}-error.log'.format(map.logdir, sitename)),
  13. 'LogFormat': site.get('LogFormat', '"%a %l %u %t \\"%r\\" %>s %O \\"%{Referer}i\\" \\"%{User-Agent}i\\""'),
  14. 'LogFormat': site.get('LogFormat', '"%a %l %u %t \\"%r\\" %>s %O \\"%{Referer}i\\" \\"%{User-Agent}i\\"" {0}'.format(sitename)),
  15. 'CustomLog': site.get('CustomLog', '{0}/{1}-access.log {1}'.format(map.logdir, sitename)),
  16. 'ProxyRequests': site.get('ProxyRequests', 'Off'),
  17. 'ProxyPreserveHost': site.get('ProxyPreserveHost', 'On'),
  18. 'ProxyRoute': site.get('ProxyRoute', {}),
  19. 'Location': {
  20. 'Order': 'allow,deny',
  21. 'Allow': 'from all',
  22. 'Require': 'all granted',
  23. },
  24. 'LocationMatch': {
  25. 'Order': 'allow,deny',
  26. 'Allow': 'from all',
  27. 'Require': 'all granted',
  28. },
  29. } %}
  30. <VirtualHost {%- for intf in vals.interfaces %} {{intf}}:{{ vals.port }}{% endfor -%}>
  31. ServerName {{ vals.ServerName }}
  32. {% if site.get('ServerAlias') != False %}ServerAlias {{ vals.ServerAlias }}{% endif %}
  33. {% if site.get('ServerAdmin') != False %}ServerAdmin {{ vals.ServerAdmin }}{% endif %}
  34. {% if site.get('UseCanonicalName') %}UseCanonicalName {{ vals.UseCanonicalName }}{% endif %}
  35. {% if site.get('LogLevel') != False %}LogLevel {{ vals.LogLevel }}{% endif %}
  36. {% if site.get('ErrorLog') != False %}ErrorLog {{ vals.ErrorLog }}{% endif %}
  37. {% if site.get('CustomLog') != False %}CustomLog {{ vals.CustomLog }} {{ vals.LogFormat }}{% endif %}
  38. {% if site.get('SSLCertificateFile') %}SSLEngine on
  39. SSLCertificateFile {{ site.SSLCertificateFile }}
  40. {% if site.get('SSLCertificateKeyFile') %}SSLCertificateKeyFile {{ site.SSLCertificateKeyFile }}{% endif %}
  41. {% if site.get('SSLCertificateChainFile') %}SSLCertificateChainFile {{ site.SSLCertificateChainFile}}{% endif %}
  42. {% endif %}
  43. {% if site.get('Rewrite') %}RewriteEngine on
  44. {{ site.Rewrite }}
  45. {% endif %}
  46. {% if site.get('SSLProxyEngine') %}SSLProxyEngine {{ site.SSLProxyEngine }}{% endif %}
  47. ProxyRequests {{ vals.ProxyRequests }}
  48. ProxyPreserveHost {{ vals.ProxyPreserveHost }}
  49. {% if site.get('ProxyErrorOverride') %}ProxyErrorOverride {{ site.ProxyErrorOverride }} {% endif %}
  50. {% if site.get('ProxyErrorDir') %}ProxyPass /{{ site.ProxyErrorDir }}/ ! {% endif %}
  51. {% for proxy, proxyargs in vals.ProxyRoute|dictsort|reverse %}
  52. {% set proxyvals = {
  53. 'ProxyPassSource': proxyargs.get('ProxyPassSource', '/'),
  54. 'ProxyPassTarget': proxyargs.get('ProxyPassTarget', 'https://{0}'.format(sitename)),
  55. 'ProxyPassTargetOptions': proxyargs.get('ProxyPassTargetOptions', ''),
  56. 'ProxyPassReverseSource': proxyargs.get('ProxyPassReverseSource', '/'),
  57. 'ProxyPassReverseTarget': proxyargs.get('ProxyPassReverseTarget', proxyargs.get('ProxyPassTarget', 'https://{0}'.format(sitename))),
  58. } %}
  59. ######### {{proxy}} #########
  60. ProxyPass {{ proxyvals.ProxyPassSource }} {{ proxyvals.ProxyPassTarget }} {{ proxyvals.ProxyPassTargetOptions }}
  61. ProxyPassReverse {{ proxyvals.ProxyPassReverseSource }} {{ proxyvals.ProxyPassReverseTarget }}
  62. {% endfor %}
  63. {%- for path, loc in site.get('Location', {}).items() %}
  64. {%- set lvals = {
  65. 'Order': loc.get('Order', vals.Location.Order),
  66. 'Allow': loc.get('Allow', vals.Location.Allow),
  67. 'Require': loc.get('Require', vals.Location.Require),
  68. 'Dav': loc.get('Dav', False),
  69. } %}
  70. <Location "{{ path }}">
  71. {% if apache.version == '2.4' %}
  72. {%- if lvals.get('Require') != False %}Require {{lvals.Require}}{% endif %}
  73. {% else %}
  74. {%- if lvals.get('Order') != False %}Order {{ lvals.Order }}{% endif %}
  75. {%- if lvals.get('Allow') != False %}Allow {{ lvals.Allow }}{% endif %}
  76. {% endif %}
  77. {%- if loc.get('Formula_Append') %} {{ loc.Formula_Append|indent(8) }} {% endif %}
  78. </Location>
  79. {% endfor %}
  80. {%- for regpath, locmat in site.get('LocationMatch', {}).items() %}
  81. {%- set lmvals = {
  82. 'Order': locmat.get('Order', vals.LocationMatch.Order),
  83. 'Allow': locmat.get('Allow', vals.LocationMatch.Allow),
  84. 'Require': locmat.get('Require', vals.LocationMatch.Require),
  85. 'Dav': locmat.get('Dav', False),
  86. } %}
  87. <LocationMatch "{{ regpath }}">
  88. {% if apache.version == '2.4' %}
  89. {%- if lmvals.get('Require') != False %}Require {{lmvals.Require}}{% endif %}
  90. {% else %}
  91. {%- if lmvals.get('Order') != False %}Order {{ lmvals.Order }}{% endif %}
  92. {%- if lmvals.get('Allow') != False %}Allow {{ lmvals.Allow }}{% endif %}
  93. {% endif %}
  94. {%- if locmat.get('Formula_Append') %} {{ locmat.Formula_Append|indent(8) }} {% endif %}
  95. </LocationMatch>
  96. {% endfor %}
  97. {%- for proxypath, prox in site.get('Proxy_control', {}).items() %}
  98. {%- set proxvals = {
  99. 'AllowAll': prox.get('AllowAll', vals.AllowAll),
  100. 'AllowCountry': prox.get('AllowCountry', vals.AllowCountry),
  101. 'AllowIP': prox.get('AllowIP', vals.AllowIP),
  102. } %}
  103. <Proxy "{{ proxypath }}">
  104. {%- if proxvals.get('AllowAll') != False %}
  105. Require all granted
  106. {%- else %}
  107. {% if proxvals.get('AllowCountry') != False %}{% set country_list = proxvals.get('AllowCountry', {}) %}GeoIPEnable On
  108. {% for every_country in country_list %}SetEnvIf GEOIP_COUNTRY_CODE {{ every_country }} AllowCountry
  109. {% endfor %}Require env AllowCountry {% endif %}
  110. {% if proxvals.get('AllowIP') is defined %} {% set ip_list = proxvals.get('AllowIP', {}) %}
  111. Require ip {% for every_ip in ip_list %}{{ every_ip }} {% endfor %} {% endif %}
  112. {%- endif %}
  113. </Proxy>
  114. {%- endfor %}
  115. {% if site.get('Formula_Append') %}
  116. {{ site.Formula_Append|indent(4) }}
  117. {% endif %}
  118. </VirtualHost>