项目作者: xgfone

项目描述 :
A generic parser of the NGINX config format
高级语言: Go
项目地址: git://github.com/xgfone/ngconf.git
创建时间: 2020-01-10T14:12:52Z
项目社区:https://github.com/xgfone/ngconf

开源协议:Apache License 2.0

下载


ngconf Build Status GoDoc License

The package ngconf supplies a generic parser of the NGINX config format referring to the implementation python-nginx, and supports Go 1.x.

Example

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/xgfone/ngconf"
  5. )
  6. const conf = `
  7. user www-data;
  8. worker_processes auto;
  9. pid /run/nginx.pid;
  10. include /etc/nginx/modules-enabled/*.conf;
  11. events {
  12. worker_connections 768;
  13. # multi_accept on;
  14. }
  15. stream {
  16. server {
  17. listen 127.0.0.1:8443;
  18. proxy_connect_timeout 1s;
  19. proxy_pass backend;
  20. }
  21. }
  22. http {
  23. ##
  24. # Basic Settings
  25. ##
  26. sendfile on;
  27. tcp_nopush on;
  28. tcp_nodelay on;
  29. keepalive_timeout 65;
  30. types_hash_max_size 2048;
  31. # server_tokens off;
  32. # server_names_hash_bucket_size 64;
  33. # server_name_in_redirect off;
  34. include /etc/nginx/mime.types;
  35. default_type application/octet-stream;
  36. ##
  37. # SSL Settings
  38. ##
  39. # Dropping SSLv3, ref: POODLE
  40. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  41. ssl_prefer_server_ciphers on;
  42. ##
  43. # Logging Settings
  44. ##
  45. access_log /var/log/nginx/access.log;
  46. error_log /var/log/nginx/error.log;
  47. ##
  48. # Gzip Settings
  49. ##
  50. gzip on;
  51. # gzip_vary on;
  52. # gzip_proxied any;
  53. # gzip_comp_level 6;
  54. # gzip_buffers 16 8k;
  55. # gzip_http_version 1.1;
  56. # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  57. ##
  58. # Virtual Host Configs
  59. ##
  60. include /etc/nginx/conf.d/*.conf;
  61. include /etc/nginx/sites-enabled/*;
  62. }
  63. #mail {
  64. # # See sample authentication script at:
  65. # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
  66. #
  67. # # auth_http localhost/auth.php;
  68. # # pop3_capabilities "TOP" "USER";
  69. # # imap_capabilities "IMAP4rev1" "UIDPLUS";
  70. #
  71. # server {
  72. # listen localhost:110;
  73. # protocol pop3;
  74. # proxy on;
  75. # }
  76. #
  77. # server {
  78. # listen localhost:143;
  79. # protocol imap;
  80. # proxy on;
  81. # }
  82. #}`
  83. func main() {
  84. root, err := ngconf.Decode(conf)
  85. if err != nil {
  86. fmt.Println(err)
  87. return
  88. }
  89. // Remove a config block.
  90. root.Del("events")
  91. // Add a new config item.
  92. stream := root.Get("stream")[0]
  93. backend := stream.Add("upstream", "backend")
  94. backend.Add("hash", "$remote_addr", "consistent")
  95. backend.Add("server", "backend1:443", "max_fails=3", "fail_timeout=30s")
  96. // // Get a config item.
  97. backend = stream.Get("upstream", "backend")[0]
  98. hash := backend.Get("hash", "$remote_addr")
  99. fmt.Println(len(hash), "node:", hash[0].Directive, hash[0].Args)
  100. // Print the whole config.
  101. fmt.Println("========================= Config =========================")
  102. fmt.Println(root)
  103. /// Output:
  104. //
  105. // 1 node: hash [$remote_addr consistent]
  106. // ========================= Config =========================
  107. // user www-data;
  108. // worker_processes auto;
  109. // pid /run/nginx.pid;
  110. // include /etc/nginx/modules-enabled/*.conf;
  111. //
  112. // stream {
  113. // server {
  114. // listen 127.0.0.1:8443;
  115. // proxy_connect_timeout 1s;
  116. // proxy_pass backend;
  117. // }
  118. //
  119. // upstream backend {
  120. // hash $remote_addr consistent;
  121. // server backend1:443 max_fails=3 fail_timeout=30s;
  122. // }
  123. // }
  124. //
  125. // http {
  126. // ##
  127. // # Basic Settings
  128. // ##
  129. // sendfile on;
  130. // tcp_nopush on;
  131. // tcp_nodelay on;
  132. // keepalive_timeout 65;
  133. // types_hash_max_size 2048;
  134. //
  135. // # server_tokens off;
  136. // # server_names_hash_bucket_size 64;
  137. // # server_name_in_redirect off;
  138. // include /etc/nginx/mime.types;
  139. // default_type application/octet-stream;
  140. //
  141. // ##
  142. // # SSL Settings
  143. // ##
  144. // # Dropping SSLv3, ref: POODLE
  145. // ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  146. // ssl_prefer_server_ciphers on;
  147. //
  148. // ##
  149. // # Logging Settings
  150. // ##
  151. // access_log /var/log/nginx/access.log;
  152. // error_log /var/log/nginx/error.log;
  153. //
  154. // ##
  155. // # Gzip Settings
  156. // ##
  157. // gzip on;
  158. //
  159. // # gzip_vary on;
  160. // # gzip_proxied any;
  161. // # gzip_comp_level 6;
  162. // # gzip_buffers 16 8k;
  163. // # gzip_http_version 1.1;
  164. // # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  165. // ##
  166. // # Virtual Host Configs
  167. // ##
  168. // include /etc/nginx/conf.d/*.conf;
  169. // include /etc/nginx/sites-enabled/*;
  170. // }
  171. //
  172. // #mail {
  173. // # # See sample authentication script at:
  174. // # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
  175. // #
  176. // # # auth_http localhost/auth.php;
  177. // # # pop3_capabilities "TOP" "USER";
  178. // # # imap_capabilities "IMAP4rev1" "UIDPLUS";
  179. // #
  180. // # server {
  181. // # listen localhost:110;
  182. // # protocol pop3;
  183. // # proxy on;
  184. // # }
  185. // #
  186. // # server {
  187. // # listen localhost:143;
  188. // # protocol imap;
  189. // # proxy on;
  190. // # }
  191. // #}
  192. }