Nginx variable refactoring and general bug solving. Got interupted commit not tested

This commit is contained in:
Kevin Veen-Birkenbach 2025-01-31 13:14:07 +01:00
parent b96d95f3e4
commit 11752f5e2e
23 changed files with 59 additions and 48 deletions

View File

@ -1,12 +1,16 @@
# Webserver Configuration # Webserver Configuration
## Nginx-Specific Path Configurations ## Nginx-Specific Path Configurations
nginx_configuration_directory: "/etc/nginx/conf.d/" # General configuration dir nginx:
nginx_servers_directory: "{{nginx_configuration_directory}}servers/" # Contains server blogs directories:
nginx_maps_directory: "{{nginx_configuration_directory}}maps/" # Contains mappins configuration: "/etc/nginx/conf.d/" # Configuration directory
nginx_streams_directory: "{{nginx_configuration_directory}}streams/" # Contains streams configuration e.g. for ldaps http:
nginx_well_known_root: "/usr/share/nginx/well-known/" # Path where well-known files are stored global: "/etc/nginx/conf.d/http/global" # Contains global configurations which will be loaded into the http block
nginx_homepage_root: "/usr/share/nginx/homepage/" # Path where the static homepage files are stored servers: "/etc/nginx/conf.d/http/servers" # Contains one configuration per domain
maps: "/etc/nginx/conf.d/http/maps/" # Contains mappings
streams: "/etc/nginx/conf.d/streams/" # Contains streams configuration e.g. for ldaps
well_known: "/usr/share/nginx/well-known/" # Path where well-known files are stored
homepage: "/usr/share/nginx/homepage/" # Path where the static homepage files are stored. @todo Move this variable to the role
## Nginx static repository ## Nginx static repository
nginx_static_repository_address: NULL #This should contain the url to an git repository which has a static homepage included and an index.html file nginx_static_repository_address: NULL #This should contain the url to an git repository which has a static homepage included and an index.html file. @todo move this variable to the role

View File

@ -13,7 +13,7 @@
- name: configure {{domain}}.conf - name: configure {{domain}}.conf
template: template:
src: roles/nginx-docker-reverse-proxy/templates/domain.conf.j2 src: roles/nginx-docker-reverse-proxy/templates/domain.conf.j2
dest: "{{nginx_servers_directory}}{{domain}}.conf" dest: "{{nginx.directories.http.servers}}{{domain}}.conf"
notify: restart nginx notify: restart nginx
- name: "include tasks update-repository-with-docker-compose.yml" - name: "include tasks update-repository-with-docker-compose.yml"

View File

@ -12,7 +12,7 @@
#- name: configure {{domain}}.conf #- name: configure {{domain}}.conf
# template: # template:
# src: "nginx-proxy.conf.j2" # src: "nginx-proxy.conf.j2"
# dest: "{{nginx_servers_directory}}{{domain}}.conf" # dest: "{{nginx.directories.http.servers}}{{domain}}.conf"
# notify: restart nginx # notify: restart nginx
- name: "include tasks nginx-docker-proxy-domain.yml" - name: "include tasks nginx-docker-proxy-domain.yml"
@ -21,7 +21,7 @@
- name: configure websocket_upgrade.conf - name: configure websocket_upgrade.conf
copy: copy:
src: "websocket_upgrade.conf" src: "websocket_upgrade.conf"
dest: "{{nginx_maps_directory}}websocket_upgrade.conf" dest: "{{nginx.directories.http.maps}}websocket_upgrade.conf"
notify: restart nginx notify: restart nginx
- name: pull docker repository - name: pull docker repository

View File

@ -2,9 +2,10 @@
set_fact: set_fact:
docker_compose_instance_directory: "{{ path_docker_compose_instances }}{{ application_id }}/" docker_compose_instance_directory: "{{ path_docker_compose_instances }}{{ application_id }}/"
- name: "Set global domain to {{ domains[application_id] }}" - name: "Set global domain based on application_id"
set_fact: set_fact:
domain: "{{ domains[application_id] }}" domain: "{{ domains[application_id] if application_id in domains else None }}"
# Default case: One Domain exists. Some applications like matrix don't have an default key
- name: "Set global http_port to {{ ports.localhost.http_ports[application_id] }}" - name: "Set global http_port to {{ ports.localhost.http_ports[application_id] }}"
set_fact: set_fact:

