From e0f3fbe22bd99e9eca73fe4f1d6cfb8a86cb2ab9 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 18 Nov 2024 15:31:15 +0100 Subject: [PATCH] fix(forgejo): handle reverse proxy Signed-off-by: Matej Focko --- roles/forgejo/defaults/main.yml | 13 +++++++++++++ roles/forgejo/tasks/main.yml | 10 ++++++++++ roles/forgejo/templates/forgejo.container | 4 ++-- roles/forgejo/templates/nginx.conf | 18 ++++++++++++++++++ 4 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 roles/forgejo/defaults/main.yml create mode 100644 roles/forgejo/templates/nginx.conf diff --git a/roles/forgejo/defaults/main.yml b/roles/forgejo/defaults/main.yml new file mode 100644 index 0000000..40285e9 --- /dev/null +++ b/roles/forgejo/defaults/main.yml @@ -0,0 +1,13 @@ +--- +# Defines whether Forgejo is deployed behind a reverse proxy +# if so, installs the nginx config file +forgejo_reverse_proxy: true + +# Subdomain to be used for the reverse proxy configuration +forgejo_subdomain: git + +# HTTP port that's both exposed by container and used by the reverse proxy +forgejo_http_port: 3000 + +# SSH port that's exposed from the container +forgejo_ssh_port: 2222 diff --git a/roles/forgejo/tasks/main.yml b/roles/forgejo/tasks/main.yml index 66b7ae0..d60326d 100644 --- a/roles/forgejo/tasks/main.yml +++ b/roles/forgejo/tasks/main.yml @@ -11,6 +11,16 @@ - forgejo.network - forgejo.volume +- name: Install the reverse proxy config + ansible.builtin.template: + src: templates/nginx.conf + dest: "/etc/nginx/conf.d/forgejo.conf" + mode: 0644 + owner: root + group: root + when: forgejo_reverse_proxy + notify: Restart nginx + - name: Enable the Forgejo quadlet ansible.builtin.systemd_service: daemon_reload: true diff --git a/roles/forgejo/templates/forgejo.container b/roles/forgejo/templates/forgejo.container index 6603318..3ace41c 100644 --- a/roles/forgejo/templates/forgejo.container +++ b/roles/forgejo/templates/forgejo.container @@ -15,8 +15,8 @@ Environment=SSH_CREATE_AUTHORIZED_PRINCIPALS_FILES=true Environment=SSH_AUTHORIZED_PRINCIPALS_ALLOW="username,email" Network=forgejo.network -PublishPort=2222:22 -PublishPort=3000:3000 +PublishPort={{ forgejo_ssh_port }}:22 +PublishPort={{ forgejo_http_port }}:3000 Volume=forgejo-data:/data diff --git a/roles/forgejo/templates/nginx.conf b/roles/forgejo/templates/nginx.conf new file mode 100644 index 0000000..6083b40 --- /dev/null +++ b/roles/forgejo/templates/nginx.conf @@ -0,0 +1,18 @@ +# {{ ansible_managed }} + +upstream forgejo { + server 127.0.0.1:{{ forgejo_http_port }}; +} + +server { + listen 443 ssl http2; # managed by Certbot + listen [::]:443 ssl http2; # managed by Certbot + + include ssl.conf; + server_name {{ forgejo_subdomain }}.{{ host_fqdn }}; + + location ~ / { + include proxy.conf; + proxy_pass http://forgejo; + } +}