Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

Compress responses for every site

Juan Denisb02dd53eaafd
diff --git a/backend/app/services/htaccess_converter.py b/backend/app/services/htaccess_converter.py
index 04a3d997..6471a38e 100644
--- a/backend/app/services/htaccess_converter.py
+++ b/backend/app/services/htaccess_converter.py
@@ -33,9 +33,9 @@ _KNOWN_UNSUPPORTED = {
'expiresactive': 'mod_expires has no direct include-level equivalent; use nginx `expires` inside a location block manually',
'expiresbytype': 'mod_expires has no direct include-level equivalent; use nginx `expires` inside a location block manually',
'expiresdefault': 'mod_expires has no direct include-level equivalent; use nginx `expires` inside a location block manually',
- 'addoutputfilterbytype': 'compression is configured globally via nginx gzip settings, not per-site',
- 'setoutputfilter': 'compression is configured globally via nginx gzip settings, not per-site',
- 'browsermatch': 'browser matching for compression is handled by nginx gzip_disable globally',
+ 'addoutputfilterbytype': 'compression is global: ServerKit writes gzip (and brotli when available) for every site in conf.d/serverkit-compression.conf',
+ 'setoutputfilter': 'compression is global: ServerKit writes gzip (and brotli when available) for every site in conf.d/serverkit-compression.conf',
+ 'browsermatch': 'per-browser rules have no nginx include-level equivalent; compression is global (conf.d/serverkit-compression.conf)',
'php_value': 'PHP settings belong in the PHP-FPM pool config or .user.ini, not nginx',
'php_flag': 'PHP settings belong in the PHP-FPM pool config or .user.ini, not nginx',
'php_admin_value': 'PHP settings belong in the PHP-FPM pool config or .user.ini, not nginx',
diff --git a/backend/app/services/nginx_service.py b/backend/app/services/nginx_service.py
index 00147ccd..305c8345 100644
--- a/backend/app/services/nginx_service.py
+++ b/backend/app/services/nginx_service.py
@@ -1,3 +1,4 @@
+import logging
import os
import subprocess
import re
@@ -7,6 +8,8 @@ from pathlib import Path
from app.utils.system import (ServiceControl, is_command_available,
run_privileged, write_privileged_file)
+logger = logging.getLogger(__name__)
+
def _auto_capture_vhost(name, action):
"""Best-effort checkpoint for request-bound vhost lifecycle mutations."""
@@ -377,6 +380,35 @@ log_format serverkit_timed '$remote_addr - $remote_user [$time_local] "$request"
'cs=$upstream_cache_status h=$host';
"""
+ # ==================== COMPRESSION (plan 86 §B1) ====================
+ # gzip (and brotli when the module is loaded) for every site, as one
+ # http-level conf.d snippet. Stock configs already set some of these —
+ # Debian's nginx.conf ships `gzip on;` — and a repeated http-level
+ # directive is fatal to `nginx -t`, so the snippet only carries the
+ # directives the running config does not already set (read from `nginx -T`).
+ COMPRESSION_CONF_NAME = 'serverkit-compression.conf'
+ # text/html is always compressed by nginx; listing it again only warns.
+ COMPRESSION_TYPES = (
+ 'text/plain text/css text/xml text/javascript application/javascript '
+ 'application/json application/xml application/rss+xml '
+ 'application/atom+xml image/svg+xml application/wasm '
+ 'font/ttf font/otf application/vnd.ms-fontobject'
+ )
+ GZIP_DIRECTIVES = (
+ ('gzip', 'on'),
+ ('gzip_vary', 'on'),
+ ('gzip_proxied', 'any'),
+ ('gzip_comp_level', '5'),
+ ('gzip_min_length', '1024'),
+ ('gzip_types', COMPRESSION_TYPES),
+ )
+ BROTLI_DIRECTIVES = (
+ ('brotli', 'on'),
+ ('brotli_comp_level', '5'),
+ ('brotli_min_length', '1024'),
+ ('brotli_types', COMPRESSION_TYPES),
+ )
+
# ==================== MICRO-CACHE (task #21) ====================
# Opt-in per-site micro-cache: a very short (10s) full-page cache in front
# of proxied/PHP sites, with hard bypasses for anything personalized.
@@ -795,6 +827,95 @@ location /p/ {{
except Exception as e:
return {'success': False, 'error': str(e)}
+ @classmethod
+ def _http_level_directives(cls, dump: str, skip_file: str) -> set:
+ """Directive names set at http level in an ``nginx -T`` dump.
+
+ ``nginx -T`` prints each file after a ``# configuration file <path>:``
+ header. In the main nginx.conf, http level is depth 1 inside
+ ``http {}``; every other file is included from the http block (conf.d)
+ or a server (sites), so its depth-0 directives are the http-level
+ ones. ``skip_file`` (our own snippet) is ignored so a rewrite never
+ mistakes its own lines for someone else's.
+ """
+ found = set()
+ current = None
+ depth = 0
+ in_http = False
+ for raw in dump.splitlines():
+ header = re.match(r'^# configuration file (.+):$', raw.strip())
+ if header:
+ current = header.group(1)
+ depth = 0
+ in_http = False
+ continue
+ if current is None or current == skip_file:
+ continue
+ line = raw.split('#', 1)[0].strip()
+ if not line:
+ continue
+ is_main = current.endswith('/nginx.conf')
+ http_level = (depth == 1 and in_http) if is_main else depth == 0
+ name = line.split(None, 1)[0].rstrip(';{')
+ if http_level and line.endswith(';'):
+ found.add(name)
+ if is_main and depth == 0 and name == 'http' and '{' in line:
+ in_http = True
+ depth += line.count('{') - line.count('}')
+ if is_main and depth == 0:
+ in_http = False
+ return found
+
+ @classmethod
+ def render_compression_snippet(cls, dump: str, conf_path: str,
+ nginx_v: str = '') -> str:
+ """The snippet for this box. When the running config already sets
+ everything it is comments only: still written, so the "already
+ handled" check stays a file-exists test instead of an ``nginx -T``
+ on every vhost write."""
+ present = cls._http_level_directives(dump, conf_path)
+ brotli = ('ngx_http_brotli_filter_module' in dump
+ or 'brotli' in (nginx_v or '').lower())
+ wanted = list(cls.GZIP_DIRECTIVES)
+ if brotli:
+ wanted += list(cls.BROTLI_DIRECTIVES)
+ lines = [f'{name} {value};' for name, value in wanted if name not in present]
+ kept = sorted(name for name, _ in wanted if name in present)
+ header = ['# ServerKit compression (auto-generated; do not edit).']
+ if kept:
+ header.append('# Already set elsewhere, left alone: ' + ', '.join(kept))
+ return '\n'.join(header + lines) + '\n'
+
+ @classmethod
+ def ensure_compression(cls) -> Dict:
+ """Write the compression snippet once, best-effort (plan 86 §B1).
+
+ Skips when the snippet exists. A snippet that fails ``nginx -t`` is
+ removed again, so compression can never take the web server down —
+ it just stays off, and the error says why.
+ """
+ conf_path = os.path.join(cls.NGINX_CONF_DIR, 'conf.d', cls.COMPRESSION_CONF_NAME)
+ if os.path.isfile(conf_path):
+ return {'success': True, 'changed': False, 'path': conf_path}
+ try:
+ dump = run_privileged([cls.NGINX_BIN, '-T'], timeout=30)
+ if dump.returncode != 0:
+ return {'success': False, 'error': (dump.stderr or 'nginx -T failed').strip()}
+ version = run_privileged([cls.NGINX_BIN, '-V'], timeout=30)
+ snippet = cls.render_compression_snippet(
+ dump.stdout or '', conf_path, (version.stderr or '') + (version.stdout or ''))
+ written = write_privileged_file(conf_path, snippet)
+ if not written['success']:
+ return {'success': False, 'error': written['error']}
+ test = cls.test_config()
+ if not test['success']:
+ run_privileged(['rm', '-f', conf_path])
+ return {'success': False,
+ 'error': f"compression snippet rejected: {test.get('message') or test.get('error')}"}
+ return {'success': True, 'changed': True, 'path': conf_path}
+ except Exception as e: # noqa: BLE001 - best-effort, never blocks a vhost
+ return {'success': False, 'error': str(e)}
+
@classmethod
def purge_micro_cache(cls) -> Dict:
"""Wipe the shared micro-cache directory contents. Linux-only.
@@ -949,6 +1070,12 @@ location /p/ {{
return {'success': False,
'error': f"log format setup failed: {fmt.get('error')}"}
+ # Compression is global and best-effort: a box where it can't be set
+ # up still gets its vhost.
+ compression = cls.ensure_compression()
+ if not compression.get('success'):
+ logger.info('nginx compression not configured: %s', compression.get('error'))
+
available_path = os.path.join(cls.SITES_AVAILABLE, name)
enabled_path = os.path.join(cls.SITES_ENABLED, name)
previous = cls.read_vhost(name)
diff --git a/backend/tests/test_nginx_compression.py b/backend/tests/test_nginx_compression.py
new file mode 100644
index 00000000..e58edf24
--- /dev/null
+++ b/backend/tests/test_nginx_compression.py
@@ -0,0 +1,151 @@
+"""Global gzip/brotli snippet (plan 86 §B1).
+
+Stock nginx configs already set some compression directives (Debian ships
+`gzip on;` in nginx.conf), and repeating an http-level directive is fatal to
+`nginx -t`. The snippet therefore carries only what the running config lacks,
+read from `nginx -T`. Its real-parser leg is in test_real_nginx_vhost_syntax.
+"""
+import pytest
+
+from app.services.nginx_service import NginxService
+
+OURS = '/etc/nginx/conf.d/serverkit-compression.conf'
+
+DEBIAN_DUMP = """nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
+# configuration file /etc/nginx/nginx.conf:
+user www-data;
+include /etc/nginx/modules-enabled/*.conf;
+events {
+\tworker_connections 768;
+}
+http {
+\tsendfile on;
+\tgzip on;
+\t# gzip_vary on;
+\t# gzip_types text/plain text/css;
+\tinclude /etc/nginx/conf.d/*.conf;
+\tinclude /etc/nginx/sites-enabled/*;
+}
+
+# configuration file /etc/nginx/sites-enabled/shop:
+server {
+ listen 80;
+ gzip_types text/plain;
+ location / {
+ gzip_vary off;
+ }
+}
+"""
+
+
+def _names(snippet):
+ return [line.split()[0] for line in snippet.splitlines()
+ if line and not line.startswith('#')]
+
+
+def test_debian_gzip_on_is_not_repeated():
+ snippet = NginxService.render_compression_snippet(DEBIAN_DUMP, OURS)
+ names = _names(snippet)
+ assert 'gzip' not in names
+ assert names == ['gzip_vary', 'gzip_proxied', 'gzip_comp_level',
+ 'gzip_min_length', 'gzip_types']
+ assert '# Already set elsewhere, left alone: gzip' in snippet
+
+
+def test_commented_and_server_level_directives_do_not_count():
+ """`# gzip_vary on;` and a server block's gzip_types are not http level."""
+ present = NginxService._http_level_directives(DEBIAN_DUMP, OURS)
+ assert 'gzip' in present
+ assert 'gzip_vary' not in present
+ assert 'gzip_types' not in present
+
+
+def test_a_conf_d_file_setting_gzip_types_is_respected():
+ dump = DEBIAN_DUMP + (
+ '\n# configuration file /etc/nginx/conf.d/tuning.conf:\n'
+ 'gzip_types text/plain;\n')
+ assert 'gzip_types' not in _names(
+ NginxService.render_compression_snippet(dump, OURS))
+
+
+def test_our_own_snippet_is_ignored_on_a_rewrite():
+ dump = DEBIAN_DUMP + (
+ f'\n# configuration file {OURS}:\n'
+ 'gzip_vary on;\ngzip_types text/plain;\n')
+ assert 'gzip_vary' in _names(
+ NginxService.render_compression_snippet(dump, OURS))
+
+
+def test_a_config_without_gzip_gets_the_full_set():
+ dump = ('# configuration file /etc/nginx/nginx.conf:\n'
+ 'events {}\nhttp {\n sendfile on;\n}\n')
+ names = _names(NginxService.render_compression_snippet(dump, OURS))
+ assert names[0] == 'gzip' and 'brotli' not in names
+
+
[email protected]('dump_extra,nginx_v', [
+ ('load_module modules/ngx_http_brotli_filter_module.so;\n', ''),
+ ('', 'configure arguments: --add-module=../ngx_brotli'),
+])
+def test_brotli_only_when_the_module_is_there(dump_extra, nginx_v):
+ dump = ('# configuration file /etc/nginx/nginx.conf:\n' + dump_extra
+ + 'events {}\nhttp {\n}\n')
+ names = _names(NginxService.render_compression_snippet(dump, OURS, nginx_v))
+ assert {'brotli', 'brotli_types'} <= set(names)
+
+
+def test_everything_already_set_is_a_comment_only_snippet():
+ """Still written, so later vhost writes don't re-run `nginx -T`."""
+ lines = ''.join(f' {n} {v};\n' for n, v in NginxService.GZIP_DIRECTIVES)
+ dump = ('# configuration file /etc/nginx/nginx.conf:\n'
+ f'events {{}}\nhttp {{\n{lines}}}\n')
+ snippet = NginxService.render_compression_snippet(dump, OURS)
+ assert _names(snippet) == []
+ assert 'left alone: gzip,' in snippet
+
+
+def test_text_html_is_not_listed():
+ """nginx always compresses text/html; listing it again only warns."""
+ assert 'text/html' not in NginxService.COMPRESSION_TYPES
+
+
+class TestEnsureCompression:
+ @pytest.fixture
+ def conf(self, tmp_path, monkeypatch, fake_subprocess):
+ from subprocess_stub import FakeProc
+
+ (tmp_path / 'conf.d').mkdir()
+ monkeypatch.setattr(NginxService, 'NGINX_CONF_DIR', str(tmp_path))
+ monkeypatch.setattr('app.services.nginx_service.is_command_available',
+ lambda *a, **k: True)
+
+ def tee(argv, kwargs):
+ with open(argv[-1], 'w') as fh:
+ fh.write(kwargs.get('input', ''))
+ return FakeProc()
+
+ def rm(argv, kwargs):
+ import os
+ for path in argv[2:]:
+ if os.path.exists(path):
+ os.remove(path)
+ return FakeProc()
+
+ fake_subprocess.when(['tee'], tee)
+ fake_subprocess.when(['rm'], rm)
+ fake_subprocess.script(['nginx', '-T'], stdout=DEBIAN_DUMP)
+ fake_subprocess.script(['nginx', '-V'], stderr='nginx version: nginx/1.24.0')
+ fake_subprocess.script(['nginx', '-t'])
+ return tmp_path / 'conf.d' / NginxService.COMPRESSION_CONF_NAME
+
+ def test_writes_the_snippet_once(self, conf, fake_subprocess):
+ assert NginxService.ensure_compression()['changed'] is True
+ assert 'gzip_types' in conf.read_text()
+ assert NginxService.ensure_compression()['changed'] is False
+
+ def test_a_snippet_nginx_rejects_is_removed(self, conf, fake_subprocess):
+ fake_subprocess.script(['nginx', '-t'], returncode=1,
+ stderr='"gzip" directive is duplicate')
+ res = NginxService.ensure_compression()
+ assert res['success'] is False and 'duplicate' in res['error']
+ assert not conf.exists(), 'a rejected snippet would break every reload'
diff --git a/backend/tests/test_nginx_timed_log.py b/backend/tests/test_nginx_timed_log.py
index 53297214..17fd4b22 100644
--- a/backend/tests/test_nginx_timed_log.py
+++ b/backend/tests/test_nginx_timed_log.py
@@ -70,6 +70,7 @@ class TestWriteVhostDeclaresTheFormat:
conf = tmp_path / 'nginx'
for d in ('sites-available', 'sites-enabled', 'conf.d'):
(conf / d).mkdir(parents=True)
+ (conf / 'conf.d' / NginxService.COMPRESSION_CONF_NAME).write_text('# set')
monkeypatch.setattr(NginxService, 'NGINX_CONF_DIR', str(conf))
monkeypatch.setattr(NginxService, 'SITES_AVAILABLE', str(conf / 'sites-available'))
monkeypatch.setattr(NginxService, 'SITES_ENABLED', str(conf / 'sites-enabled'))
diff --git a/backend/tests/test_nginx_write_vhost.py b/backend/tests/test_nginx_write_vhost.py
index 16587775..94abd76e 100644
--- a/backend/tests/test_nginx_write_vhost.py
+++ b/backend/tests/test_nginx_write_vhost.py
@@ -28,6 +28,8 @@ def nginx(tmp_path, monkeypatch, fake_subprocess):
available.mkdir()
enabled.mkdir()
(tmp_path / 'conf.d').mkdir() # http-level snippets (timed log format)
+ # Compression already configured: its own suite covers the nginx -T probe.
+ (tmp_path / 'conf.d' / NginxService.COMPRESSION_CONF_NAME).write_text('# set')
monkeypatch.setattr(NginxService, 'NGINX_CONF_DIR', str(tmp_path))
monkeypatch.setattr(NginxService, 'SITES_AVAILABLE', str(available))
monkeypatch.setattr(NginxService, 'SITES_ENABLED', str(enabled))
diff --git a/backend/tests/test_real_nginx_vhost_syntax.py b/backend/tests/test_real_nginx_vhost_syntax.py
index c4c28774..b318c575 100644
--- a/backend/tests/test_real_nginx_vhost_syntax.py
+++ b/backend/tests/test_real_nginx_vhost_syntax.py
@@ -127,3 +127,38 @@ def test_the_harness_itself_rejects_garbage(tmp_path):
"""A wrapper that passed everything would make the suite vacuous."""
proc = _nginx_t(tmp_path, 'server { this is not nginx syntax }')
assert proc.returncode != 0
+
+
[email protected]('stock_http', [
+ '', # RHEL-shaped: no gzip at all
+ ' gzip on;\n', # Debian-shaped: gzip on in nginx.conf
+ ' gzip on;\n gzip_types text/plain;\n gzip_vary on;\n',
+])
+def test_compression_snippet_round_trips_through_real_nginx(tmp_path, stock_http):
+ """Render the snippet from the REAL `nginx -T` of a stock-shaped config,
+ include it, and the result must still pass `nginx -t` — a repeated
+ http-level gzip directive is exactly what would fail here (plan 86 §B1)."""
+ snippet_path = tmp_path / 'serverkit-compression.conf'
+ snippet_path.write_text('')
+ main = tmp_path / 'nginx.conf'
+ main.write_text(
+ f'pid {tmp_path}/nginx.pid;\n'
+ f'error_log {tmp_path}/error.log;\n'
+ 'events {}\n'
+ 'http {\n'
+ f' access_log {tmp_path}/access.log;\n'
+ + stock_http
+ + f' include {snippet_path};\n'
+ '}\n')
+ dump = subprocess.run([NGINX, '-T', '-c', str(main)],
+ capture_output=True, text=True)
+ assert dump.returncode == 0, dump.stderr
+
+ snippet = NginxService.render_compression_snippet(
+ dump.stdout, str(snippet_path))
+ snippet_path.write_text(snippet)
+
+ proc = subprocess.run([NGINX, '-t', '-c', str(main)],
+ capture_output=True, text=True)
+ assert proc.returncode == 0, f'{snippet}\n{proc.stderr}'
+ assert 'gzip_types' in snippet or 'gzip_types' in stock_http
diff --git a/backend/tests/test_restore_point_nginx.py b/backend/tests/test_restore_point_nginx.py
index f5ac1035..7d32bfbf 100644
--- a/backend/tests/test_restore_point_nginx.py
+++ b/backend/tests/test_restore_point_nginx.py
@@ -15,6 +15,10 @@ def nginx_tree(tmp_path, monkeypatch, fake_subprocess):
enabled = tmp_path / 'sites-enabled'
available.mkdir()
enabled.mkdir()
+ # Global snippets already in place, so vhost writes don't probe nginx -T.
+ (tmp_path / 'conf.d').mkdir()
+ (tmp_path / 'conf.d' / NginxService.COMPRESSION_CONF_NAME).write_text('# set')
+ monkeypatch.setattr(NginxService, 'NGINX_CONF_DIR', str(tmp_path))
monkeypatch.setattr(NginxService, 'SITES_AVAILABLE', str(available))
monkeypatch.setattr(NginxService, 'SITES_ENABLED', str(enabled))
monkeypatch.setattr(