我在本地计算机上运行 traefik。
仪表板可以工作,但我无法访问我的应用程序。
当我尝试向应用程序发送请求时,我总是收到 HTTP 404。
我在一台本地机器上运行所有内容。
无需 docker、k8s 等
我做错了什么?
堆栈:
应用程序源代码:
package main
import (
"log"
"net/http"
)
func responseURLHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("Host: %s | Method: %s | Path: %s\n", r.Host, r.Method, r.URL)
}
func main() {
http.HandleFunc("/", responseURLHandler)
log.Println("starting....")
http.ListenAndServe(":8000", nil)
log.Println("stopping....")
}
配置文件traefik
#############################
#
# Configuration for Traefik.
#
#############################
#############################
# Global configuration
#############################
global:
checkNewVersion: true
sendAnonymousUsage: true
#############################
# Traefik UI
#############################
serversTransport:
insecureSkipVerify: true
#############################
# Traefik logs configuration
#############################
log:
level: INFO
# format: json
#############################
# Access logs configuration
#############################
accessLog: {}
# Sets the file path for the access log. If not specified, stdout will be used.
# Intermediate directories are created if necessary.
#
# Optional
# Default: os.Stdout
#
# filePath: /path/to/log/log.txt
# Format is either "json" or "common".
#
# Optional
# Default: "common"
#
# format: json
#############################
# API and dashboard configuration
#############################
# Enable API and dashboard
#
# Optional
#
api:
# Enable the API in insecure mode
#
# Optional
# Default: false
#
insecure: true
# Enabled Dashboard
#
# Optional
# Default: true
#
# dashboard: false
#############################
# EntryPoints configuration
#############################
entryPoints:
web:
address: ":8888"
asDefault: true
http:
routers:
app-router:
service: app-service
entryPoints:
- "web"
services:
app-service:
loadBalancer:
servers:
- url: http://localhost:8000/
试运行
运行应用程序
go run main.go
运行traefik
traefik --configfile=traefik_lab.yaml
发送请求
curl http://localhost:8888/api/xxx/111
结果:Traefik 访问日志
::1 - - [26/Dec/2024:12:09:32 +0000] "GET /api/xxx/111 HTTP/1.1" 404 19 "-" "-" 1 "-" "-" 0ms
设法解决问题。
问题是 traefik 的一部分忽略了指令:
http
。 目录结构:
tree .
.
├── app.yaml
├── main.go
└── traefik.yaml
文件
traefik.yaml
:
################################################################
#
# Configuration for Traefik v2.
#
################################################################
################################################################
# Global configuration
################################################################
global:
checkNewVersion: true
sendAnonymousUsage: true
################################################################
# Traefik UI
################################################################
serversTransport:
insecureSkipVerify: true
################################################################
# Traefik logs configuration
################################################################
log:
level: INFO
# format: json
################################################################
# Access logs configuration
################################################################
accessLog: {}
# Sets the file path for the access log. If not specified, stdout will be used.
# Intermediate directories are created if necessary.
#
# Optional
# Default: os.Stdout
#
# filePath: /path/to/log/log.txt
# Format is either "json" or "common".
#
# Optional
# Default: "common"
#
# format: json
################################################################
# API and dashboard configuration
################################################################
# Enable API and dashboard
#
# Optional
#
api:
# Enable the API in insecure mode
#
# Optional
# Default: false
#
insecure: true
# Enabled Dashboard
#
# Optional
# Default: true
#
# dashboard: false
################################################################
# EntryPoints configuration
################################################################
entryPoints:
web:
address: ":8888"
providers:
file:
filename: app.yaml
文件
app.yaml
:
http:
routers:
my-router:
rule: "Host(`localhost`)"
service: my-service
entryPoints:
- web
services:
my-service:
loadBalancer:
servers:
- url: "http://localhost:8000"
此外,您也可以指定一个包含配置的目录,而不是特定的配置文件。
设置
traefik.yaml
providers:
file:
directory: "/path/to/dynamic/conf"