Traefik:无法访问应用程序 - HTTP 404

问题描述 投票:0回答:1

我在本地计算机上运行 traefik。 仪表板可以工作,但我无法访问我的应用程序。
当我尝试向应用程序发送请求时,我总是收到 HTTP 404。
我在一台本地机器上运行所有内容。
无需 docker、k8s 等

我做错了什么?

堆栈:

  1. Ubuntu 22.10
  2. Traefik v3.2.3(来自 github repo 的二进制文件)
  3. 简单的 golang 应用程序

应用程序源代码:

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
local traefik
1个回答
0
投票

设法解决问题。

问题是 traefik 的一部分忽略了指令:

http

通过指定提供程序并将设置存储在单独的文件中可以解决该问题。
一般来说,traefik 有时不会发出有关错误配置的信号,而只是忽略它们。

解决方案

目录结构:

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"
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.