aboutsummaryrefslogtreecommitdiff
path: root/roles
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-10-22 20:12:08 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-10-22 20:12:08 +0000
commit782e4429c89c6e91cd9aa7e5ba59517a52200dcd (patch)
treee7191c9c998070d9393ce22748ebcfa859991dc0 /roles
parentfeat: add fail2ban (diff)
downloadinfra-782e4429c89c6e91cd9aa7e5ba59517a52200dcd.tar.gz
feat: add nginx
Diffstat (limited to 'roles')
-rw-r--r--roles/nginx/handlers/main.yml5
-rw-r--r--roles/nginx/tasks/main.yml50
-rw-r--r--roles/nginx/templates/nginx-validate.j238
-rw-r--r--roles/nginx/templates/nginx.conf.j233
4 files changed, 126 insertions, 0 deletions
diff --git a/roles/nginx/handlers/main.yml b/roles/nginx/handlers/main.yml
new file mode 100644
index 0000000..0f31453
--- /dev/null
+++ b/roles/nginx/handlers/main.yml
@@ -0,0 +1,5 @@
1---
2- name: Reload nginx
3 ansible.builtin.service:
4 name: nginx
5 state: reloaded
diff --git a/roles/nginx/tasks/main.yml b/roles/nginx/tasks/main.yml
new file mode 100644
index 0000000..f4ffb6c
--- /dev/null
+++ b/roles/nginx/tasks/main.yml
@@ -0,0 +1,50 @@
1---
2- block:
3 - name: Install nginx and required modules
4 community.general.apk:
5 name:
6 - nginx
7 - nginx-openrc
8 - nginx-mod-http-xslt-filter
9 state: present
10
11 # permission are not right
12 # - name: Ensure nginx web and log directories exist
13 # ansible.builtin.file:
14 # path: "{{ item }}"
15 # state: directory
16 # owner: nginx
17 # group: nginx
18 # mode: '0755'
19 # loop:
20 # - /srv/http
21 # - /var/log/nginx
22 # - /var/lib/nginx
23 # - /var/www/acme
24
25 - name: Deploy main nginx.conf
26 ansible.builtin.template:
27 src: nginx.conf.j2
28 dest: /etc/nginx/nginx.conf
29 owner: root
30 group: root
31 mode: '0444'
32 validate: nginx-validate %s /etc/nginx/nginx.conf
33 notify: Reload nginx
34
35 - name: Enable and start nginx service
36 ansible.builtin.service:
37 name: nginx
38 enabled: true
39 state: started
40
41 - name: Install atomic nginx validation wrapper
42 ansible.builtin.template:
43 src: nginx-validate.j2
44 dest: /usr/local/bin/nginx-validate
45 owner: root
46 group: root
47 mode: '0755'
48
49 tags:
50 - nginx
diff --git a/roles/nginx/templates/nginx-validate.j2 b/roles/nginx/templates/nginx-validate.j2
new file mode 100644
index 0000000..81c6fb8
--- /dev/null
+++ b/roles/nginx/templates/nginx-validate.j2
@@ -0,0 +1,38 @@
1#!/bin/sh
2
3# {{ ansible_managed }}
4# Atomic Nginx configuration validator wrapper for Ansible
5
6TEMP_FILE="$1"
7DEST_FILE="$2"
8
9if [ -z "$TEMP_FILE" ] || [ -z "$DEST_FILE" ]; then
10 echo "Usage: nginx-validate <temp_file> <dest_file>" >&2
11 exit 1
12fi
13
14TIMESTAMP=$(date +%Y%m%d-%H%M%S)
15BACKUP_FILE="${DEST_FILE}.${TIMESTAMP}.bak"
16
17# Back up current destination if it exists
18if [ -f "$DEST_FILE" ]; then
19 cp "$DEST_FILE" "$BACKUP_FILE"
20fi
21
22# Apply the rendered temporary file for testing
23cp "$TEMP_FILE" "$DEST_FILE"
24
25# Test master configuration
26if nginx -t; then
27 rm -f "$BACKUP_FILE"
28 exit 0
29else
30 # Restore previous config on failure and clean up
31 if [ -f "$BACKUP_FILE" ]; then
32 cp "$BACKUP_FILE" "$DEST_FILE"
33 rm -f "$BACKUP_FILE"
34 else
35 rm -f "$DEST_FILE"
36 fi
37 exit 1
38fi
diff --git a/roles/nginx/templates/nginx.conf.j2 b/roles/nginx/templates/nginx.conf.j2
new file mode 100644
index 0000000..110fa02
--- /dev/null
+++ b/roles/nginx/templates/nginx.conf.j2
@@ -0,0 +1,33 @@
1# {{ ansible_managed }}
2user nginx;
3error_log /var/log/nginx/error.log warn;
4pid /var/run/nginx/nginx.pid;
5
6worker_processes auto;
7worker_rlimit_nofile 65535;
8
9events {
10 worker_connections 4096;
11 use epoll;
12 multi_accept on;
13}
14
15http {
16 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
17 '$status $body_bytes_sent "$http_referer" '
18 '"$http_user_agent" "$http_x_forwarded_for"';
19 access_log /var/log/nginx/access.log main;
20
21 sendfile on;
22 tcp_nopush on;
23 tcp_nodelay on;
24 server_tokens off;
25 keepalive_timeout 65;
26
27 client_body_buffer_size 512k;
28 client_max_body_size 64M;
29
30 default_type application/octet-stream;
31 include /etc/nginx/mime.types;
32 include /etc/nginx/http.d/*.conf;
33}