From 92c186f7da952f72630fe4161766b5799f751b71 Mon Sep 17 00:00:00 2001 From: Alexey Berezhok Date: Mon, 13 Jul 2026 01:18:16 +0300 Subject: [PATCH] Fixes --- ...nager-plugin-nginx_mod_rewrite_plugin.spec | 5 +- payload/nginx-vhosts-ssl.template.patch | 26 +++ payload/nginx-vhosts.template.patch | 33 ++++ .../mgr5/addon/nginx_form_rewrite_plugin.py | 156 +++++++++++++++++- .../ispmgr_mod_nginx_mod_rewrite_plugin.xml | 6 +- 5 files changed, 220 insertions(+), 6 deletions(-) create mode 100644 payload/nginx-vhosts-ssl.template.patch create mode 100644 payload/nginx-vhosts.template.patch diff --git a/ispmanager-plugin-nginx_mod_rewrite_plugin.spec b/ispmanager-plugin-nginx_mod_rewrite_plugin.spec index d286895..83f9e3d 100644 --- a/ispmanager-plugin-nginx_mod_rewrite_plugin.spec +++ b/ispmanager-plugin-nginx_mod_rewrite_plugin.spec @@ -24,6 +24,8 @@ mkdir -p %{buildroot}/usr/local/ispmanager-mod_rewrite/utils wget -O %{buildroot}/usr/local/ispmanager-mod_rewrite/mod_rewrite.zip https://github.com/bayrepo/ngx_http_apache_rewrite_module/archive/refs/heads/main.zip cp utils/build_mod_rewrite.py %{buildroot}/usr/local/ispmanager-mod_rewrite/utils mkdir -p %{buildroot}/usr/local/ispmanager-mod_rewrite/settings +mkdir -p %{buildroot}/usr/local/ispmanager-mod_rewrite/patches +cp payload/* %{buildroot}/usr/local/ispmanager-mod_rewrite/patches/ %triggerin -- nginx /usr/local/ispmanager-mod_rewrite/utils/build_mod_rewrite.py @@ -53,7 +55,8 @@ fi %attr(644, root, root) /usr/local/mgr5/skins/icons/mod_rewrite_menu.svg %attr(644, root, root) /usr/local/mgr5/skins/icons/mod_rewrite_tool.svg %attr(644, root, root) /usr/local/mgr5/skins/common/plugin-logo/ispmanager-plugin-nginx_mod_rewrite_plugin.png - +%attr(600, root, root) /usr/local/ispmanager-mod_rewrite/patches/nginx-vhosts.template.patch +%attr(600, root, root) /usr/local/ispmanager-mod_rewrite/patches/nginx-vhosts-ssl.template.patch %changelog * Mon Jun 29 2026 Alexey BayRepo - 0.0.1-1 diff --git a/payload/nginx-vhosts-ssl.template.patch b/payload/nginx-vhosts-ssl.template.patch new file mode 100644 index 0000000..78f0c49 --- /dev/null +++ b/payload/nginx-vhosts-ssl.template.patch @@ -0,0 +1,26 @@ +--- /usr/local/mgr5/etc/templates/default/nginx-vhosts-ssl.template 2026-06-16 09:04:25.000000000 +0300 ++++ /usr/local/mgr5/etc/templates/nginx-vhosts-ssl.template 2026-07-12 00:10:35.487428277 +0300 +@@ -85,7 +85,13 @@ server { + {% if THIS_BLOCK_FOR_REMOVE_EXPIRES %} + expires [% $EXPIRES_VALUE %]; + {% endif %} ++{% if $NGINX_MOD_REWRITE_PLUGIN_SITE_EDIT_ENABLE == on %} ++ HtaccessEnable on; ++{% endif %} + location / { ++{% if $NGINX_MOD_REWRITE_PLUGIN_SITE_EDIT_ENABLE == on %} ++ RewriteEngine on; ++{% endif %} + {% if $NGINX_PROXY == proxy_panel %} + try_files /does_not_exists @ispmgr; + {% else %} +@@ -163,6 +169,9 @@ server { + {% endif %} + {% if $REDIRECT_TO_PHPFPM == on %} + location @php { ++{% if $NGINX_MOD_REWRITE_PLUGIN_SITE_EDIT_ENABLE == on %} ++ RewriteEngine on; ++{% endif %} + include {% $INCLUDE_DYNAMIC_RESOURCE_PATH %}; + fastcgi_index index.php; + fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f {% $EMAIL %}"; diff --git a/payload/nginx-vhosts.template.patch b/payload/nginx-vhosts.template.patch new file mode 100644 index 0000000..b0dde81 --- /dev/null +++ b/payload/nginx-vhosts.template.patch @@ -0,0 +1,33 @@ +--- /usr/local/mgr5/etc/templates/default/nginx-vhosts.template 2026-06-16 09:04:25.000000000 +0300 ++++ /usr/local/mgr5/etc/templates/nginx-vhosts.template 2026-07-12 00:08:24.172873945 +0300 +@@ -76,7 +76,13 @@ server { + {% if THIS_BLOCK_FOR_REMOVE_EXPIRES %} + expires [% $EXPIRES_VALUE %]; + {% endif %} ++{% if $NGINX_MOD_REWRITE_PLUGIN_SITE_EDIT_ENABLE == on %} ++ HtaccessEnable on; ++{% endif %} + location / { ++{% if $NGINX_MOD_REWRITE_PLUGIN_SITE_EDIT_ENABLE == on %} ++ RewriteEngine on; ++{% endif %} + {% if $NGINX_PROXY == proxy_panel %} + try_files /does_not_exists @proxy_redirect; + {% else %} +@@ -154,6 +160,9 @@ server { + {% endif %} + {% if $REDIRECT_TO_PHPFPM == on %} + location @php { ++{% if $NGINX_MOD_REWRITE_PLUGIN_SITE_EDIT_ENABLE == on %} ++ RewriteEngine on; ++{% endif %} + include {% $INCLUDE_DYNAMIC_RESOURCE_PATH %}; + fastcgi_index index.php; + fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f {% $EMAIL %}"; +@@ -165,5 +174,5 @@ server { + {% endif %} + } + {% if $SSL == on %} +-{% import etc/templates/default/nginx-vhosts-ssl.template %} ++{% import etc/templates/nginx-vhosts-ssl.template %} + {% endif %} diff --git a/usr/local/mgr5/addon/nginx_form_rewrite_plugin.py b/usr/local/mgr5/addon/nginx_form_rewrite_plugin.py index f1f5cb0..f86b977 100755 --- a/usr/local/mgr5/addon/nginx_form_rewrite_plugin.py +++ b/usr/local/mgr5/addon/nginx_form_rewrite_plugin.py @@ -6,6 +6,8 @@ from sys import stdin import re import datetime from xml.dom import minidom +import shutil +import subprocess CONFIG_PATH = "/usr/share/nginx/modules/ngx_http_apache_rewrite_module.conf" MODULE_LINE = 'load_module "/usr/lib64/nginx/modules/ngx_http_apache_rewrite_module.so";' @@ -13,6 +15,9 @@ MODULE_PATTERN = re.compile(r'^\s*load_module\s+"\/usr\/lib64\/nginx\/modules\/n LOG_PATH = "/usr/local/mgr5/var/nginx-mod-rewrite-plugin-addon.log" def _log(msg: str) -> None: + debug = read_global_config({'param_name': 'debug'}) + if debug not in ("1", "on"): + return try: with open(LOG_PATH, 'a') as log_file: log_file.write(f"{datetime.datetime.now().isoformat()} - {msg}\n") @@ -262,6 +267,90 @@ def set_nginx_rewrite_module(enable: bool): except Exception as e: _log(f"Error disabling module in config file {CONFIG_PATH}: {e}") +def make_nginx_custom_template() -> None: + tmpl = "/usr/local/mgr5/etc/templates/nginx-vhosts.template" + tmpl_ssl = "/usr/local/mgr5/etc/templates/nginx-vhosts-ssl.template" + + # If templates already exist, abort + if os.path.exists(tmpl) or os.path.exists(tmpl_ssl): + _log("Error: Custom templates already exist. Aborting make_nginx_custom_template.") + return + + default_dir = "/usr/local/mgr5/etc/templates/default" + default_tmpl = os.path.join(default_dir, "nginx-vhosts.template") + default_ssl_tmpl = os.path.join(default_dir, "nginx-vhosts-ssl.template") + target_dir = "/usr/local/mgr5/etc/templates" + + # Copy default templates if they exist + try: + if os.path.exists(default_tmpl): + shutil.copy2(default_tmpl, target_dir) + if os.path.exists(default_ssl_tmpl): + shutil.copy2(default_ssl_tmpl, target_dir) + except Exception as e: + _log(f"Error copying default templates: {e}") + # Remove any partially copied files + for f in (tmpl, tmpl_ssl): + try: + if os.path.exists(f): + os.remove(f) + except Exception: + pass + return + + # Ensure copied files exist + if not os.path.exists(tmpl) or not os.path.exists(tmpl_ssl): + _log("Error: Failed to copy one or both template files.") + for f in (tmpl, tmpl_ssl): + try: + if os.path.exists(f): + os.remove(f) + except Exception: + pass + return + + patches_dir = "/usr/local/ispmanager-mod_rewrite/patches" + patch_tmpl = os.path.join(patches_dir, "nginx-vhosts.template.patch") + patch_ssl = os.path.join(patches_dir, "nginx-vhosts-ssl.template.patch") + + def apply_patch(target, patch_file): + if not os.path.exists(patch_file): + _log(f"Patch file {patch_file} does not exist.") + return False + try: + result = subprocess.run( + ["patch", "-p1", "-i", patch_file], + cwd=target_dir, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + if result.returncode != 0: + _log(f"Patch failed for {target}: {result.stderr.decode(errors='ignore')}") + return False + return True + except Exception as e: + _log(f"Exception applying patch to {target}: {e}") + return False + + patch_success = True + if not apply_patch(tmpl, patch_tmpl): + patch_success = False + if not apply_patch(tmpl_ssl, patch_ssl): + patch_success = False + + if not patch_success: + _log("Error: One or more patches failed. Removing copied template files.") + for f in (tmpl, tmpl_ssl): + try: + if os.path.exists(f): + os.remove(f) + except Exception: + pass + return + + read_global_config({'param_name': 'templates_created', 'param_value': 'on'}) + _log("Success: Custom Nginx templates created and patched successfully.") + if __name__ == "__main__": root = etree.parse(stdin).getroot() func = os.getenv('PARAM_func') @@ -279,9 +368,11 @@ if __name__ == "__main__": out_error_xml("Access denied") nginx_mod_rewrite_plugin_menu_module_enable = os.getenv("PARAM_nginx_mod_rewrite_plugin_menu_module_enable") + nginx_mod_rewrite_plugin_menu_debug_enable = os.getenv("PARAM_nginx_mod_rewrite_plugin_menu_debug_enable") mod_enabled = check_nginx_rewrite_module() _log(f"Current module state: {'enabled' if mod_enabled else 'disabled'}") mod_rewrite_global = read_global_config({'param_name': 'global_mod_rewrite_enable'}) + debug = read_global_config({'param_name': 'debug'}) if os.getenv('PARAM_clicked_button') == 'ok' and nginx_mod_rewrite_plugin_menu_module_enable is not None: if nginx_mod_rewrite_plugin_menu_module_enable == "on" and not mod_enabled: @@ -312,11 +403,22 @@ if __name__ == "__main__": new_element = etree.Element("nginx_mod_rewrite_plugin_menu_global_module_enable") new_element.text = "on" root.append(new_element) + make_nginx_custom_template() elif nginx_mod_rewrite_plugin_menu_global_module_enable == "off" and mod_rewrite_global in ("1", "on"): read_global_config({'param_name': 'global_mod_rewrite_enable', 'param_value': 'off'}) new_element = etree.Element("nginx_mod_rewrite_plugin_menu_global_module_enable") new_element.text = "off" root.append(new_element) + is_template = read_global_config({'param_name': 'templates_created'}) + if is_template in ("1", "on"): + tmpl = "/usr/local/mgr5/etc/templates/nginx-vhosts.template" + tmpl_ssl = "/usr/local/mgr5/etc/templates/nginx-vhosts-ssl.template" + for f in (tmpl, tmpl_ssl): + try: + if os.path.exists(f): + os.remove(f) + except Exception: + pass else: new_element = etree.Element("nginx_mod_rewrite_plugin_menu_global_module_enable") new_element.text = "on" if mod_rewrite_global in ("1", "on") else "off" @@ -326,6 +428,26 @@ if __name__ == "__main__": new_element.text = "on" if mod_rewrite_global in ("1", "on") else "off" root.append(new_element) + if os.getenv('PARAM_clicked_button') == 'ok' and nginx_mod_rewrite_plugin_menu_debug_enable is not None: + if nginx_mod_rewrite_plugin_menu_debug_enable == "on" and debug not in ("1", "on"): + read_global_config({'param_name': 'debug', 'param_value': 'on'}) + new_element = etree.Element("nginx_mod_rewrite_plugin_menu_debug_enable") + new_element.text = "on" + root.append(new_element) + elif nginx_mod_rewrite_plugin_menu_debug_enable == "off" and debug in ("1", "on"): + read_global_config({'param_name': 'debug', 'param_value': 'off'}) + new_element = etree.Element("nginx_mod_rewrite_plugin_menu_debug_enable") + new_element.text = "off" + root.append(new_element) + else: + new_element = etree.Element("nginx_mod_rewrite_plugin_menu_debug_enable") + new_element.text = "on" if debug in ("1", "on") else "off" + root.append(new_element) + else: + new_element = etree.Element("nginx_mod_rewrite_plugin_menu_debug_enable") + new_element.text = "on" if debug in ("1", "on") else "off" + root.append(new_element) + elif func == "webdomain.edit": _log("webdomain.edit handler") if int(user_level) < 16: @@ -340,15 +462,41 @@ if __name__ == "__main__": mod_rewrite_global = read_global_config({'param_name': 'global_mod_rewrite_enable'}) if nginx_mod_rewrite_plugin_site_edit_enable == "on": if mod_rewrite_global in ("1", "on"): - _log("Global state enabled") - new_element = etree.Element("nginx_mod_rewrite_plugin_site_edit_enable") - new_element.text = "on" - root.append(new_element) + _log("Template state enabled") + new_element = etree.Element("nginx_mod_rewrite_plugin_site_edit_enable") + new_element.text = "on" + root.append(new_element) + else: + new_element = etree.Element("nginx_mod_rewrite_plugin_site_edit_enable") + new_element.text = "on" + root.append(new_element) + if os.path.isdir(path_to_global_config): + mod_file_path = os.path.join(path_to_global_config, "mod_rewrite.conf") + try: + with open(mod_file_path, "w", encoding="utf-8") as f: + f.write("GlobalLocationRewriteEngine on;\nHtaccessEnable on;\n") + _log(f"Created/overwritten mod_rewrite.conf at {mod_file_path}") + except Exception as e: + _log(f"Failed to write mod_rewrite.conf at {mod_file_path}: {e}") + else: + _log(f"Path {path_to_global_config} is not a directory or does not exist") site_state({ domain: "on"}) else: _log("mod_rewrite should be disbled") if mod_rewrite_global in ("1", "on"): _log("Global state enabled") + if os.path.isdir(path_to_global_config): + mod_file_path = os.path.join(path_to_global_config, "mod_rewrite.conf") + if os.path.isfile(mod_file_path): + try: + os.remove(mod_file_path) + _log(f"Deleted existing mod_rewrite.conf at {mod_file_path}") + except Exception as e: + _log(f"Failed to delete mod_rewrite.conf at {mod_file_path}: {e}") + else: + _log(f"mod_rewrite.conf at {mod_file_path} does not exist; nothing to delete") + else: + _log(f"Path {path_to_global_config} is not a directory or does not exist") new_element = etree.Element("nginx_mod_rewrite_plugin_site_edit_enable") new_element.text = "off" root.append(new_element) diff --git a/usr/local/mgr5/etc/xml/ispmgr_mod_nginx_mod_rewrite_plugin.xml b/usr/local/mgr5/etc/xml/ispmgr_mod_nginx_mod_rewrite_plugin.xml index b1131e9..569ac11 100644 --- a/usr/local/mgr5/etc/xml/ispmgr_mod_nginx_mod_rewrite_plugin.xml +++ b/usr/local/mgr5/etc/xml/ispmgr_mod_nginx_mod_rewrite_plugin.xml @@ -57,6 +57,9 @@ + + + @@ -83,7 +86,8 @@ Глобальные настройки mod_rewrite для nginx Добавить модуль в nginx - Включать глобально для всех location сайта + Использовать шаблон для настройки сайта + Включить вывод дополнительной ифнормации в лог Сохранить