View File

@ -9,13 +9,13 @@
- name: Create {{domain}}.conf if LDAP is exposed to internet - name: Create {{domain}}.conf if LDAP is exposed to internet
template: template:
src: "nginx.stream.conf.j2" src: "nginx.stream.conf.j2"
dest: "{{nginx_streams_directory}}{{domain}}.conf" dest: "{{nginx.directories.streams}}{{domain}}.conf"
notify: restart nginx notify: restart nginx
when: ldap_expose_to_internet | bool when: ldap_expose_to_internet | bool
- name: Remove {{domain}}.conf if LDAP is not exposed to internet - name: Remove {{domain}}.conf if LDAP is not exposed to internet
file: file:
path: "{{ nginx_streams_directory }}{{ domain }}.conf" path: "{{ nginx.directories.streams }}{{ domain }}.conf"
state: absent state: absent
when: not ldap_expose_to_internet | bool when: not ldap_expose_to_internet | bool

View File

@ -4,5 +4,5 @@
- name: configure {{domain}}.conf - name: configure {{domain}}.conf
template: template:
src: "mastodon.conf.j2" src: "mastodon.conf.j2"
dest: "{{nginx_servers_directory}}{{domain}}.conf" dest: "{{nginx.directories.http.servers}}{{domain}}.conf"
notify: restart nginx notify: restart nginx

View File

@ -6,7 +6,7 @@
include_tasks: certbot-matomo.yml include_tasks: certbot-matomo.yml
vars: vars:
domain: "{{domains.matrix_synapse}}" domain: "{{domains.matrix_synapse}}"
http_port: "{{ports.localhost.http_port[matrix_synapse]}}" http_port: "{{ports.localhost.http_ports.matrix_synapse}}"
- name: create {{well_known_directory}} - name: create {{well_known_directory}}
file: file:
@ -22,17 +22,17 @@
- name: create {{domains.matrix_synapse}}.conf - name: create {{domains.matrix_synapse}}.conf
template: template:
src: "templates/nginx.conf.j2" src: "templates/nginx.conf.j2"
dest: "{{nginx_servers_directory}}{{domains.matrix_synapse}}.conf" dest: "{{nginx.directories.http.servers}}{{domains.matrix_synapse}}.conf"
vars: vars:
domain: "{{domains.matrix_synapse}}" domain: "{{domains.matrix_synapse}}"
http_port: "{{ports.localhost.http_port[matrix_synapse]}}" http_port: "{{ports.localhost.http_ports.matrix_synapse}}"
notify: restart nginx notify: restart nginx
- name: "include tasks nginx-docker-proxy-domain.yml for element" - name: "include tasks nginx-docker-proxy-domain.yml for element"
include_tasks: nginx-docker-proxy-domain.yml include_tasks: nginx-docker-proxy-domain.yml
vars: vars:
domain: "{{domains.matrix_element}}" domain: "{{domains.matrix_element}}"
http_port: "{{ports.localhost.http_port[matrix_element]}}" http_port: "{{ports.localhost.http_ports.matrix_element}}"
- name: include create-and-seed-database.yml for multiple bridges - name: include create-and-seed-database.yml for multiple bridges
include_tasks: create-and-seed-database.yml include_tasks: create-and-seed-database.yml

View File

