From 04c73fb184bdabcca99bec0547137cb81e2bf930 Mon Sep 17 00:00:00 2001 From: Jim Martens Date: Sat, 19 Aug 2023 16:50:26 +0200 Subject: [PATCH] Enable updating nginx configuration after certbot run --- ansible/roles/spring_boot/tasks/nginx.yaml | 20 +++++++++-- .../sites-available/spring_domain.conf.j2 | 2 ++ .../spring_domain_existing.conf.j2 | 36 +++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 ansible/roles/spring_boot/templates/etc/nginx/sites-available/spring_domain_existing.conf.j2 diff --git a/ansible/roles/spring_boot/tasks/nginx.yaml b/ansible/roles/spring_boot/tasks/nginx.yaml index c724911..a45f890 100644 --- a/ansible/roles/spring_boot/tasks/nginx.yaml +++ b/ansible/roles/spring_boot/tasks/nginx.yaml @@ -1,5 +1,20 @@ --- -- name: Add HTTP site +- name: Determine if Nginx config already exists + ansible.builtin.stat: + path: /etc/nginx/sites-available/{{ spring_domain }}.conf + register: nginx_site +- name: Update Spring nginx site + when: nginx_site.stat.exists + ansible.builtin.template: + src: etc/nginx/sites-available/spring_domain_existing.conf.j2 + dest: /etc/nginx/sites-available/{{ spring_domain }}.conf + owner: root + group: root + mode: "644" + force: true + lstrip_blocks: true +- name: Add Spring nginx site + when: not nginx_site.stat.exists ansible.builtin.template: src: etc/nginx/sites-available/spring_domain.conf.j2 dest: /etc/nginx/sites-available/{{ spring_domain }}.conf @@ -12,7 +27,8 @@ ansible.builtin.command: cmd: rm /etc/nginx/sites-enabled/default removes: /etc/nginx/sites-enabled/default -- name: Enable HTTP site +- name: Enable Spring nginx site + when: not nginx_site.stat.exists ansible.builtin.command: cmd: ln -sf /etc/nginx/sites-available/{{ spring_domain }}.conf /etc/nginx/sites-enabled/{{ spring_domain }}.conf creates: /etc/nginx/sites-enabled/{{ spring_domain }}.conf diff --git a/ansible/roles/spring_boot/templates/etc/nginx/sites-available/spring_domain.conf.j2 b/ansible/roles/spring_boot/templates/etc/nginx/sites-available/spring_domain.conf.j2 index 5f8fa3e..66c8e52 100644 --- a/ansible/roles/spring_boot/templates/etc/nginx/sites-available/spring_domain.conf.j2 +++ b/ansible/roles/spring_boot/templates/etc/nginx/sites-available/spring_domain.conf.j2 @@ -11,6 +11,8 @@ server { {% for app in spring_apps %} {% if app.expose %} location {{ app.path }} { + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; proxy_pass http://localhost:{{ app.port }}; } {% endif %} diff --git a/ansible/roles/spring_boot/templates/etc/nginx/sites-available/spring_domain_existing.conf.j2 b/ansible/roles/spring_boot/templates/etc/nginx/sites-available/spring_domain_existing.conf.j2 new file mode 100644 index 0000000..45a7b98 --- /dev/null +++ b/ansible/roles/spring_boot/templates/etc/nginx/sites-available/spring_domain_existing.conf.j2 @@ -0,0 +1,36 @@ +server { + + server_name {{ spring_domain }}; + + location ^~ /.well-known/acme-challenge/ { + default_type "text/plain"; + } + + {% for app in spring_apps %} + {% if app.expose %} + location {{ app.path }} { + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_pass http://localhost:{{ app.port }}; + } + {% endif %} + {% endfor %} + + listen [::]:443 ssl ipv6only=on; # managed by Certbot + listen 443 ssl; # managed by Certbot + ssl_certificate /etc/letsencrypt/live/{{ spring_domain }}/fullchain.pem; # managed by Certbot + ssl_certificate_key /etc/letsencrypt/live/{{ spring_domain }}/privkey.pem; # managed by Certbot + include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot + ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot +} +server { + if ($host = {{ spring_domain }}) { + return 301 https://$host$request_uri; + } # managed by Certbot + + listen 80; + listen [::]:80; + + server_name {{ spring_domain }}; + return 404; # managed by Certbot +}