1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
<%-
##
## An apache config for static websites.
##
def location_directory(name, location)
if ['amber', 'rack'].include?(location['format'])
File.join(@base_dir, name, 'public')
else
File.join(@base_dir, name)
end
end
#
# document root
#
@document_root = begin
root = '/var/www'
@locations && @locations.each do |name, location|
root = location_directory(name, location) if location['path'] == '/'
end
root.gsub(%r{^/|/$}, '')
end
#
# provider.json
#
# if the domain is a bootstrap domain, we need to expose
# a /provider.json file.
#
bootstrap_domain = scope.lookupvar('site_static::bootstrap_domain')
bootstrap_client = scope.lookupvar('site_static::bootstrap_client')
if ([@aliases]+[@domain]).flatten.include?(bootstrap_domain)
provider_json = \
%(
Alias /provider.json /srv/static/public/provider.json
<Location /provider.json>
Header set X-Minimum-Client-Version #{bootstrap_client['min']}
</Location>
)
else
provider_json = ""
end
#
# locations
#
locations = ""
@locations && @locations.each do |name, location|
location_path = location['path'].gsub(%r{^/|/$}, '')
directory = location_directory(name, location)
local_vars = {'location_path'=>location_path, 'directory'=>directory, 'location'=>location, 'name'=>name}
template_path = File.join(File.dirname(__FILE__), location['format']) + '.erb'
break unless File.exists?(template_path)
locations += \
%(
#
# #{name} (#{location['format']})
#
#{scope.function_templatewlv([template_path, local_vars])}
)
end
#
# allow custom apache config
#
custom_apache_config = if @apache_config
@apache_config.gsub(':percent:','%')
end
-%>
<Directory "/srv/static/public/">
Require all granted
</Directory>
<%- if @tor -%>
##
## Tor
##
<VirtualHost 127.0.0.1:80>
ServerName <%= @tor_domain %>
ServerAlias www.<%= @tor_domain %>
<IfModule mod_headers.c>
Header set X-Frame-Options "deny"
Header always unset X-Powered-By
Header always unset X-Runtime
</IfModule>
DocumentRoot "/<%= @document_root %>/"
AccessFileName .htaccess
<%= provider_json %>
<%= custom_apache_config %>
<%= locations %>
</VirtualHost>
<%- end -%>
##
## HTTP
##
<VirtualHost *:80>
ServerName <%= @domain %>
ServerAlias www.<%= @domain %>
<%- @aliases && @aliases.each do |domain_alias| -%>
ServerAlias <%= domain_alias %>
<%- end -%>
<%- if @tls_only -%>
RewriteEngine On
RewriteRule ^.*$ https://<%= @domain -%>%{REQUEST_URI} [R=permanent,L]
<%- else -%>
<%= provider_json %>
<%= custom_apache_config %>
<%= locations %>
<%- end -%>
</VirtualHost>
##
## HTTPS
##
<VirtualHost *:443>
ServerName <%= @domain %>
ServerAlias www.<%= @domain %>
<%- @aliases && @aliases.each do |domain_alias| -%>
ServerAlias <%= domain_alias %>
<%- end -%>
#RewriteLog "/var/log/apache2/rewrite.log"
#RewriteLogLevel 3
Include include.d/ssl_common.inc
<IfModule mod_headers.c>
<%- if @tls_only -%>
Header always set Strict-Transport-Security: "max-age=15768000;includeSubdomains"
<%- end -%>
Header set X-Frame-Options "deny"
Header always unset X-Powered-By
Header always unset X-Runtime
</IfModule>
SSLCertificateKeyFile /etc/x509/keys/<%= @domain %>.key
SSLCertificateFile /etc/x509/certs/<%= @domain %>.crt
RequestHeader set X_FORWARDED_PROTO 'https'
DocumentRoot "/<%= @document_root %>/"
AccessFileName .htaccess
<%= provider_json %>
<%= custom_apache_config %>
<%= locations %>
</VirtualHost>
|