perf_opt.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package config
  2. import (
  3. "os"
  4. "sort"
  5. "github.com/0xJacky/Nginx-UI/internal/nginx"
  6. "github.com/pkg/errors"
  7. "github.com/tufanbarisyildirim/gonginx/config"
  8. "github.com/tufanbarisyildirim/gonginx/dumper"
  9. "github.com/tufanbarisyildirim/gonginx/parser"
  10. )
  11. // PerfOpt represents Nginx performance optimization settings
  12. type PerfOpt struct {
  13. WorkerProcesses string `json:"worker_processes"` // auto or number
  14. WorkerConnections string `json:"worker_connections"` // max connections
  15. KeepaliveTimeout string `json:"keepalive_timeout"` // timeout in seconds
  16. Gzip string `json:"gzip"` // on or off
  17. GzipMinLength string `json:"gzip_min_length"` // min length to compress
  18. GzipCompLevel string `json:"gzip_comp_level"` // compression level
  19. ClientMaxBodySize string `json:"client_max_body_size"` // max body size (with unit: k, m, g)
  20. ServerNamesHashBucketSize string `json:"server_names_hash_bucket_size"` // hash bucket size
  21. ClientHeaderBufferSize string `json:"client_header_buffer_size"` // header buffer size (with unit: k, m, g)
  22. ClientBodyBufferSize string `json:"client_body_buffer_size"` // body buffer size (with unit: k, m, g)
  23. }
  24. // UpdatePerfOpt updates the Nginx performance optimization settings
  25. func UpdatePerfOpt(opt *PerfOpt) error {
  26. confPath := nginx.GetConfPath("nginx.conf")
  27. if confPath == "" {
  28. return errors.New("failed to get nginx.conf path")
  29. }
  30. // Read the current configuration
  31. content, err := os.ReadFile(confPath)
  32. if err != nil {
  33. return errors.Wrap(err, "failed to read nginx.conf")
  34. }
  35. // Parse the configuration
  36. p := parser.NewStringParser(string(content), parser.WithSkipValidDirectivesErr())
  37. conf, err := p.Parse()
  38. if err != nil {
  39. return errors.Wrap(err, "failed to parse nginx.conf")
  40. }
  41. // Process the configuration and update performance settings
  42. updateNginxConfig(conf.Block, opt)
  43. // Dump the updated configuration
  44. updatedConf := dumper.DumpBlock(conf.Block, dumper.IndentedStyle)
  45. return Save(confPath, updatedConf, nil)
  46. }
  47. // updateNginxConfig updates the performance settings in the Nginx configuration
  48. func updateNginxConfig(block config.IBlock, opt *PerfOpt) {
  49. if block == nil {
  50. return
  51. }
  52. directives := block.GetDirectives()
  53. // Update main context directives
  54. updateOrAddDirective(block, directives, "worker_processes", opt.WorkerProcesses)
  55. // Look for events, http, and other blocks
  56. for _, directive := range directives {
  57. if directive.GetName() == "events" && directive.GetBlock() != nil {
  58. // Update events block directives
  59. eventsBlock := directive.GetBlock()
  60. eventsDirectives := eventsBlock.GetDirectives()
  61. updateOrAddDirective(eventsBlock, eventsDirectives, "worker_connections", opt.WorkerConnections)
  62. } else if directive.GetName() == "http" && directive.GetBlock() != nil {
  63. // Update http block directives
  64. httpBlock := directive.GetBlock()
  65. httpDirectives := httpBlock.GetDirectives()
  66. updateOrAddDirective(httpBlock, httpDirectives, "keepalive_timeout", opt.KeepaliveTimeout)
  67. updateOrAddDirective(httpBlock, httpDirectives, "gzip", opt.Gzip)
  68. updateOrAddDirective(httpBlock, httpDirectives, "gzip_min_length", opt.GzipMinLength)
  69. updateOrAddDirective(httpBlock, httpDirectives, "gzip_comp_level", opt.GzipCompLevel)
  70. updateOrAddDirective(httpBlock, httpDirectives, "client_max_body_size", opt.ClientMaxBodySize)
  71. updateOrAddDirective(httpBlock, httpDirectives, "server_names_hash_bucket_size", opt.ServerNamesHashBucketSize)
  72. updateOrAddDirective(httpBlock, httpDirectives, "client_header_buffer_size", opt.ClientHeaderBufferSize)
  73. updateOrAddDirective(httpBlock, httpDirectives, "client_body_buffer_size", opt.ClientBodyBufferSize)
  74. }
  75. }
  76. }
  77. // updateOrAddDirective updates a directive if it exists, or adds it to the block if it doesn't
  78. func updateOrAddDirective(block config.IBlock, directives []config.IDirective, name string, value string) {
  79. if value == "" {
  80. return
  81. }
  82. // Search for existing directive
  83. for _, directive := range directives {
  84. if directive.GetName() == name {
  85. // Update existing directive
  86. if len(directive.GetParameters()) > 0 {
  87. directive.GetParameters()[0].Value = value
  88. }
  89. return
  90. }
  91. }
  92. // If we get here, we need to add a new directive
  93. // Create a new directive and add it to the block
  94. // This requires knowledge of the underlying implementation
  95. // For now, we'll use the Directive type from gonginx/config
  96. newDirective := &config.Directive{
  97. Name: name,
  98. Parameters: []config.Parameter{{Value: value}},
  99. }
  100. // Add the new directive to the block
  101. // This is specific to the gonginx library implementation
  102. switch block := block.(type) {
  103. case *config.Config:
  104. block.Block.Directives = append(block.Block.Directives, newDirective)
  105. case *config.Block:
  106. block.Directives = append(block.Directives, newDirective)
  107. case *config.HTTP:
  108. block.Directives = append(block.Directives, newDirective)
  109. }
  110. }
  111. // sortDirectives sorts directives alphabetically by name
  112. func sortDirectives(directives []config.IDirective) {
  113. sort.SliceStable(directives, func(i, j int) bool {
  114. // Ensure both i and j can return valid names
  115. return directives[i].GetName() < directives[j].GetName()
  116. })
  117. }