@ -19,7 +19,7 @@ services:
- SYNAPSE_SERVER_NAME={{domains.matrix_synapse}} - SYNAPSE_SERVER_NAME={{domains.matrix_synapse}}
- SYNAPSE_REPORT_STATS=no - SYNAPSE_REPORT_STATS=no
ports: ports:
- "127.0.0.1:{{ports.localhost.http_port[matrix_synapse]}}:8008" - "127.0.0.1:{{ports.localhost.http_ports.matrix_synapse}}:8008"
healthcheck: healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8008/"] test: ["CMD", "curl", "-f", "http://localhost:8008/"]
interval: 1m interval: 1m
@ -39,7 +39,7 @@ services:
volumes: volumes:
- ./element-config.json:/app/config.json - ./element-config.json:/app/config.json
ports: ports:
- "127.0.0.1:{{ports.localhost.http_port[matrix_element]}}:80" - "127.0.0.1:{{ports.localhost.http_ports.matrix_element}}:80"
healthcheck: healthcheck:
test: ["CMD", "wget", "--spider", "-q", "http://localhost:80/"] test: ["CMD", "wget", "--spider", "-q", "http://localhost:80/"]
interval: 1m interval: 1m

View File

@ -3,7 +3,7 @@ application_id: "matrix"
database_password: "{{matrix_database_password}}" database_password: "{{matrix_database_password}}"
database_type: "postgres" database_type: "postgres"
registration_file_folder: "/data/" registration_file_folder: "/data/"
well_known_directory: "{{nginx_well_known_root}}/matrix/" well_known_directory: "{{nginx.directories.well_known}}/matrix/"
bridges: bridges:
- database_password: "{{ mautrix_whatsapp_bridge_database_password }}" - database_password: "{{ mautrix_whatsapp_bridge_database_password }}"

View File

@ -2,7 +2,7 @@
application_id: "mybb" application_id: "mybb"
docker_compose_instance_confd_directory: "{{docker_compose_instance_directory}}conf.d/" docker_compose_instance_confd_directory: "{{docker_compose_instance_directory}}conf.d/"
docker_compose_instance_confd_defaultconf_file: "{{docker_compose_instance_confd_directory}}default.conf" docker_compose_instance_confd_defaultconf_file: "{{docker_compose_instance_confd_directory}}default.conf"
target_mount_conf_d_directory: "{{nginx_servers_directory}}" target_mount_conf_d_directory: "{{nginx.directories.http.servers}}"
source_domain: "mybb.{{primary_domain}}" source_domain: "mybb.{{primary_domain}}"
database_password: "{{mybb_database_password}}" database_password: "{{mybb_database_password}}"
database_type: "mariadb" database_type: "mariadb"

View File

@ -8,7 +8,7 @@
- name: configure {{domain}}.conf - name: configure {{domain}}.conf
template: template:
src: "templates/nextcloud.conf.j2" src: "templates/nextcloud.conf.j2"
dest: "{{nginx_servers_directory}}{{domain}}.conf" dest: "{{nginx.directories.http.servers}}{{domain}}.conf"
notify: restart nginx notify: restart nginx
- name: create nginx.conf - name: create nginx.conf

View File

@ -4,5 +4,5 @@
- name: configure {{domain}}.conf - name: configure {{domain}}.conf
template: template:
src: "templates/peertube.conf.j2" src: "templates/peertube.conf.j2"
dest: "{{nginx_servers_directory}}{{domain}}.conf" dest: "{{nginx.directories.http.servers}}{{domain}}.conf"
notify: restart nginx notify: restart nginx

View File

@ -4,7 +4,7 @@ import sys
import re import re
# file in which fqdn server configs are deposit # file in which fqdn server configs are deposit
config_path = '{{nginx_servers_directory}}' config_path = '{{nginx.directories.http.servers}}'
# Initialize the error counter # Initialize the error counter
error_counter = 0 error_counter = 0

View File

@ -1,5 +1,7 @@
- name: create nginx letsencrypt config file - name: create nginx letsencrypt config file
template: src=letsencrypt.conf.j2 dest={{nginx_servers_directory}}letsencrypt.conf template:
src: "letsencrypt.conf.j2"
dest: "{{nginx.directories.http.global}}letsencrypt.conf"
notify: restart nginx notify: restart nginx
when: run_once_letsencrypt is not defined when: run_once_letsencrypt is not defined

View File

@ -11,6 +11,6 @@
target_domain: "{{item.target}}" target_domain: "{{item.target}}"
template: template:
src: redirect.domain.nginx.conf.j2 src: redirect.domain.nginx.conf.j2
dest: "{{nginx_servers_directory}}{{ domain }}.conf" dest: "{{nginx.directories.http.servers}}{{ domain }}.conf"
loop: "{{domain_mappings}}" loop: "{{domain_mappings}}"
notify: restart nginx notify: restart nginx

