Skip to content

Commit 9f011b3

Browse files
authored
Merge branch 'nginx_default_vhost' of 'https://github.com/zhquan/bap-deployment-toolkit'
Merges #119 Closes #119
2 parents 7a19308 + d18ccad commit 9f011b3

File tree

8 files changed

+121
-31
lines changed

8 files changed

+121
-31
lines changed

ansible/roles/nginx/defaults/main.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,9 @@ gcloud_apt_deb_ubuntu_repository: >-
7373
7474
docker_network_name: bap_network
7575
docker_log_max_size: 500m
76+
77+
# Uncomment to add NGINX default configuration
78+
#nginx_default_host: default.example.com
79+
80+
# NGINX ssl_ciphers list from Mozilla https://wiki.mozilla.org/Security/Server_Side_TLS
81+
nginx_ssl_ciphers: "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305:TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256"

ansible/roles/nginx/files/default.conf

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

ansible/roles/nginx/tasks/main.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@
6565
loop_var: instance
6666
when: custom_cert is undefined and instance.nginx is defined
6767

68+
- name: "Configure virtualhost (certbot with acme-challenge) for default host"
69+
template:
70+
src: vhost_port_80.j2
71+
dest: "{{ nginx_virtualhosts_workdir }}/default.conf"
72+
backup: true
73+
when: custom_cert is undefined and nginx_default_host is defined
74+
6875
- name: "Create a docker network: {{ docker_network_name }}"
6976
docker_network:
7077
name: "{{ docker_network_name }}"
@@ -99,7 +106,11 @@
99106
with_items:
100107
- bitergia-logo.png
101108
- custom_404.html
102-
- default.conf
109+
when: nginx_default_host is defined
110+
111+
- name: Confgure virtualhost for default host
112+
include_tasks: virtualhost_default.yml
113+
when: nginx_default_host is defined
103114

104115
- name: Start NGINX container with virtualhost(s)
105116
docker_container:
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
3+
- name: "Get {{ nginx_default_host }} certificate info"
4+
shell: openssl x509 -in "{{ nginx_letsencrypt_configdir }}/live/{{ nginx_default_host }}/cert.pem" -noout -dates | awk -F= '/notAfter=/ {print $2}'
5+
register: cert_info
6+
when: custom_cert is undefined
7+
8+
- name: "Show {{ nginx_default_host }} certificate info"
9+
debug:
10+
msg: "{{ cert_info }}"
11+
when: custom_cert is undefined
12+
13+
- name: "Create certificate for {{ nginx_default_host }}"
14+
command: "certbot -n certonly --webroot -w {{ nginx_letsencrypt_webroot_path }} -d {{ nginx_default_host }} --work-dir {{ nginx_letsencrypt_workdir }} --logs-dir {{ nginx_letsencrypt_logsdir }} --config-dir {{ nginx_letsencrypt_configdir }}"
15+
args:
16+
creates: "{{ nginx_letsencrypt_workdir }}/live/{{ nginx_default_host }}"
17+
ignore_errors: true
18+
when: custom_cert is undefined and cert_info.stderr
19+
20+
- name: "Copy custom SSL certificates"
21+
run_once: true
22+
copy:
23+
src: "{{ item.src }}"
24+
dest: "{{ item.dest }}"
25+
owner: "{{ item.owner }}"
26+
group: "{{ item.group }}"
27+
mode: "{{ item.mode }}"
28+
loop:
29+
- src: "{{ custom_cert.cert if custom_cert is defined else 'cert.crt' }}"
30+
dest: "{{ nginx_certs_dir }}/custom.crt"
31+
owner: '1000'
32+
group: '1000'
33+
mode: '0644'
34+
- src: "{{ custom_cert.key if custom_cert is defined else 'cert.key' }}"
35+
dest: "{{ nginx_certs_dir }}/custom.key"
36+
owner: '1000'
37+
group: '1000'
38+
mode: '0600'
39+
loop_control:
40+
label: "{{ item.dest }}"
41+
when: custom_cert is defined
42+
43+
- name: "Create virtualhost configuration for default.conf"
44+
template:
45+
src: default.conf.j2
46+
dest: "{{ nginx_virtualhosts_workdir }}/default.conf"
47+
backup: true
48+
when: nginx_default_host is defined
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{% include 'vhost_port_80.j2' %}
2+
3+
server {
4+
listen 443 ssl default_server;
5+
http2 on;
6+
7+
server_name {{ nginx_default_host}};
8+
9+
{% if custom_cert is defined %}
10+
ssl_certificate /etc/ssl/certs/custom.crt;
11+
ssl_certificate_key /etc/ssl/certs/custom.key;
12+
{% else %}
13+
ssl_certificate /etc/ssl/certbot_certs/live/{{ nginx_default_host }}/fullchain.pem;
14+
ssl_certificate_key /etc/ssl/certbot_certs/live/{{ nginx_default_host }}/privkey.pem;
15+
{% endif %}
16+
ssl_protocols TLSv1.3 TLSv1.2;
17+
ssl_ecdh_curve X25519:prime256v1:secp384r1;
18+
ssl_ciphers {{ nginx_ssl_ciphers }};
19+
ssl_prefer_server_ciphers off;
20+
ssl_stapling on;
21+
ssl_stapling_verify on;
22+
ssl_session_cache shared:SSL:10m;
23+
ssl_session_timeout 10m;
24+
25+
error_page 404 /custom_404.html;
26+
27+
location = /custom_404.html {
28+
root /etc/nginx/conf.d;
29+
internal;
30+
}
31+
32+
location /bitergia-logo.png {
33+
root /etc/nginx/conf.d;
34+
}
35+
}

ansible/roles/nginx/templates/vhost.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ server {
2020
{% endif %}
2121
ssl_protocols TLSv1.3 TLSv1.2;
2222
ssl_ecdh_curve X25519:prime256v1:secp384r1;
23-
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305:TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256;
23+
ssl_ciphers {{ nginx_ssl_ciphers }};
2424
ssl_prefer_server_ciphers off;
2525
ssl_stapling on;
2626
ssl_stapling_verify on;

ansible/roles/nginx/templates/vhost_port_80.j2

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
server {
2+
{% if instance is undefined and nginx_default_host is defined %}
3+
listen 80 default_server;
4+
server_name {{ nginx_default_host}};
5+
{% else %}
26
listen 80;
3-
47
server_name {{ instance.nginx.fqdn }};
8+
{% endif %}
59

610
{% if custom_cert is undefined %}
711
location /.well-known/acme-challenge/ {
@@ -12,4 +16,13 @@ server {
1216
location / {
1317
return 301 https://$server_name$request_uri;
1418
}
19+
20+
{% if instance is undefined and nginx_default_host is defined %}
21+
error_page 404 /custom_404.html;
22+
23+
location = /custom_404.html {
24+
root /etc/nginx/conf.d;
25+
internal;
26+
}
27+
{% endif %}
1528
}

docs/deployment_and_config.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,12 @@ all:
155155
# Mordred Settings
156156
mordred_setups_repo_url: <repo_mordred_config.git>
157157
158-
# Ngninx Certbot
158+
# Nginx Certbot
159159
letsencrypt_register_email: <letsencrypt_register_email>
160160
161+
## Uncomment to define an Nginx Default host
162+
#nginx_default_host: <nginx_defualt_fqdn>
163+
161164
## Uncomment to define custom certificates. Otherwise it will create Let's Encrypt certificates.
162165
#custom_cert:
163166
# cert: custom.crt
@@ -257,6 +260,7 @@ Replace the entries in `<>` with your values:
257260
to the proxied server (by default is `75`).
258261
- `letsencrypt_register_email`: email used for registration, recovery contact,
259262
and warnings about expired certs on Let's Encrypt.
263+
- `nginx_defualt_fqdn` (optional): enable a default host (404 page not found).
260264

261265
After configuring these parameters, you need to configure the instances of the
262266
task scheduler (Mordred) and Nginx virtual host. You need a task scheduler for each project

0 commit comments

Comments
 (0)