Skip to content

Commit bfdb0ff

Browse files
authored
feat(ocf_www): move nginx to 80/443, apache to backend-only
feat(ocf_www): move nginx to 80/443, apache to backend-only
2 parents bfd3e01 + 6adf988 commit bfdb0ff

File tree

10 files changed

+62
-276
lines changed

10 files changed

+62
-276
lines changed

modules/ocf_www/files/build-vhosts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -417,15 +417,18 @@ def main():
417417

418418
web_vhosts = get_vhosts()
419419

420-
# Apache config (existing behavior)
420+
# Exclude web vhosts that overlap with app vhosts (app vhosts
421+
# take priority and are proxied directly to apphost by nginx)
422+
web_only_vhosts = {
423+
domain: conf
424+
for domain, conf in web_vhosts.items()
425+
if domain not in prod_app_vhosts
426+
}
427+
428+
# Apache config (web-only vhosts; apphost vhosts are proxied
429+
# directly by nginx and don't need Apache backend vhosts)
421430
apache_config = build_config(
422-
prod_app_vhosts,
423-
jinja_env.get_template('vhost-web.jinja'),
424-
dev_config=args.dev,
425-
)
426-
apache_config += '\n\n'
427-
apache_config += build_config(
428-
web_vhosts,
431+
web_only_vhosts,
429432
jinja_env.get_template('vhost-web.jinja'),
430433
dev_config=args.dev,
431434
)
@@ -438,7 +441,7 @@ def main():
438441
)
439442
nginx_config += '\n\n'
440443
nginx_config += build_config(
441-
web_vhosts,
444+
web_only_vhosts,
442445
jinja_env.get_template('vhost-web-nginx.jinja'),
443446
dev_config=args.dev,
444447
)

modules/ocf_www/files/vhost-web-nginx.jinja

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# {{vhost.comment}}
2-
# CR-soon oliverni: move to 80/443
32

43
{% if vhost.ssl %}
54
server {
6-
listen 8443 ssl http2;
7-
listen [::]:8443 ssl http2;
5+
listen 443 ssl http2;
6+
listen [::]:443 ssl http2;
87
server_name "{{vhost.fqdn}}";
98

109
ssl_certificate {{vhost.ssl.bundle}};
@@ -32,15 +31,28 @@ server {
3231
{% endif %}
3332
}
3433

34+
{% for ws_location in vhost.websocket_locations %}
35+
location /{{ws_location}} {
36+
proxy_http_version 1.1;
37+
proxy_set_header Upgrade $http_upgrade;
38+
proxy_set_header Connection "Upgrade";
39+
proxy_set_header Host $host;
40+
proxy_set_header X-Forwarded-For $remote_addr;
41+
proxy_set_header X-Forwarded-Proto $scheme;
42+
proxy_set_header X-Real-IP $remote_addr;
43+
proxy_pass http://127.0.0.1:{{backend_port}};
44+
}
45+
{% endfor %}
46+
3547
access_log /var/log/nginx/vhost-access.log vhost;
3648
}
3749
{% endif %}
3850