View File

@ -11,7 +11,7 @@ This Ansible role configures an Nginx server to serve a static homepage. It hand
## Role Variables ## Role Variables
- `nginx_homepage_root`: The directory where the homepage content will be stored (default: `/usr/share/nginx/homepage`) - `nginx.directories.homepage`: The directory where the homepage content will be stored (default: `/usr/share/nginx/homepage`)
- `domain`: The domain name for the Nginx server configuration - `domain`: The domain name for the Nginx server configuration
- `administrator_email`: The email used for SSL certificate registration with Let's Encrypt - `administrator_email`: The email used for SSL certificate registration with Let's Encrypt
- `nginx_static_repository_address`: The Git repository address containing the homepage content - `nginx_static_repository_address`: The Git repository address containing the homepage content

View File

@ -2,14 +2,14 @@
- name: "pull homepage from {{nginx_static_repository_address}}" - name: "pull homepage from {{nginx_static_repository_address}}"
git: git:
repo: "{{nginx_static_repository_address}}" repo: "{{nginx_static_repository_address}}"
dest: "{{nginx_homepage_root}}" dest: "{{nginx.directories.homepage}}"
update: yes update: yes
ignore_errors: true ignore_errors: true
- name: configure {{primary_domain}}.conf - name: configure {{primary_domain}}.conf
template: template:
src: "static.nginx.conf.j2" src: "static.nginx.conf.j2"
dest: "{{nginx_servers_directory}}{{primary_domain}}.conf" dest: "{{nginx.directories.http.servers}}{{primary_domain}}.conf"
vars: vars:
domain: "{{primary_domain}}" domain: "{{primary_domain}}"
notify: restart nginx notify: restart nginx

View File

