文件后端

类似其他的反向代理工具,Træfɪk也可以使用文件来配置。你有两种选择:

  • 在全局配置文件 traefik.toml 后追加你的配置:
  1. # traefik.toml
  2. logLevel = "DEBUG"
  3. defaultEntryPoints = ["http", "https"]
  4. [entryPoints]
  5. [entryPoints.http]
  6. address = ":80"
  7. [entryPoints.http.redirect]
  8. entryPoint = "https"
  9. [entryPoints.https]
  10. address = ":443"
  11. [entryPoints.https.tls]
  12. [[entryPoints.https.tls.certificates]]
  13. CertFile = "integration/fixtures/https/snitest.com.cert"
  14. KeyFile = "integration/fixtures/https/snitest.com.key"
  15. [[entryPoints.https.tls.certificates]]
  16. CertFile = "integration/fixtures/https/snitest.org.cert"
  17. KeyFile = "integration/fixtures/https/snitest.org.key"
  18. [file]
  19. # rules
  20. [backends]
  21. [backends.backend1]
  22. [backends.backend1.circuitbreaker]
  23. expression = "NetworkErrorRatio() > 0.5"
  24. [backends.backend1.servers.server1]
  25. url = "http://172.17.0.2:80"
  26. weight = 10
  27. [backends.backend1.servers.server2]
  28. url = "http://172.17.0.3:80"
  29. weight = 1
  30. [backends.backend2]
  31. [backends.backend1.maxconn]
  32. amount = 10
  33. extractorfunc = "request.host"
  34. [backends.backend2.LoadBalancer]
  35. method = "drr"
  36. [backends.backend2.servers.server1]
  37. url = "http://172.17.0.4:80"
  38. weight = 1
  39. [backends.backend2.servers.server2]
  40. url = "http://172.17.0.5:80"
  41. weight = 2
  42. [frontends]
  43. [frontends.frontend1]
  44. backend = "backend2"
  45. [frontends.frontend1.routes.test_1]
  46. rule = "Host:test.localhost"
  47. [frontends.frontend2]
  48. backend = "backend1"
  49. passHostHeader = true
  50. priority = 10
  51. entrypoints = ["https"] # 覆盖 defaultEntryPoints
  52. [frontends.frontend2.routes.test_1]
  53. rule = "Host:{subdomain:[a-z]+}.localhost"
  54. [frontends.frontend3]
  55. entrypoints = ["http", "https"] # 覆盖 defaultEntryPoints
  56. backend = "backend2"
  57. rule = "Path:/test"
  • 或者将你的规则分割成独立的文件,例如 rules.toml
  1. # traefik.toml
  2. logLevel = "DEBUG"
  3. [entryPoints]
  4. [entryPoints.http]
  5. address = ":80"
  6. [entryPoints.http.redirect]
  7. entryPoint = "https"
  8. [entryPoints.https]
  9. address = ":443"
  10. [entryPoints.https.tls]
  11. [[entryPoints.https.tls.certificates]]
  12. CertFile = "integration/fixtures/https/snitest.com.cert"
  13. KeyFile = "integration/fixtures/https/snitest.com.key"
  14. [[entryPoints.https.tls.certificates]]
  15. CertFile = "integration/fixtures/https/snitest.org.cert"
  16. KeyFile = "integration/fixtures/https/snitest.org.key"
  17. [file]
  18. filename = "rules.toml"
  1. # rules.toml
  2. [backends]
  3. [backends.backend1]
  4. [backends.backend1.circuitbreaker]
  5. expression = "NetworkErrorRatio() > 0.5"
  6. [backends.backend1.servers.server1]
  7. url = "http://172.17.0.2:80"
  8. weight = 10
  9. [backends.backend1.servers.server2]
  10. url = "http://172.17.0.3:80"
  11. weight = 1
  12. [backends.backend2]
  13. [backends.backend1.maxconn]
  14. amount = 10
  15. extractorfunc = "request.host"
  16. [backends.backend2.LoadBalancer]
  17. method = "drr"
  18. [backends.backend2.servers.server1]
  19. url = "http://172.17.0.4:80"
  20. weight = 1
  21. [backends.backend2.servers.server2]
  22. url = "http://172.17.0.5:80"
  23. weight = 2
  24. [frontends]
  25. [frontends.frontend1]
  26. backend = "backend2"
  27. [frontends.frontend1.routes.test_1]
  28. rule = "Host:test.localhost"
  29. [frontends.frontend2]
  30. backend = "backend1"
  31. passHostHeader = true
  32. priority = 10
  33. entrypoints = ["https"] # overrides defaultEntryPoints
  34. [frontends.frontend2.routes.test_1]
  35. rule = "Host:{subdomain:[a-z]+}.localhost"
  36. [frontends.frontend3]
  37. entrypoints = ["http", "https"] # overrides defaultEntryPoints
  38. backend = "backend2"
  39. rule = "Path:/test"

如果你希望 Træfɪk 动态监测这些文件的变化,只需添加:

  1. [file]
  2. watch = true