refactor: improve nginx config generation

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
dw-0
2024-05-20 11:35:43 +02:00
parent 01afe1fe77
commit b3df3e7b5c
4 changed files with 56 additions and 30 deletions

View File

@@ -0,0 +1,12 @@
# ======================================================================= #
# Copyright (C) 2020 - 2024 Dominik Willner <th33xitus@gmail.com> #
# #
# This file is part of KIAUH - Klipper Installation And Update Helper #
# https://github.com/dw-0/kiauh #
# #
# This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= #
from pathlib import Path
MODULE_PATH = Path(__file__).resolve().parent

View File

@@ -0,0 +1,95 @@
server {
listen %PORT%;
# uncomment the next line to activate IPv6
# listen [::]:%PORT%;
access_log /var/log/nginx/%NAME%-access.log;
error_log /var/log/nginx/%NAME%-error.log;
# disable this section on smaller hardware like a pi zero
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_proxied expired no-cache no-store private auth;
gzip_comp_level 4;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css text/xml text/javascript application/javascript application/x-javascript application/json application/xml;
# web_path from %NAME% static files
root %ROOT_DIR%;
index index.html;
server_name _;
# disable max upload size checks
client_max_body_size 0;
# disable proxy request buffering
proxy_request_buffering off;
location / {
try_files $uri $uri/ /index.html;
}
location = /index.html {
add_header Cache-Control "no-store, no-cache, must-revalidate";
}
location /websocket {
proxy_pass http://apiserver/websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 86400;
}
location ~ ^/(printer|api|access|machine|server)/ {
proxy_pass http://apiserver$request_uri;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Scheme $scheme;
}
location /webcam/ {
postpone_output 0;
proxy_buffering off;
proxy_ignore_headers X-Accel-Buffering;
access_log off;
error_log off;
proxy_pass http://mjpgstreamer1/;
}
location /webcam2/ {
postpone_output 0;
proxy_buffering off;
proxy_ignore_headers X-Accel-Buffering;
access_log off;
error_log off;
proxy_pass http://mjpgstreamer2/;
}
location /webcam3/ {
postpone_output 0;
proxy_buffering off;
proxy_ignore_headers X-Accel-Buffering;
access_log off;
error_log off;
proxy_pass http://mjpgstreamer3/;
}
location /webcam4/ {
postpone_output 0;
proxy_buffering off;
proxy_ignore_headers X-Accel-Buffering;
access_log off;
error_log off;
proxy_pass http://mjpgstreamer4/;
}
}

View File

@@ -12,6 +12,7 @@ from typing import List
from components.klipper.klipper import Klipper
from components.moonraker.moonraker import Moonraker
from components.webui_client import MODULE_PATH
from components.webui_client.base_data import (
BaseWebClient,
BaseWebClientConfig,
@@ -34,18 +35,15 @@ from components.webui_client.client_utils import (
)
from core.instance_manager.instance_manager import InstanceManager
from core.settings.kiauh_settings import KiauhSettings
from utils import NGINX_SITES_AVAILABLE, NGINX_SITES_ENABLED
from utils.common import check_install_dependencies
from utils.config_utils import add_config_section
from utils.fs_utils import (
copy_common_vars_nginx_cfg,
copy_upstream_nginx_cfg,
create_nginx_cfg,
create_symlink,
get_next_free_port,
is_valid_port,
read_ports_from_nginx_configs,
remove_file,
unzip,
)
from utils.input_utils import get_confirm, get_number_input
@@ -54,7 +52,6 @@ from utils.sys_utils import (
cmd_sysctl_service,
download_file,
get_ipv4_addr,
set_nginx_permissions,
)
@@ -141,7 +138,15 @@ def install_client(client: BaseWebClient) -> None:
copy_upstream_nginx_cfg()
copy_common_vars_nginx_cfg()
create_client_nginx_cfg(client, port)
create_nginx_cfg(
display_name=client.display_name,
cfg_name=client.name,
template_src=MODULE_PATH.joinpath("assets/nginx_cfg"),
PORT=port,
ROOT_DIR=client.client_dir,
NAME=client.name,
)
if kl_instances:
symlink_webui_nginx_log(kl_instances)
cmd_sysctl_service("nginx", "restart")
@@ -190,20 +195,3 @@ def update_client(client: BaseWebClient) -> None:
if client.client == WebClientType.MAINSAIL:
restore_mainsail_config_json()
def create_client_nginx_cfg(client: BaseWebClient, port: int) -> None:
display_name = client.display_name
root_dir = client.client_dir
source = NGINX_SITES_AVAILABLE.joinpath(client.name)
target = NGINX_SITES_ENABLED.joinpath(client.name)
try:
Logger.print_status(f"Creating NGINX config for {display_name} ...")
remove_file(Path("/etc/nginx/sites-enabled/default"), True)
create_nginx_cfg(client.name, port, root_dir)
create_symlink(source, target, True)
set_nginx_permissions()
Logger.print_ok(f"NGINX config for {display_name} successfully created.")
except Exception:
Logger.print_error(f"Creating NGINX config for {display_name} failed!")
raise