39-
{% if not vhost.ssl or vhost.is_redirect %}
51+
{% if not vhost.ssl %}
4052
# HTTP (redirect or non-SSL)
4153
server {
42-
listen 8080;
43-
listen [::]:8080;
54+
listen 80;
55+
listen [::]:80;
4456
server_name "{{vhost.fqdn}}";
4557

4658
location /.well-known/ {
@@ -50,12 +62,11 @@ server {
5062
location / {
5163
{% if vhost.is_redirect %}
5264
return {{vhost.redirect_type}} {{vhost.redirect_dest}}$request_uri;
53-
{% elif vhost.ssl %}
54-
return 301 {{vhost.canonical_url}}$request_uri;
5565
{% else %}
5666
proxy_pass http://127.0.0.1:{{backend_port}};
5767
proxy_set_header Host $host;
5868
proxy_set_header X-Forwarded-For $remote_addr;
69+
proxy_set_header X-Forwarded-Proto $scheme;
5970
proxy_set_header X-Real-IP $remote_addr;
6071
{% endif %}
6172
}
Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,17 @@
1+
{% if not vhost.is_redirect %}
12
# {{vhost.comment}}
2-
{% set ports = [vhost.port, backend_port] if vhost.ssl else [vhost.port] %}
3-
{% for port in ports %}
4-
<VirtualHost *:{{port}}>
3+
<VirtualHost 127.0.0.1:{{backend_port}}>
54
ServerName {{vhost.fqdn}}
65
ServerAdmin {{vhost.contact_email}}
76

8-
{% if vhost.ssl and port != backend_port %}
9-
# SSL
10-
SSLEngine on
11-
SSLCertificateFile {{vhost.ssl.bundle}}
12-
SSLCertificateKeyFile {{vhost.ssl.key}}
13-
Protocols h2 http/1.1
14-
{% endif %}
7+
# Trust X-Forwarded-Proto from nginx so %{HTTPS} works in .htaccess
8+
SetEnvIf X-Forwarded-Proto "https" HTTPS=on
159

16-
{% if vhost.is_redirect %}
17-
RewriteEngine on
18-
RewriteCond %{REQUEST_URI} !^/\.well-known/
19-
# 301 redirects are more correct, but get cached forever by dumb browsers.
20-
# Doesn't matter too much for vhosts.
21-
RewriteRule ^(.*)$ {{vhost.redirect_dest}}$1 [L,R={{vhost.redirect_type}}]
22-
{% elif vhost.is_apphost %}
23-
RequestHeader set X-Forwarded-Proto https
24-
ProxyPreserveHost On
25-
SSLProxyEngine on
26-
# Proxy to apphost server
27-
ProxyPass / https://apphost.ocf.berkeley.edu/ upgrade=websocket
28-
{% elif vhost.disabled %}
10+
{% if vhost.disabled %}
2911
# Proxy to the local "unavailable" vhost, which serves up a friendly
3012
# "your website is rekt" page.
3113
RequestHeader set Host unavailable.ocf.berkeley.edu
32-
ProxyPass / http://127.0.0.1/
14+
ProxyPass / http://127.0.0.1:{{backend_port}}/
3315
{% else %}
3416
DocumentRoot {{vhost.docroot}}
3517

@@ -61,4 +43,4 @@
6143

6244
UserDir disabled
6345
</VirtualHost>
64-
{% endfor %}
46+
{% endif %}

modules/ocf_www/manifests/init.pp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,31 @@
1212
# www.ocf.berkeley.edu, which is by far the most complicated domain.
1313
#
1414
# Nginx sits in front of Apache for slowloris protection.
15-
# CR-soon oliverni: swap nginx to 80/443, apache to 127.0.0.1:$backend_port only
15+
# Nginx handles 80/443, Apache only listens on 127.0.0.1:$backend_port.
1616
class ocf_www {
1717
# Port Apache listens on as nginx's backend (plain HTTP on localhost).
1818
# Must match BACKEND_PORT in build-vhosts.
19-
# Phase 2: make this the only Apache port and bind to 127.0.0.1.
2019
$backend_port = 16767
20+
21+
# All Apache vhosts are backend-only (nginx handles 80/443).
22+
Apache::Vhost {
23+
ip => '127.0.0.1',
24+
port => $backend_port,
25+
}
26+
2127
include ocf::acct
2228
include ocf::extrapackages
2329
include ocf::firewall::allow_web
2430
include ocf::limits
2531
include ocf::tmpfs
2632
include ocf::ssl::default
2733

28-
# enables the http2 module
29-
apache::mod { 'http2': }
30-
3134
class { 'ocf::nfs':
3235
cron => false,
3336
web => false,
3437
}
3538

36-
# nginx reverse proxy (test ports for now)
39+
# nginx reverse proxy
3740
include ocf_www::nginx
3841

3942
class {
@@ -66,8 +69,9 @@
6669
backport_on => 'stretch';
6770
}
6871

69-
# Restart apache if any cert changes occur
70-
Class['ocf::ssl::default'] ~> Class['Apache::Service']
72+
# Apache no longer serves SSL directly (nginx handles it), but mod_ssl is
73+
# still needed for SSLProxyEngine (outbound HTTPS to apphost).
74+
include apache::mod::ssl
7175

7276
include ocf_www::lets_encrypt
7377
include ocf_www::logging

modules/ocf_www/manifests/nginx.pp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
# Nginx reverse proxy in front of Apache for slowloris protection.
2-
# CR-soon oliverni: move to 80/443, put apache on 127.0.0.1:$backend_port
32
#
43
# Static vhosts (www, shorturl, etc.) are defined here.
54
# Dynamic user vhosts come from build-vhosts via /etc/nginx/ocf-vhost.conf.
65
class ocf_www::nginx {
76
include ocf::ssl::default
8-
include ocf_www::nginx::firewall
97

10-
# CR-soon oliverni: change listen/ssl ports to 80/443
11-
$http_port = 8080
12-
$ssl_port = 8443
8+
$http_port = 80
9+
$ssl_port = 443
1310

1411
$backend = "http://127.0.0.1:${ocf_www::backend_port}"
1512

modules/ocf_www/manifests/nginx/firewall.pp

Lines changed: 0 additions & 12 deletions
This file was deleted.

modules/ocf_www/manifests/site/ocfweb_redirects.pp

Lines changed: 3 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
# redirect those sites to the appropriate pages on ocfweb.
33
class ocf_www::site::ocfweb_redirects {
44
# accounts
5-
$accounts_canonical_url = $::host_env ? {
6-
'dev' => 'https://dev-accounts.ocf.berkeley.edu/',
7-
'prod' => 'https://accounts.ocf.berkeley.edu/',
8-
}
9-
105
$accounts_options = {
116
servername => 'accounts.ocf.berkeley.edu',
127
serveraliases => ['dev-accounts.ocf.berkeley.edu'],
@@ -21,42 +16,11 @@
2116
],
2217
}
2318

24-
apache::vhost { 'accounts':
25-
* => $accounts_options,
26-
port => 443,
27-
ssl => true,
28-
headers => ['always set Strict-Transport-Security max-age=31536000'],
29-
ssl_key => "/etc/ssl/private/${::fqdn}.key",
30-
ssl_cert => "/etc/ssl/private/${::fqdn}.crt",
31-
ssl_chain => "/etc/ssl/private/${::fqdn}.intermediate",
32-
}
33-
34-
# nginx backend (plain HTTP on localhost)
3519
apache::vhost { 'accounts-backend':
36-
* => $accounts_options,
37-
port => $ocf_www::backend_port,
38-
}
39-
40-
apache::vhost { 'accounts-http-redirect':
41-
servername => 'accounts.ocf.berkeley.edu',
42-
serveraliases => [
43-
'dev-accounts',
44-
'dev-accounts.ocf.berkeley.edu',
45-
'accounts',
46-
],
47-
port => 80,
48-
docroot => '/var/www/html',
49-
50-
redirect_status => 'permanent',
51-
redirect_dest => $accounts_canonical_url;
20+
* => $accounts_options,
5221
}
5322

5423
# wiki
55-
$wiki_canonical_url = $::host_env ? {
56-
'dev' => 'https://dev-wiki.ocf.berkeley.edu/',
57-
'prod' => 'https://wiki.ocf.berkeley.edu/',
58-
}
59-
6024
$wiki_options = {
6125
servername => 'wiki.ocf.berkeley.edu',
6226
serveraliases => ['dev-wiki.ocf.berkeley.edu'],
@@ -67,42 +31,11 @@
6731
],
6832
}
6933

70-
apache::vhost { 'wiki':
71-
* => $wiki_options,
72-
port => 443,
73-
ssl => true,
74-
headers => ['always set Strict-Transport-Security max-age=31536000'],
75-
ssl_key => "/etc/ssl/private/${::fqdn}.key",
76-
ssl_cert => "/etc/ssl/private/${::fqdn}.crt",
77-
ssl_chain => "/etc/ssl/private/${::fqdn}.intermediate",
78-
}
79-
80-
# nginx backend (plain HTTP on localhost)
8134
apache::vhost { 'wiki-backend':
82-
* => $wiki_options,
83-
port => $ocf_www::backend_port,
84-
}
85-
86-
apache::vhost { 'wiki-http-redirect':
87-
servername => 'wiki.ocf.berkeley.edu',
88-
serveraliases => [
89-
'dev-wiki',
90-
'dev-wiki.ocf.berkeley.edu',
91-
'wiki',
92-
],
93-
port => 80,
94-
docroot => '/var/www/html',
95-
96-
redirect_status => 'permanent',
97-
redirect_dest => $wiki_canonical_url;
35+
* => $wiki_options,
9836
}
9937

10038
# hello
101-
$hello_canonical_url = $::host_env ? {
102-
'dev' => 'https://dev-hello.ocf.berkeley.edu/',
103-
'prod' => 'https://hello.ocf.berkeley.edu/',
104-
}
105-
10639
$hello_options = {
10740
servername => 'hello.ocf.berkeley.edu',
10841
serveraliases => [
@@ -118,35 +51,7 @@
11851
],
11952
}
12053

121-
apache::vhost { 'hello':
122-
* => $hello_options,
123-
port => 443,
124-
ssl => true,
125-
headers => ['always set Strict-Transport-Security max-age=31536000'],
126-
ssl_key => "/etc/ssl/private/${::fqdn}.key",
127-
ssl_cert => "/etc/ssl/private/${::fqdn}.crt",
128-
ssl_chain => "/etc/ssl/private/${::fqdn}.intermediate",
129-
}
130-
131-
# nginx backend (plain HTTP on localhost)
13254
apache::vhost { 'hello-backend':
133-
* => $hello_options,
134-
port => $ocf_www::backend_port,
135-
}
136-
137-
apache::vhost { 'hello-http-redirect':
138-
servername => 'hello.ocf.berkeley.edu',
139-
serveraliases => [
140-
'dev-hello',
141-
'dev-hello.ocf.berkeley.edu',
142-
'dev-staff.ocf.berkeley.edu',
143-
'hello',
144-
'staff.ocf.berkeley.edu',
145-
],
146-
port => 80,
147-
docroot => '/var/www/html',
148-
149-
redirect_status => 'permanent',
150-
redirect_dest => $hello_canonical_url;
55+
* => $hello_options,
15156
}
15257
}

0 commit comments

Comments
 (0)