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.

Move nginx private URLs to location snippets

Juan Denise0492f54f07d
diff --git a/backend/app/services/nginx_service.py b/backend/app/services/nginx_service.py
index f322a44d..3d2d693e 100644
--- a/backend/app/services/nginx_service.py
+++ b/backend/app/services/nginx_service.py
@@ -261,28 +261,11 @@ location = /wordpress {{
# This file is auto-generated. Do not edit manually.
# Generated at: {timestamp}
-server {{
- listen 80;
- listen [::]:80;
-
- # Match requests to server IP for private URLs
- # Note: Do NOT use default_server here - serverkit.conf handles that
- server_name _;
-
- access_log /var/log/nginx/private-urls.access.log;
- error_log /var/log/nginx/private-urls.error.log;
-
{locations}
- # Fallback for unknown slugs under /p/
- location /p/ {{
- return 404;
- }}
-
- # Root location - return 404 for unmatched requests
- location / {{
- return 404;
- }}
+# Fallback for unknown slugs under /p/
+location /p/ {{
+ return 404;
}}
'''
@@ -780,47 +763,33 @@ server {{
)
locations.append(location)
- # Generate full config
+ # Generate full config (location-only snippet, included by serverkit.conf)
+ cls._ensure_locations_dir()
if locations:
config = cls.PRIVATE_URL_MAIN_CONFIG.format(
timestamp=datetime.utcnow().isoformat(),
locations='\n'.join(locations)
)
else:
- # No private URLs - create minimal config
- # Note: Do NOT use default_server here - serverkit.conf handles that
+ # No private URLs - create minimal fallback
config = f'''# ServerKit Private URL Routes
# This file is auto-generated. Do not edit manually.
# Generated at: {datetime.utcnow().isoformat()}
# No private URLs configured
-server {{
- listen 80;
- listen [::]:80;
- server_name _;
-
- location /p/ {{
- return 404;
- }}
-
- location / {{
- return 404;
- }}
+location /p/ {{
+ return 404;
}}
'''
- # Write config file
- config_path = os.path.join(cls.SITES_AVAILABLE, cls.PRIVATE_URL_CONFIG_NAME)
+ # Write location snippet to serverkit-locations dir
+ config_path = os.path.join(cls.LOCATIONS_DIR, f'{cls.PRIVATE_URL_CONFIG_NAME}.conf')
process = run_privileged(['tee', config_path], input=config)
if process.returncode != 0:
return {'success': False, 'error': f'Failed to write config: {process.stderr}'}
- # Enable the site if not already enabled
- enabled_path = os.path.join(cls.SITES_ENABLED, cls.PRIVATE_URL_CONFIG_NAME)
- if not os.path.exists(enabled_path):
- result = run_privileged(['ln', '-sf', config_path, enabled_path])
- if result.returncode != 0:
- return {'success': False, 'error': f'Failed to enable config: {result.stderr}'}
+ # Clean up legacy separate server block if it exists
+ cls._remove_legacy_site(cls.PRIVATE_URL_CONFIG_NAME)
# Reload Nginx
reload_result = cls.reload()
@@ -844,10 +813,37 @@ server {{
Returns:
Dict with exists, enabled, content, and URL count
"""
- return cls.get_site_config(cls.PRIVATE_URL_CONFIG_NAME)
+ return cls.get_location_config(cls.PRIVATE_URL_CONFIG_NAME)
# ==================== GITEA CONFIGURATION ====================
+ @classmethod
+ def get_location_config(cls, name: str) -> Dict:
+ """Get a location snippet config from the serverkit-locations directory.
+
+ Args:
+ name: Config name (without .conf extension)
+
+ Returns:
+ Dict with exists, enabled, content, and path
+ """
+ config_path = os.path.join(cls.LOCATIONS_DIR, f'{name}.conf')
+ config = {
+ 'exists': os.path.isfile(config_path),
+ 'enabled': os.path.isfile(config_path), # If it exists, it's included
+ 'content': None,
+ 'path': config_path
+ }
+
+ if config['exists']:
+ try:
+ with open(config_path, 'r') as f:
+ config['content'] = f.read()
+ except Exception:
+ pass
+
+ return config
+
@classmethod
def _ensure_locations_dir(cls):
"""Ensure the serverkit-locations directory exists."""
@@ -924,7 +920,7 @@ server {{
Returns:
Dict with exists, enabled, content, and path
"""
- return cls.get_site_config(cls.GITEA_CONFIG_NAME)
+ return cls.get_location_config(cls.GITEA_CONFIG_NAME)
# ==================== WORDPRESS CONFIGURATION ====================
@@ -1009,4 +1005,4 @@ server {{
Returns:
Dict with exists, enabled, content, and path
"""
- return cls.get_site_config(cls.WORDPRESS_CONFIG_NAME)
+ return cls.get_location_config(cls.WORDPRESS_CONFIG_NAME)
diff --git a/install.sh b/install.sh
index a26f9c57..a85151c2 100644
--- a/install.sh
+++ b/install.sh
@@ -402,13 +402,14 @@ if ! grep -q "sites-enabled" /etc/nginx/nginx.conf; then
sed -i '/http {/a \ include /etc/nginx/sites-enabled/*;' /etc/nginx/nginx.conf
fi
-# Remove any nginx configs that use server_name _ (catch-all) besides serverkit.conf
+# Remove any nginx configs that use server_name _ (catch-all) besides serverkit-managed ones
# These conflict with the panel and can allow compromised apps to hijack all traffic
for conf in /etc/nginx/sites-enabled/*; do
[ -f "$conf" ] || continue
name=$(basename "$conf")
- [ "$name" = "serverkit.conf" ] && continue
- if grep -q 'server_name.*_' "$conf" 2>/dev/null; then
+ # Skip all ServerKit-managed configs
+ case "$name" in serverkit-*|serverkit.conf) continue ;; esac
+ if grep -Eq 'server_name[[:space:]]+_;' "$conf" 2>/dev/null; then
print_warning "Removing conflicting catch-all config: $name"
rm -f "/etc/nginx/sites-enabled/$name"
rm -f "/etc/nginx/sites-available/$name"
diff --git a/serverkit b/serverkit
index 06e1baeb..a3059378 100644
--- a/serverkit
+++ b/serverkit
@@ -457,13 +457,14 @@ cmd_update() {
cp -r "$INSTALL_DIR/backend/templates/"*.yaml /etc/serverkit/templates/ 2>/dev/null || true
cp -r "$INSTALL_DIR/backend/templates/"*.yml /etc/serverkit/templates/ 2>/dev/null || true
- # Remove any nginx configs that use server_name _ besides serverkit.conf
+ # Remove any nginx configs that use server_name _ besides serverkit-managed ones
mkdir -p /etc/nginx/serverkit-locations
for conf in /etc/nginx/sites-enabled/*; do
[ -f "$conf" ] || continue
name=$(basename "$conf")
- [ "$name" = "serverkit.conf" ] && continue
- if grep -q 'server_name.*_' "$conf" 2>/dev/null; then
+ # Skip all ServerKit-managed configs
+ case "$name" in serverkit-*) continue ;; esac
+ if grep -Eq 'server_name[[:space:]]+_;' "$conf" 2>/dev/null; then
print_warning "Removing conflicting catch-all config: $name"
rm -f "/etc/nginx/sites-enabled/$name"
rm -f "/etc/nginx/sites-available/$name"
@@ -939,6 +940,10 @@ cmd_doctor() {
FIXED=0
FIX_MODE="${1:-}" # --fix to auto-fix
+ if [ "$FIX_MODE" = "--fix" ]; then
+ check_root
+ fi
+
doctor_issue() {
ISSUES=$((ISSUES + 1))
print_error "$1"
@@ -986,38 +991,44 @@ cmd_doctor() {
echo ""
# ── 2. Frontend container ───────────────────────────────────
- echo "Frontend"
- echo "───────────────────────────────────────"
-
FRONTEND_RUNNING=false
FRONTEND_PORT=""
- if docker ps --format '{{.Names}}' 2>/dev/null | grep -q "$FRONTEND_CONTAINER"; then
- FRONTEND_RUNNING=true
- # Get the actual host port the container is mapped to
- FRONTEND_PORT=$(docker port "$FRONTEND_CONTAINER" 80 2>/dev/null | head -1 | grep -oP ':\K[0-9]+' || echo "")
- doctor_ok "Container running (port $FRONTEND_PORT)"
- else
- doctor_issue "Container '$FRONTEND_CONTAINER' is not running"
- if [ "$FIX_MODE" = "--fix" ]; then
- cd "$INSTALL_DIR"
- docker compose up -d 2>/dev/null && {
- sleep 3
- FRONTEND_PORT=$(docker port "$FRONTEND_CONTAINER" 80 2>/dev/null | head -1 | grep -oP ':\K[0-9]+' || echo "")
- FRONTEND_RUNNING=true
- doctor_fixed "Started frontend container (port $FRONTEND_PORT)"
- } || print_error " Could not start container"
+ if command -v docker >/dev/null 2>&1; then
+ echo "Frontend"
+ echo "───────────────────────────────────────"
+
+ if docker ps --format '{{.Names}}' 2>/dev/null | grep -q "$FRONTEND_CONTAINER"; then
+ FRONTEND_RUNNING=true
+ # Get the actual host port the container is mapped to
+ FRONTEND_PORT=$(docker port "$FRONTEND_CONTAINER" 80 2>/dev/null | head -1 | grep -oP ':\K[0-9]+' || echo "")
+ doctor_ok "Container running (port $FRONTEND_PORT)"
else
- doctor_skip "docker compose up -d"
+ doctor_issue "Container '$FRONTEND_CONTAINER' is not running"
+ if [ "$FIX_MODE" = "--fix" ]; then
+ cd "$INSTALL_DIR"
+ docker compose up -d 2>/dev/null && {
+ sleep 3
+ FRONTEND_PORT=$(docker port "$FRONTEND_CONTAINER" 80 2>/dev/null | head -1 | grep -oP ':\K[0-9]+' || echo "")
+ FRONTEND_RUNNING=true
+ doctor_fixed "Started frontend container (port $FRONTEND_PORT)"
+ } || print_error " Could not start container"
+ else
+ doctor_skip "docker compose up -d"
+ fi
fi
- fi
- if [ "$FRONTEND_RUNNING" = true ] && [ -n "$FRONTEND_PORT" ]; then
- if curl -sf "http://127.0.0.1:$FRONTEND_PORT" > /dev/null 2>&1; then
- doctor_ok "Frontend responding on port $FRONTEND_PORT"
- else
- doctor_issue "Container running but not responding on port $FRONTEND_PORT"
+ if [ "$FRONTEND_RUNNING" = true ] && [ -n "$FRONTEND_PORT" ]; then
+ if curl -sf "http://127.0.0.1:$FRONTEND_PORT" > /dev/null 2>&1; then
+ doctor_ok "Frontend responding on port $FRONTEND_PORT"
+ else
+ doctor_issue "Container running but not responding on port $FRONTEND_PORT"
+ fi
fi
+ else
+ echo "Frontend"
+ echo "───────────────────────────────────────"
+ doctor_issue "Docker not installed or not in PATH; skipping frontend checks"
fi
echo ""
@@ -1049,9 +1060,10 @@ cmd_doctor() {
for SITE_CONF in /etc/nginx/sites-enabled/*; do
[ -f "$SITE_CONF" ] || continue
SITE_NAME=$(basename "$SITE_CONF")
- [ "$SITE_NAME" = "serverkit.conf" ] && continue
+ # Skip all ServerKit-managed configs
+ case "$SITE_NAME" in serverkit-*|serverkit.conf) continue ;; esac
- if grep -q 'server_name\s*_' "$SITE_CONF" 2>/dev/null; then
+ if grep -Eq 'server_name[[:space:]]+_;' "$SITE_CONF" 2>/dev/null; then
doctor_issue "'$SITE_NAME' uses server_name _ (conflicts with panel, can hijack all traffic)"
if [ "$FIX_MODE" = "--fix" ]; then
rm -f "/etc/nginx/sites-enabled/$SITE_NAME"