fix nginx domain部署失败
This commit is contained in:
+18
-11
@@ -2575,7 +2575,7 @@ func deployNginxSSLConfig(domain string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// HandleNginxSetupSSL handles POST /api/child/nginx/setup-ssl
|
// HandleNginxSetupSSL handles POST /api/child/nginx/setup-ssl
|
||||||
// Deploys full nginx.conf (if provided) or SSL 443 server block for a domain.
|
// Deploys nginx.conf + domain server block to servers/{domain}.conf.
|
||||||
func (h *ManageHandler) HandleNginxSetupSSL(w http.ResponseWriter, r *http.Request) {
|
func (h *ManageHandler) HandleNginxSetupSSL(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
writeError(w, http.StatusMethodNotAllowed, "Method not allowed")
|
writeError(w, http.StatusMethodNotAllowed, "Method not allowed")
|
||||||
@@ -2589,6 +2589,7 @@ func (h *ManageHandler) HandleNginxSetupSSL(w http.ResponseWriter, r *http.Reque
|
|||||||
var req struct {
|
var req struct {
|
||||||
Domain string `json:"domain"`
|
Domain string `json:"domain"`
|
||||||
NginxConfig string `json:"nginx_config"`
|
NginxConfig string `json:"nginx_config"`
|
||||||
|
DomainConfig string `json:"domain_config"`
|
||||||
}
|
}
|
||||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.Domain == "" {
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.Domain == "" {
|
||||||
writeError(w, http.StatusBadRequest, "domain is required")
|
writeError(w, http.StatusBadRequest, "domain is required")
|
||||||
@@ -2597,9 +2598,7 @@ func (h *ManageHandler) HandleNginxSetupSSL(w http.ResponseWriter, r *http.Reque
|
|||||||
|
|
||||||
domain := strings.ToLower(strings.TrimSpace(req.Domain))
|
domain := strings.ToLower(strings.TrimSpace(req.Domain))
|
||||||
|
|
||||||
if req.NginxConfig != "" {
|
confDir := "/usr/local/nginx"
|
||||||
// Full nginx.conf provided by master — write it directly
|
|
||||||
confDir := "/usr/local/nginx/conf"
|
|
||||||
if _, err := os.Stat(confDir); err != nil {
|
if _, err := os.Stat(confDir); err != nil {
|
||||||
confDir = "/etc/nginx"
|
confDir = "/etc/nginx"
|
||||||
}
|
}
|
||||||
@@ -2608,21 +2607,29 @@ func (h *ManageHandler) HandleNginxSetupSSL(w http.ResponseWriter, r *http.Reque
|
|||||||
os.MkdirAll(filepath.Join(confDir, "cert"), 0755)
|
os.MkdirAll(filepath.Join(confDir, "cert"), 0755)
|
||||||
os.MkdirAll(filepath.Join(confDir, "servers"), 0755)
|
os.MkdirAll(filepath.Join(confDir, "servers"), 0755)
|
||||||
|
|
||||||
|
if req.NginxConfig != "" {
|
||||||
|
// Deploy base nginx.conf
|
||||||
mainConf := filepath.Join(confDir, "nginx.conf")
|
mainConf := filepath.Join(confDir, "nginx.conf")
|
||||||
|
|
||||||
// Backup existing config
|
|
||||||
if content, err := os.ReadFile(mainConf); err == nil {
|
if content, err := os.ReadFile(mainConf); err == nil {
|
||||||
backupPath := mainConf + ".bak." + time.Now().Format("20060102150405")
|
os.WriteFile(mainConf+".bak."+time.Now().Format("20060102150405"), content, 0644)
|
||||||
os.WriteFile(backupPath, content, 0644)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.WriteFile(mainConf, []byte(req.NginxConfig), 0644); err != nil {
|
if err := os.WriteFile(mainConf, []byte(req.NginxConfig), 0644); err != nil {
|
||||||
writeError(w, http.StatusInternalServerError, fmt.Sprintf("Failed to write nginx.conf: %v", err))
|
writeError(w, http.StatusInternalServerError, fmt.Sprintf("Failed to write nginx.conf: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Printf("[Manage] Full nginx.conf deployed for domain %s at %s", domain, mainConf)
|
log.Printf("[Manage] nginx.conf deployed at %s", mainConf)
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.DomainConfig != "" {
|
||||||
|
// Deploy domain-specific server block to servers/{domain}.conf
|
||||||
|
domainConfPath := filepath.Join(confDir, "servers", domain+".conf")
|
||||||
|
if err := os.WriteFile(domainConfPath, []byte(req.DomainConfig), 0644); err != nil {
|
||||||
|
writeError(w, http.StatusInternalServerError, fmt.Sprintf("Failed to write domain config: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("[Manage] Domain config deployed at %s", domainConfPath)
|
||||||
} else {
|
} else {
|
||||||
// Fallback: legacy behavior — generate SSL server block snippet
|
// Fallback: legacy behavior
|
||||||
deployNginxSSLConfig(domain)
|
deployNginxSSLConfig(domain)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user