Skip to content

Commit 423c7b6

Browse files
committed
♻️ refactor: Refactored the Ansible role to use the new nginx acme-module for automatic certificate management, and updated the Nginx configuration template to support Let's Encrypt.
1 parent 6ea76de commit 423c7b6

File tree

6 files changed

+31
-112
lines changed

6 files changed

+31
-112
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ nginx_proxy: |
2424
}
2525
```
2626
27-
### TLS Certificates
27+
### TLS Certificates with ACME
2828
29-
The default configuration provides simple, self-signed certificates if none exist.
30-
Please make sure to replace them with your own certificates.
31-
Simply overwrite the following files:
29+
This role uses the [nginx-acme-module](https://github.com/nginx/nginx-acme) to automatically manage TLS-certificates.
3230
33-
- `/etc/nginx/tls/certificate.key;`
34-
- `/etc/nginx/tls/certificate.crt;`
31+
You can modify the url to the acme issuer in `nginx_acme_issuer_uri`.
32+
If you need to provide multiple server names, you can list them in `nginx_server_names`.
33+
34+
⚠️ You should check it the specified `nginx_resolver` is suitable for you.
3535

3636
### Advanced Configuration
3737

defaults/main.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ configure_for_firewalld: false
88
configure_for_ufw: false
99
configure_for_selinux: false
1010

11+
nginx_server_names: ["{{ inventory_hostname }}"]
12+
nginx_acme_issuer_uri: "https://acme-v02.api.letsencrypt.org/directory"
13+
# Specify a suitable DNS resolver
14+
nginx_resolver: 1.1.1.1
15+
1116
nginx_proxy: |
1217
location / {
1318
proxy_set_header Host $host;

files/dummy-tls-crt.pem

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

files/dummy-tls-key.pem

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

tasks/main.yml

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
---
22

3-
- name: Install nginx
4-
ansible.builtin.package:
5-
name: nginx
6-
state: present
3+
- name: Include OS-specific install tasks
4+
ansible.builtin.include_tasks: "install_{{ ansible_os_family | lower }}.yml"
75

86
- name: Create configuration directories
97
ansible.builtin.file:
@@ -35,19 +33,6 @@
3533
loop: '{{ nginx_config }}'
3634
notify: Reload nginx
3735

38-
- name: Install dummy TLS certificate
39-
ansible.builtin.copy:
40-
src: dummy-tls-{{ item }}.pem
41-
dest: /etc/nginx/tls/certificate.{{ item }}
42-
owner: root
43-
group: root
44-
mode: '0400'
45-
force: false
46-
notify: Reload nginx
47-
loop:
48-
- key
49-
- crt
50-
5136
- name: SELinux settings
5237
when: configure_for_selinux
5338
block:

templates/nginx.conf

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ user www-data;
77
user nginx;
88
{% endif %}
99

10+
# Load the ACME module for automatic certificate management
11+
load_module modules/ngx_http_acme_module.so;
12+
1013
# Defines the number of worker processes. Setting it to the number of
1114
# available CPU cores should be a good start. The value `auto` will try to
1215
# autodetect that.
@@ -33,6 +36,15 @@ events {
3336
}
3437

3538
http {
39+
# ACME configuration
40+
acme_issuer letsencrypt {
41+
uri {{ nginx_acme_issuer_uri }};
42+
state_path /var/cache/nginx/acme-letsencrypt;
43+
accept_terms_of_service;
44+
}
45+
46+
resolver {{ nginx_resolver }} valid=300s;
47+
3648
# Include mime types for different file extensions.
3749
include /etc/nginx/mime.types;
3850

@@ -87,18 +99,20 @@ http {
8799

88100
# Enforce encrypted connections for everything else
89101
location / {
90-
return 301 https://{{ inventory_hostname }}$request_uri;
102+
return 301 https://{{ nginx_server_names | first }}$request_uri;
91103
}
92104
}
93105

94106
server {
95107
listen 443 ssl http2;
96108
listen [::]:443 ssl http2;
97-
server_name _;
109+
server_name {{ nginx_server_names | join(" ") }};
98110

99-
ssl_certificate_key /etc/nginx/tls/certificate.key;
100-
ssl_certificate /etc/nginx/tls/certificate.crt;
111+
acme_certificate letsencrypt;
101112

113+
ssl_certificate $acme_certificate;
114+
ssl_certificate_key $acme_certificate_key;
115+
ssl_certificate_cache max=2;
102116
# Additional TLS related Nginx options
103117
include /etc/nginx/tls/tls.conf;
104118

0 commit comments

Comments
 (0)