@ -13,12 +13,12 @@ server
location / location /
{ {
root {{nginx_homepage_root}}; root {{nginx.directories.homepage}};
index index.html index.htm; index index.html index.htm;
} }
location /.well-known/ { location /.well-known/ {
alias {{nginx_well_known_root}}; alias {{nginx.directories.well_known}};
allow all; allow all;
default_type "text/plain"; default_type "text/plain";
autoindex on; autoindex on;

View File

@ -1,13 +1,13 @@
# README.md for nginx-www-redirect Role # README.md for nginx-www-redirect Role
## Overview ## Overview
The `nginx-www-redirect` role is designed to automate the process of setting up redirects from `www.domain.tld` to `domain.tld` for all domains and subdomains configured within the `{{nginx_servers_directory}}` directory. This role dynamically identifies configuration files following the pattern `*domain.tld.conf` and creates corresponding redirection rules. The `nginx-www-redirect` role is designed to automate the process of setting up redirects from `www.domain.tld` to `domain.tld` for all domains and subdomains configured within the `{{nginx.directories.http.servers}}` directory. This role dynamically identifies configuration files following the pattern `*domain.tld.conf` and creates corresponding redirection rules.
## Role Description ## Role Description
This role performs several key tasks: This role performs several key tasks:
1. **Find Configuration Files**: Locates all `.conf` files in the `{{nginx_servers_directory}}` directory that match the `*.*.conf` pattern, ensuring that only domain and subdomain configurations are selected. 1. **Find Configuration Files**: Locates all `.conf` files in the `{{nginx.directories.http.servers}}` directory that match the `*.*.conf` pattern, ensuring that only domain and subdomain configurations are selected.
2. **Filter Domain Names**: Processes each configuration file, extracting the domain names and removing both the `.conf` extension and the `{{nginx_servers_directory}}` path. 2. **Filter Domain Names**: Processes each configuration file, extracting the domain names and removing both the `.conf` extension and the `{{nginx.directories.http.servers}}` path.
3. **Prepare Redirect Domain Mappings**: Transforms the filtered domain names into a source-target mapping format, where `source` is `www.domain.tld` and `target` is `domain.tld`. 3. **Prepare Redirect Domain Mappings**: Transforms the filtered domain names into a source-target mapping format, where `source` is `www.domain.tld` and `target` is `domain.tld`.
@ -25,7 +25,7 @@ Example playbook:
## Requirements ## Requirements
- Ansible environment set up and configured to run roles. - Ansible environment set up and configured to run roles.
- Access to the `{{nginx_servers_directory}}` directory on the target hosts. - Access to the `{{nginx.directories.http.servers}}` directory on the target hosts.
- The `nginx-domain-redirect` role must be present and properly configured to handle the redirection mappings. - The `nginx-domain-redirect` role must be present and properly configured to handle the redirection mappings.
## Notes ## Notes

View File

@ -1,7 +1,7 @@
--- ---
- name: Find all .conf - name: Find all .conf
ansible.builtin.find: ansible.builtin.find:
paths: "{{nginx_servers_directory}}" paths: "{{nginx.directories.http.servers}}"
patterns: '*.*.conf' patterns: '*.*.conf'
register: conf_files register: conf_files
@ -9,8 +9,8 @@
set_fact: set_fact:
filtered_domains: "{{ conf_files.files | map(attribute='path') | map('regex_search', domain_regex) | select('string') | map('regex_replace', path_regex, '') | map('regex_replace', '.conf$', '') | list }}" filtered_domains: "{{ conf_files.files | map(attribute='path') | map('regex_search', domain_regex) | select('string') | map('regex_replace', path_regex, '') | map('regex_replace', '.conf$', '') | list }}"
vars: vars:
domain_regex: "^{{ nginx_servers_directory }}(?!www\\.)[^/]+\\.conf$" domain_regex: "^{{nginx.directories.http.servers}}(?!www\\.)[^/]+\\.conf$"
path_regex: "^{{ nginx_servers_directory }}" path_regex: "^{{nginx.directories.http.servers}}"
- name: The domains for which a www. redirect will be implemented - name: The domains for which a www. redirect will be implemented
debug: debug:

View File

@ -11,15 +11,19 @@
notify: restart nginx notify: restart nginx
when: run_once_nginx is not defined and nginx_matomo_tracking | bool when: run_once_nginx is not defined and nginx_matomo_tracking | bool
- name: "Delete {{nginx.directories.configuration}} directory, when mode_reset"
file:
path: "{{ nginx.directories.configuration }}"
state: absent
when: mode_reset and run_once_nginx is not defined
- name: Ensure nginx configuration directories are present - name: Ensure nginx configuration directories are present
file: file:
path: "{{ item }}" path: "{{ item }}"
state: directory state: directory
mode: '0755' mode: '0755'
loop: recurse: yes
- "{{nginx_servers_directory}}" loop: "{{ nginx.directories.http + nginx.directories.streams }}"
- "{{nginx_maps_directory}}"
- "{{nginx_streams_directory}}"
when: run_once_nginx is not defined when: run_once_nginx is not defined
- name: create nginx config file - name: create nginx config file

View File

@ -34,11 +34,11 @@ http
gzip_types application/atom+xml application/javascript application/xml+rss application/x-javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy text/javascript text/xml; gzip_types application/atom+xml application/javascript application/xml+rss application/x-javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy text/javascript text/xml;
types_hash_max_size 4096; types_hash_max_size 4096;
include {{nginx_maps_directory}}*.conf; include {{nginx.directories.http.maps}}*.conf;
include {{nginx_servers_directory}}*.conf; include {{nginx.directories.http.servers}}*.conf;
} }
# For port proxies # For port proxies
stream{ stream{
include {{nginx_streams_directory}}*.conf; include {{nginx.directories.streams}}*.conf;
} }

View File

@ -1,5 +1,5 @@
- name: configure {{domain}}.conf - name: configure {{domain}}.conf
template: template:
src: "roles/nginx-docker-reverse-proxy/templates/domain.conf.j2" src: "roles/nginx-docker-reverse-proxy/templates/domain.conf.j2"
dest: "{{nginx_servers_directory}}{{domain}}.conf" dest: "{{nginx.directories.http.servers}}{{domain}}.conf"
notify: restart nginx notify: restart nginx