This commit is contained in:
Alexey Berezhok
2026-07-13 01:18:16 +03:00
parent 10cd2de901
commit 92c186f7da
5 changed files with 220 additions and 6 deletions

View File

@@ -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 <a@bayrepo.ru> - 0.0.1-1

View File

@@ -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 %}";

View File

@@ -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 %}

View File

@@ -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)

View File

@@ -57,6 +57,9 @@
<field name="nginx_mod_rewrite_plugin_menu_global_module_enable">
<input type="checkbox" name="nginx_mod_rewrite_plugin_menu_global_module_enable"/>
</field>
<field name="nginx_mod_rewrite_plugin_menu_debug_enable">
<input type="checkbox" name="nginx_mod_rewrite_plugin_menu_debug_enable"/>
</field>
</form>
</metadata>
<lang name="ru">
@@ -83,7 +86,8 @@
<messages name="nginx_mod_rewrite_plugin_menu_settings">
<msg name="title">Глобальные настройки mod_rewrite для nginx</msg>
<msg name="nginx_mod_rewrite_plugin_menu_module_enable">Добавить модуль в nginx</msg>
<msg name="nginx_mod_rewrite_plugin_menu_global_module_enable">Включать глобально для всех location сайта</msg>
<msg name="nginx_mod_rewrite_plugin_menu_global_module_enable">Использовать шаблон для настройки сайта</msg>
<msg name="nginx_mod_rewrite_plugin_menu_debug_enable">Включить вывод дополнительной ифнормации в лог</msg>
<msg name="msg_create">Сохранить</msg>
</messages>
</lang>