summaryrefslogtreecommitdiff
path: root/roles/nginx
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2019-10-12 03:01:11 +0200
committerChristian Pointner <equinox@spreadspace.org>2019-10-12 03:01:11 +0200
commitd11a24b7f0edf309a2bbd5687acbea8bc1e012cc (patch)
tree919f7fedd1fecd88877ffbb12449345bf589dc83 /roles/nginx
parentcreate acmetool accounts for some machines (diff)
added generic nginx role
Diffstat (limited to 'roles/nginx')
-rw-r--r--roles/nginx/defaults/main.yml13
-rw-r--r--roles/nginx/handlers/main.yml5
-rw-r--r--roles/nginx/tasks/acme.yml50
-rw-r--r--roles/nginx/tasks/main.yml53
-rw-r--r--roles/nginx/templates/generic-proxy-no-buffering-with-acme.conf.j248
5 files changed, 169 insertions, 0 deletions
diff --git a/roles/nginx/defaults/main.yml b/roles/nginx/defaults/main.yml
new file mode 100644
index 00000000..ae4aa9d4
--- /dev/null
+++ b/roles/nginx/defaults/main.yml
@@ -0,0 +1,13 @@
+---
+nginx_pkg_name: nginx-light
+
+# nginx_vhosts:
+# example:
+# template: generic-proxy-no-buffering-with-acme
+# acme: yes
+# hostnames:
+# - example.com
+# - www.example.com
+# proxy_pass: http://127.0.0.1:8080
+# other.io:
+# contents: "<< nginx vhost config file contents >>"
diff --git a/roles/nginx/handlers/main.yml b/roles/nginx/handlers/main.yml
new file mode 100644
index 00000000..6deed0cd
--- /dev/null
+++ b/roles/nginx/handlers/main.yml
@@ -0,0 +1,5 @@
+---
+- name: restart nginx
+ service:
+ name: nginx
+ state: restarted
diff --git a/roles/nginx/tasks/acme.yml b/roles/nginx/tasks/acme.yml
new file mode 100644
index 00000000..c08c0a57
--- /dev/null
+++ b/roles/nginx/tasks/acme.yml
@@ -0,0 +1,50 @@
+---
+- name: check if acme certs already exist
+ loop: "{{ item.value.hostnames }}"
+ loop_control:
+ loop_var: acme_hostname
+ stat:
+ path: "/var/lib/acme/live/{{ acme_hostname }}"
+ register: acme_cert_stat
+
+- name: set acmecert_missing_hostnames variable
+ set_fact:
+ acmecert_missing_hostnames: "{{ acme_cert_stat.results | acme_cert_nonexistent(item.value.hostnames) }}"
+
+- name: link nonexistent hostnames to self-signed interim cert
+ when: acmecert_missing_hostnames | length > 0
+ block:
+ - name: get id of existing selfsigned interim certificate
+ command: cat /var/lib/acme/.selfsigned-interim-cert
+ changed_when: false
+ check_mode: false
+ register: selfsigned_interim_cert_id
+
+ - name: set selfsigned_interim_cert_id variable
+ set_fact:
+ selfsigned_interim_cert_id: "{{ selfsigned_interim_cert_id.stdout }}"
+
+ - name: link to snakeoil cert for nonexistent hostnames
+ loop: "{{ acmecert_missing_hostnames }}"
+ loop_control:
+ loop_var: acme_missing_hostname
+ file:
+ src: "../certs/{{ selfsigned_interim_cert_id }}"
+ dest: "/var/lib/acme/live/{{ acme_missing_hostname }}"
+ state: link
+
+- name: enable vhost config using acme cert
+ file:
+ src: ../sites-available/liquidtruth
+ dest: /etc/nginx/sites-enabled/liquidtruth
+ state: link
+
+- name: make sure nginx config has been (re)loaded
+ meta: flush_handlers
+
+- name: get certificate using acmetool
+ import_role:
+ name: acmetool/cert
+ vars:
+ acmetool_cert_name: "{{ item.value.hostnames[0] }}"
+ acmetool_cert_hostnames: "{{ item.value.hostnames }}"
diff --git a/roles/nginx/tasks/main.yml b/roles/nginx/tasks/main.yml
new file mode 100644
index 00000000..19791235
--- /dev/null
+++ b/roles/nginx/tasks/main.yml
@@ -0,0 +1,53 @@
+---
+- name: install nginx
+ apt:
+ name: "{{ nginx_pkg_name }}"
+ state: present
+
+- name: remove nginx default config
+ file:
+ name: /etc/nginx/sites-enabled/default
+ state: absent
+ notify: restart nginx
+
+- name: install nginx config snippets
+ loop:
+ - ssl
+ - hsts
+ copy:
+ src: "{{ global_files_dir }}/common/nginx-snippets/{{ item }}.conf"
+ dest: /etc/nginx/snippets/
+ notify: restart nginx
+
+- name: generate Diffie-Hellman parameters
+ openssl_dhparam:
+ path: /etc/ssl/dhparams.pem
+ size: 2048
+ notify: restart nginx
+
+- name: install nginx configs from template
+ loop: "{{ nginx_vhosts | dict2items }}"
+ loop_control:
+ label: "{{ item.key }}"
+ when: "'template' in item.value"
+ template:
+ src: "{{ item.value.template }}.conf.j2"
+ dest: "/etc/nginx/sites-available/{{ item.key }}"
+ notify: restart nginx
+
+- name: install nginx configs from config data
+ loop: "{{ nginx_vhosts | dict2items }}"
+ loop_control:
+ label: "{{ item.key }}"
+ when: "'contents' in item.value"
+ copy:
+ contents: "{{ item.value.contents }}"
+ dest: "/etc/nginx/sites-available/{{ item.key }}"
+ notify: restart nginx
+
+- name: generate acme certificate
+ loop: "{{ nginx_vhosts | dict2items }}"
+ loop_control:
+ label: "{{ item.key }} ({{ item.value.hostnames | join(', ') }})"
+ when: item.value.acme
+ include_tasks: acme.yml
diff --git a/roles/nginx/templates/generic-proxy-no-buffering-with-acme.conf.j2 b/roles/nginx/templates/generic-proxy-no-buffering-with-acme.conf.j2
new file mode 100644
index 00000000..784b2590
--- /dev/null
+++ b/roles/nginx/templates/generic-proxy-no-buffering-with-acme.conf.j2
@@ -0,0 +1,48 @@
+# used for websockets
+# set http_connection to either upgrade or close (as normal)
+map $http_upgrade $connection_upgrade {
+ default upgrade;
+ '' close;
+}
+
+server {
+ listen 80;
+ listen [::]:80;
+ server_name {{ item.value.hostnames | join(' ') }};
+
+ include snippets/acmetool.conf;
+
+ location / {
+ return 301 https://$host$request_uri;
+ }
+}
+
+server {
+ listen 443 ssl http2;
+ listen [::]:443 ssl http2;
+ server_name {{ item.value.hostnames | join(' ') }};
+
+ include snippets/acmetool.conf;
+ include snippets/ssl.conf;
+ ssl_certificate /var/lib/acme/live/{{ item.value.hostnames[0] }}/fullchain;
+ ssl_certificate_key /var/lib/acme/live/{{ item.value.hostnames[0] }}/privkey;
+ include snippets/hsts.conf;
+
+ location / {
+ proxy_buffering off;
+ proxy_ignore_headers "X-Accel-Buffering";
+ proxy_request_buffering off;
+ proxy_http_version 1.1;
+
+ proxy_set_header Host $host;
+ proxy_set_header X-Real-IP $remote_addr;
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-Proto $scheme;
+
+ # for websockets
+ proxy_set_header Upgrade $http_upgrade;
+ proxy_set_header Connection $connection_upgrade;
+
+ proxy_pass {{ item.value.proxy_pass }};
+ }
+}