增加修改sock,与Gunicorn配置相对应的按钮

This commit is contained in:
2025-09-07 12:43:09 +08:00
parent fffed99165
commit cc90d6f947
2 changed files with 173 additions and 0 deletions

View File

@@ -421,6 +421,11 @@ class NginxTab(QWidget):
self.download_site_config_btn.clicked.connect(self.download_site_config)
site_config_btn_layout.addWidget(self.download_site_config_btn)
# 修改为Unix socket连接按钮
self.modify_to_unix_socket_btn = QPushButton("修改为Unix Socket连接")
self.modify_to_unix_socket_btn.clicked.connect(self.modify_to_unix_socket)
site_config_btn_layout.addWidget(self.modify_to_unix_socket_btn)
site_config_layout.addLayout(site_config_btn_layout)
layout.addLayout(site_config_layout)
@@ -897,6 +902,63 @@ http {
self.append_output(f"添加静态文件映射失败: {error_msg}")
logger.error(f"添加静态文件映射失败: {error_msg}")
def modify_to_unix_socket(self):
"""修改站点配置文件中的proxy_pass为Unix socket连接"""
# 读取config.json文件获取项目信息
try:
with open('config.json', 'r', encoding='utf-8') as f:
config = json.load(f)
# 获取第一个服务器配置(假设只有一个服务器配置)
server_config = next(iter(config.values()))
username = server_config.get('username', '')
git_url = server_config.get('git_url', '')
# 从git_url中提取项目名.git前的值
if git_url:
# 提取git_url中最后一个/和.git之间的部分
if '/' in git_url:
project_name = git_url.split('/')[-1]
if project_name.endswith('.git'):
project_name = project_name[:-4] # 移除.git后缀
else:
project_name = 'webstatus' # 默认值
else:
project_name = 'webstatus' # 默认值
# 构建Unix socket路径
unix_socket_path = f"unix:/home/{username}/{project_name}/sock/gunicorn.sock"
# 获取当前配置内容
current_config = self.site_config_editor.toPlainText()
# 查找并替换proxy_pass的值
import re
# 使用正则表达式匹配proxy_pass行
pattern = r'(\s+proxy_pass\s+)http://[^;]+;'
replacement = f'\1{unix_socket_path};'
new_config = re.sub(pattern, replacement, current_config)
if new_config != current_config:
# 更新配置内容
self.site_config_editor.setPlainText(new_config)
self.append_output(f"已将proxy_pass修改为: {unix_socket_path}")
logger.info(f"已将proxy_pass修改为: {unix_socket_path}")
else:
# 检查是否已经是Unix socket连接
if unix_socket_path in current_config:
self.append_output("配置文件已经是Unix socket连接")
logger.info("配置文件已经是Unix socket连接")
else:
self.append_output("未找到可修改的proxy_pass配置")
logger.warning("未找到可修改的proxy_pass配置")
except Exception as e:
error_msg = str(e)
self.append_output(f"修改为Unix socket连接失败: {error_msg}")
logger.error(f"修改为Unix socket连接失败: {error_msg}")
def restart_nginx(self):
"""重启Nginx"""
if not self.ssh_client: