mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
feat: custom nginx conf dir path #62
This commit is contained in:
parent
7a98c16535
commit
02fb07f6b4
12 changed files with 625 additions and 615 deletions
|
@ -5,6 +5,11 @@ JwtSecret =
|
|||
Email =
|
||||
HTTPChallengePort = 9180
|
||||
StartCmd = login
|
||||
Database = database
|
||||
CADir =
|
||||
Demo =
|
||||
GithubProxy =
|
||||
NginxConfigDir =
|
||||
|
||||
[nginx_log]
|
||||
AccessLogPath = /var/log/nginx/access.log
|
||||
|
|
2
main.go
2
main.go
|
@ -48,7 +48,7 @@ func prog(state overseer.State) {
|
|||
gin.SetMode(settings.ServerSettings.RunMode)
|
||||
|
||||
settings.Init(confPath)
|
||||
log.Printf("Nginx config dir path: %s", nginx.GetNginxConfPath(""))
|
||||
log.Printf("Nginx config dir path: %s", nginx.GetConfPath())
|
||||
if "" != settings.ServerSettings.JwtSecret {
|
||||
model.Init()
|
||||
|
||||
|
|
|
@ -110,8 +110,8 @@ func IssueCert(c *gin.Context) {
|
|||
|
||||
close(logChan)
|
||||
|
||||
sslCertificatePath := nginx.GetNginxConfPath("ssl/" + domain + "/fullchain.cer")
|
||||
sslCertificateKeyPath := nginx.GetNginxConfPath("ssl/" + domain + "/private.key")
|
||||
sslCertificatePath := nginx.GetConfPath("ssl", domain, "fullchain.cer")
|
||||
sslCertificateKeyPath := nginx.GetConfPath("ssl", domain, "private.key")
|
||||
|
||||
certModel, err := model.FirstOrCreateCert(domain)
|
||||
|
||||
|
|
|
@ -7,7 +7,6 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
@ -22,7 +21,7 @@ func GetConfigs(c *gin.Context) {
|
|||
"is_dir": "bool",
|
||||
}
|
||||
|
||||
configFiles, err := os.ReadDir(nginx.GetNginxConfPath(dir))
|
||||
configFiles, err := os.ReadDir(nginx.GetConfPath(dir))
|
||||
|
||||
if err != nil {
|
||||
ErrHandler(c, err)
|
||||
|
@ -42,7 +41,7 @@ func GetConfigs(c *gin.Context) {
|
|||
}
|
||||
case mode&os.ModeSymlink != 0: // is a symbol
|
||||
var targetPath string
|
||||
targetPath, err = os.Readlink(nginx.GetNginxConfPath(file.Name()))
|
||||
targetPath, err = os.Readlink(nginx.GetConfPath(file.Name()))
|
||||
if err != nil {
|
||||
log.Println("GetConfigs Read Symlink Error", targetPath, err)
|
||||
continue
|
||||
|
@ -77,7 +76,7 @@ func GetConfigs(c *gin.Context) {
|
|||
|
||||
func GetConfig(c *gin.Context) {
|
||||
name := c.Param("name")
|
||||
path := filepath.Join(nginx.GetNginxConfPath("/"), name)
|
||||
path := nginx.GetConfPath("/", name)
|
||||
|
||||
content, err := os.ReadFile(path)
|
||||
|
||||
|
@ -108,7 +107,7 @@ func AddConfig(c *gin.Context) {
|
|||
name := request.Name
|
||||
content := request.Content
|
||||
|
||||
path := filepath.Join(nginx.GetNginxConfPath("/"), name)
|
||||
path := nginx.GetConfPath("/", name)
|
||||
|
||||
if _, err = os.Stat(path); err == nil {
|
||||
c.JSON(http.StatusNotAcceptable, gin.H{
|
||||
|
@ -125,7 +124,7 @@ func AddConfig(c *gin.Context) {
|
|||
}
|
||||
}
|
||||
|
||||
output := nginx.ReloadNginx()
|
||||
output := nginx.Reload()
|
||||
|
||||
if output != "" && strings.Contains(output, "error") {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
|
@ -153,7 +152,7 @@ func EditConfig(c *gin.Context) {
|
|||
ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
path := filepath.Join(nginx.GetNginxConfPath("/"), name)
|
||||
path := nginx.GetConfPath("/", name)
|
||||
content := request.Content
|
||||
|
||||
origContent, err := os.ReadFile(path)
|
||||
|
@ -171,7 +170,7 @@ func EditConfig(c *gin.Context) {
|
|||
}
|
||||
}
|
||||
|
||||
output := nginx.ReloadNginx()
|
||||
output := nginx.Reload()
|
||||
|
||||
if output != "" && strings.Contains(output, "error") {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
|
|
|
@ -9,7 +9,6 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
@ -25,14 +24,14 @@ func GetDomains(c *gin.Context) {
|
|||
"modify": "time",
|
||||
}
|
||||
|
||||
configFiles, err := os.ReadDir(nginx.GetNginxConfPath("sites-available"))
|
||||
configFiles, err := os.ReadDir(nginx.GetConfPath("sites-available"))
|
||||
|
||||
if err != nil {
|
||||
ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
enabledConfig, err := os.ReadDir(filepath.Join(nginx.GetNginxConfPath("sites-enabled")))
|
||||
enabledConfig, err := os.ReadDir(nginx.GetConfPath("sites-enabled"))
|
||||
|
||||
if err != nil {
|
||||
ErrHandler(c, err)
|
||||
|
@ -86,10 +85,10 @@ func GetDomain(c *gin.Context) {
|
|||
name = rewriteName.(string)
|
||||
}
|
||||
|
||||
path := filepath.Join(nginx.GetNginxConfPath("sites-available"), name)
|
||||
path := nginx.GetConfPath("sites-available", name)
|
||||
|
||||
enabled := true
|
||||
if _, err := os.Stat(filepath.Join(nginx.GetNginxConfPath("sites-enabled"), name)); os.IsNotExist(err) {
|
||||
if _, err := os.Stat(nginx.GetConfPath("sites-enabled", name)); os.IsNotExist(err) {
|
||||
enabled = false
|
||||
}
|
||||
|
||||
|
@ -168,23 +167,23 @@ func EditDomain(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
path := filepath.Join(nginx.GetNginxConfPath("sites-available"), name)
|
||||
path := nginx.GetConfPath("sites-available", name)
|
||||
|
||||
err := os.WriteFile(path, []byte(json.Content), 0644)
|
||||
if err != nil {
|
||||
ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
enabledConfigFilePath := filepath.Join(nginx.GetNginxConfPath("sites-enabled"), name)
|
||||
enabledConfigFilePath := nginx.GetConfPath("sites-enabled", name)
|
||||
// rename the config file if needed
|
||||
if name != json.Name {
|
||||
newPath := filepath.Join(nginx.GetNginxConfPath("sites-available"), json.Name)
|
||||
newPath := nginx.GetConfPath("sites-available", json.Name)
|
||||
// recreate soft link
|
||||
log.Println(enabledConfigFilePath)
|
||||
if _, err = os.Stat(enabledConfigFilePath); err == nil {
|
||||
log.Println(enabledConfigFilePath)
|
||||
_ = os.Remove(enabledConfigFilePath)
|
||||
enabledConfigFilePath = filepath.Join(nginx.GetNginxConfPath("sites-enabled"), json.Name)
|
||||
enabledConfigFilePath = nginx.GetConfPath("sites-enabled", json.Name)
|
||||
err = os.Symlink(newPath, enabledConfigFilePath)
|
||||
|
||||
if err != nil {
|
||||
|
@ -202,10 +201,10 @@ func EditDomain(c *gin.Context) {
|
|||
|
||||
}
|
||||
|
||||
enabledConfigFilePath = filepath.Join(nginx.GetNginxConfPath("sites-enabled"), name)
|
||||
enabledConfigFilePath = nginx.GetConfPath("sites-enabled", name)
|
||||
if _, err = os.Stat(enabledConfigFilePath); err == nil {
|
||||
// Test nginx configuration
|
||||
err = nginx.TestNginxConf()
|
||||
err = nginx.TestConf()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": err.Error(),
|
||||
|
@ -214,7 +213,7 @@ func EditDomain(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
output := nginx.ReloadNginx()
|
||||
output := nginx.Reload()
|
||||
|
||||
if output != "" && strings.Contains(output, "error") {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
|
@ -228,8 +227,8 @@ func EditDomain(c *gin.Context) {
|
|||
}
|
||||
|
||||
func EnableDomain(c *gin.Context) {
|
||||
configFilePath := filepath.Join(nginx.GetNginxConfPath("sites-available"), c.Param("name"))
|
||||
enabledConfigFilePath := filepath.Join(nginx.GetNginxConfPath("sites-enabled"), c.Param("name"))
|
||||
configFilePath := nginx.GetConfPath("sites-available", c.Param("name"))
|
||||
enabledConfigFilePath := nginx.GetConfPath("sites-enabled", c.Param("name"))
|
||||
|
||||
_, err := os.Stat(configFilePath)
|
||||
|
||||
|
@ -248,7 +247,7 @@ func EnableDomain(c *gin.Context) {
|
|||
}
|
||||
|
||||
// Test nginx config, if not pass then rollback.
|
||||
err = nginx.TestNginxConf()
|
||||
err = nginx.TestConf()
|
||||
if err != nil {
|
||||
_ = os.Remove(enabledConfigFilePath)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
|
@ -257,7 +256,7 @@ func EnableDomain(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
output := nginx.ReloadNginx()
|
||||
output := nginx.Reload()
|
||||
|
||||
if output != "" && strings.Contains(output, "error") {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
|
@ -272,7 +271,7 @@ func EnableDomain(c *gin.Context) {
|
|||
}
|
||||
|
||||
func DisableDomain(c *gin.Context) {
|
||||
enabledConfigFilePath := filepath.Join(nginx.GetNginxConfPath("sites-enabled"), c.Param("name"))
|
||||
enabledConfigFilePath := nginx.GetConfPath("sites-enabled", c.Param("name"))
|
||||
|
||||
_, err := os.Stat(enabledConfigFilePath)
|
||||
|
||||
|
@ -296,7 +295,7 @@ func DisableDomain(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
output := nginx.ReloadNginx()
|
||||
output := nginx.Reload()
|
||||
|
||||
if output != "" {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
|
@ -313,8 +312,8 @@ func DisableDomain(c *gin.Context) {
|
|||
func DeleteDomain(c *gin.Context) {
|
||||
var err error
|
||||
name := c.Param("name")
|
||||
availablePath := filepath.Join(nginx.GetNginxConfPath("sites-available"), name)
|
||||
enabledPath := filepath.Join(nginx.GetNginxConfPath("sites-enabled"), name)
|
||||
availablePath := nginx.GetConfPath("sites-available", name)
|
||||
enabledPath := nginx.GetConfPath("sites-enabled", name)
|
||||
|
||||
if _, err = os.Stat(availablePath); os.IsNotExist(err) {
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
|
|
|
@ -13,7 +13,6 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -107,7 +106,7 @@ func getLogPath(control *controlStruct) (logPath string, err error) {
|
|||
switch control.Type {
|
||||
case "site":
|
||||
var config *nginx.NgxConfig
|
||||
path := filepath.Join(nginx.GetNginxConfPath("sites-available"), control.ConfName)
|
||||
path := nginx.GetConfPath("sites-available", control.ConfName)
|
||||
config, err = nginx.ParseNgxConfig(path)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "error parsing ngx config")
|
||||
|
|
|
@ -3,7 +3,6 @@ package model
|
|||
import (
|
||||
"github.com/0xJacky/Nginx-UI/server/pkg/nginx"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -38,7 +37,7 @@ func GetAutoCertList() (c []Cert) {
|
|||
db.Where("auto_cert", AutoCertEnabled).Find(&t)
|
||||
|
||||
// check if this domain is enabled
|
||||
enabledConfig, err := os.ReadDir(filepath.Join(nginx.GetNginxConfPath("sites-enabled")))
|
||||
enabledConfig, err := os.ReadDir(nginx.GetConfPath("sites-enabled"))
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
|
|
|
@ -110,7 +110,7 @@ func IssueCert(domain []string, logChan chan string, errChan chan error) {
|
|||
return
|
||||
}
|
||||
name := strings.Join(domain, "_")
|
||||
saveDir := nginx.GetNginxConfPath("ssl/" + name)
|
||||
saveDir := nginx.GetConfPath("ssl/" + name)
|
||||
if _, err = os.Stat(saveDir); os.IsNotExist(err) {
|
||||
err = os.MkdirAll(saveDir, 0755)
|
||||
if err != nil {
|
||||
|
@ -142,7 +142,7 @@ func IssueCert(domain []string, logChan chan string, errChan chan error) {
|
|||
close(errChan)
|
||||
logChan <- "Reloading nginx"
|
||||
|
||||
nginx.ReloadNginx()
|
||||
nginx.Reload()
|
||||
|
||||
logChan <- "Finished"
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package nginx
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/0xJacky/Nginx-UI/server/settings"
|
||||
"log"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
@ -9,7 +10,7 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
func TestNginxConf() error {
|
||||
func TestConf() error {
|
||||
out, err := exec.Command("nginx", "-t").CombinedOutput()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
@ -22,7 +23,7 @@ func TestNginxConf() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func ReloadNginx() string {
|
||||
func Reload() string {
|
||||
out, err := exec.Command("nginx", "-s", "reload").CombinedOutput()
|
||||
|
||||
if err != nil {
|
||||
|
@ -38,19 +39,26 @@ func ReloadNginx() string {
|
|||
return ""
|
||||
}
|
||||
|
||||
func GetNginxConfPath(dir string) string {
|
||||
func GetConfPath(dir ...string) string {
|
||||
|
||||
var confPath string
|
||||
|
||||
if settings.ServerSettings.NginxConfigDir == "" {
|
||||
out, err := exec.Command("nginx", "-V").CombinedOutput()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
log.Println("nginx.GetConfPath exec.Command error", err)
|
||||
return ""
|
||||
}
|
||||
// fmt.Printf("%s\n", out)
|
||||
|
||||
r, _ := regexp.Compile("--conf-path=(.*)/(.*.conf)")
|
||||
match := r.FindStringSubmatch(string(out))
|
||||
if len(match) < 1 {
|
||||
log.Println("nginx.GetConfPath len(match) < 1")
|
||||
return ""
|
||||
}
|
||||
confPath = r.FindStringSubmatch(string(out))[1]
|
||||
} else {
|
||||
confPath = settings.ServerSettings.NginxConfigDir
|
||||
}
|
||||
|
||||
confPath := r.FindStringSubmatch(string(out))[1]
|
||||
|
||||
// fmt.Println(confPath)
|
||||
|
||||
return filepath.Join(confPath, dir)
|
||||
return filepath.Join(confPath, filepath.Join(dir...))
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@ type Server struct {
|
|||
Demo bool `json:"demo"`
|
||||
PageSize int `json:"page_size"`
|
||||
GithubProxy string `json:"github_proxy"`
|
||||
NginxConfigDir string `json:"nginx_config_dir"`
|
||||
}
|
||||
|
||||
type NginxLog struct {
|
||||
|
|
|
@ -46,7 +46,7 @@ func TestAcme(t *testing.T) {
|
|||
"install",
|
||||
"--log",
|
||||
"--home", "/usr/local/acme.sh",
|
||||
"--cert-home", nginx.GetNginxConfPath("ssl")).
|
||||
"--cert-home", nginx.GetConfPath("ssl")).
|
||||
CombinedOutput()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
|
|
@ -20,14 +20,14 @@ func TestCert(t *testing.T) {
|
|||
}
|
||||
fmt.Printf("%s\n", out)
|
||||
|
||||
_, err = os.Stat(nginx.GetNginxConfPath("ssl/test.ojbk.me/fullchain.cer"))
|
||||
_, err = os.Stat(nginx.GetConfPath("ssl/test.ojbk.me/fullchain.cer"))
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
log.Println("[found]", "fullchain.cer")
|
||||
_, err = os.Stat(nginx.GetNginxConfPath("ssl/test.ojbk.me/test.ojbk.me.key"))
|
||||
_, err = os.Stat(nginx.GetConfPath("ssl/test.ojbk.me/test.ojbk.me.key"))
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue