nginx.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package nginx
  2. import (
  3. "os"
  4. "strings"
  5. "sync"
  6. "time"
  7. "github.com/0xJacky/Nginx-UI/internal/docker"
  8. "github.com/0xJacky/Nginx-UI/settings"
  9. )
  10. var (
  11. mutex sync.Mutex
  12. lastStdOut string
  13. lastStdErr error
  14. )
  15. // TestConfig tests the nginx config
  16. func TestConfig() (stdOut string, stdErr error) {
  17. mutex.Lock()
  18. defer mutex.Unlock()
  19. if settings.NginxSettings.TestConfigCmd != "" {
  20. return execShell(settings.NginxSettings.TestConfigCmd)
  21. }
  22. return execCommand("nginx", "-t")
  23. }
  24. // Reload reloads the nginx
  25. func Reload() (stdOut string, stdErr error) {
  26. mutex.Lock()
  27. defer mutex.Unlock()
  28. if settings.NginxSettings.ReloadCmd != "" {
  29. return execShell(settings.NginxSettings.ReloadCmd)
  30. }
  31. return execCommand("nginx", "-s", "reload")
  32. }
  33. // Restart restarts the nginx
  34. func Restart() {
  35. mutex.Lock()
  36. defer mutex.Unlock()
  37. // fix(docker): nginx restart always output network error
  38. time.Sleep(500 * time.Millisecond)
  39. if settings.NginxSettings.RestartCmd != "" {
  40. lastStdOut, lastStdErr = execShell(settings.NginxSettings.RestartCmd)
  41. return
  42. }
  43. pidPath := GetPIDPath()
  44. daemon := GetSbinPath()
  45. lastStdOut, lastStdErr = execCommand("start-stop-daemon", "--stop", "--quiet", "--oknodo", "--retry=TERM/30/KILL/5", "--pidfile", pidPath)
  46. if lastStdErr != nil {
  47. return
  48. }
  49. if daemon == "" {
  50. lastStdOut, lastStdErr = execCommand("nginx")
  51. return
  52. }
  53. lastStdOut, lastStdErr = execCommand("start-stop-daemon", "--start", "--quiet", "--pidfile", pidPath, "--exec", daemon)
  54. return
  55. }
  56. // GetLastOutput returns the last output of the nginx command
  57. func GetLastOutput() (stdOut string, stdErr error) {
  58. mutex.Lock()
  59. defer mutex.Unlock()
  60. return lastStdOut, lastStdErr
  61. }
  62. // GetModulesPath returns the nginx modules path
  63. func GetModulesPath() string {
  64. // First try to get from nginx -V output
  65. stdOut, stdErr := execCommand("nginx", "-V")
  66. if stdErr != nil {
  67. return ""
  68. }
  69. if stdOut != "" {
  70. // Look for --modules-path in the output
  71. if strings.Contains(stdOut, "--modules-path=") {
  72. parts := strings.Split(stdOut, "--modules-path=")
  73. if len(parts) > 1 {
  74. // Extract the path
  75. path := strings.Split(parts[1], " ")[0]
  76. // Remove quotes if present
  77. path = strings.Trim(path, "\"")
  78. return path
  79. }
  80. }
  81. }
  82. // Default path if not found
  83. return "/usr/lib/nginx/modules"
  84. }
  85. func IsNginxRunning() bool {
  86. pidPath := GetPIDPath()
  87. switch settings.NginxSettings.RunningInAnotherContainer() {
  88. case true:
  89. return docker.StatPath(pidPath)
  90. case false:
  91. if fileInfo, err := os.Stat(pidPath); err != nil || fileInfo.Size() == 0 {
  92. return false
  93. }
  94. return true
  95. }
  96. return false
  97. }