> For the complete documentation index, see [llms.txt](https://gitbook.gpg123.vip/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.gpg123.vip/traefik-yi-ge-qiang-da-de-dai-li-pei-zhi-gong-ju/ji-yu-wen-jian/an-zhuang-pei-zhi.md).

# 安装配置

demo源码：<https://gitee.com/gpg-dev/my-traefik.git>

## 准备环境

* [x] 下载traefik
* [x] 基于cli命令或配置文件启动

## 下载

<https://github.com/traefik/traefik/releases/tag/v3.0.0>

根据需求下载自己适合的环境安装包，并解压....此过程省略

<figure><img src="/files/BEE72mVD3Q6uOVIYVWVn" alt=""><figcaption></figcaption></figure>

## 基于cli命令配置

按照traefik -h提供描述以及官方文档描述，进行配置

**cli命令示例：**

{% code title="" lineNumbers="true" %}

```
traefik --providers.file.directory=/home/config --api.dashboard=true --api.insecure
```

{% endcode %}

## 基于配置文件配置 -推荐

要使用配置文件启动 Traefik，请遵循以下步骤：

1. 打开命令行或终端。
2. 导航到 Traefik 的安装目录。
3. 执行以下命令以启动 Traefik：

   ```shell
   ./traefik --configFile=your-config-file.yaml
   ```

   将 `your-config-file.yaml` 替换为您的配置文件名称。

**配置文件示例：**

{% code title="your-config-file.yaml" lineNumbers="true" %}

```yaml
global:
  checkNewVersion: false    # 周期性的检查是否有新版本发布
  sendAnonymousUsage: false # 周期性的匿名发送使用统计信息
serversTransport:
  insecureSkipVerify: true  # Traefik忽略验证代理服务的TLS证书
#从文件获取
providers:
  file:
    directory: /home/my-traefik/config
    watch: true
ping: true    #开启ping
#api
api:
  dashboard: true  #dashboard-ui
  insecure: true
  debug: false
#配置指标
metrics:
  prometheus: # 配置Prometheus监控指标数据，并使用默认配置
    addRoutersLabels: true  # 添加routers metrics
    entryPoint: "metrics"   # 指定metrics监听地址
entryPoints:
  metrics:
    address: ":19100"        # 配置19100端口，作为metrics收集入口
  http:
    address: ":80"           #配置80作为http入口
    # http请求自动跳转https
    http:
      redirections:
        entryPoint:
          to: https
    proxyProtocol:
      insecure: true
    forwardedHeaders:
      insecure: true
  https:
    address: ":443"         # 配置443端口，并设置入口名称为 websecure
    proxyProtocol:
      insecure: true
    forwardedHeaders:
      insecure: true

# 开启ACME 自动生成HTTPS证书
certificatesResolvers:
  myCertResolver:
    acme:
      # 邮箱地址
      email: "2694484453@qq.com"
      # 签发的https证书存放位置
      storage: "/home/my-traefik/acme.json"
      # 自动签发证书的一种验证方式(还有tlsChallent、dnsChallenge,我们用的是常用的这种httpChallenge方式)
      httpChallenge:
        entryPoint: http

accessLog:
  filePath: "/home/my-traefik/logs/access.log" # 设置访问日志文件存储路径，如果为空则输出到控制台
  format: "common"          # 设置访问调试日志格式
  bufferingSize: 0          # 设置访问日志缓存行数
  filters:
    statusCodes: [ "200","500","404","403","502" ]   # 设置只保留指定状态码范围内的访问日志
    retryAttempts: true     # 设置代理访问重试失败时，保留访问日志
    minDuration: 20         # 设置保留请求时间超过指定持续时间的访问日志
  fields: # 设置访问日志中的字段是否保留（keep保留、drop不保留）
    defaultMode: keep       # 设置默认保留访问日志字段
    names: # 针对访问日志特别字段特别配置保留模式
      ClientUsername: drop
      StartUTC: drop        # 禁用日志timestamp使用UTC
    headers: # 设置Header中字段是否保留
      defaultMode: keep     # 设置默认保留Header中字段
      names: # 针对Header中特别字段特别配置保留模式
        #User-Agent: redact # 可以针对指定agent
        Authorization: drop
        Content-Type: keep

```

{% endcode %}

## 编写执行脚本

基于cli启动脚本

{% code title="start.sh" %}

```
nohup traefik --providers.file.directory=/home/config --api.dashboard=true --api.insecure > treafik-log.txt  2>&1 &
```

{% endcode %}

基于file启动脚本

{% code title="start-by-file.sh" %}

```
nohup traefik --configFile=/home/my-traefik/traefik.yml > /dev/null 2>&1 &
```

{% endcode %}

Traefik 将根据您提供的配置文件启动。可以通过访问 Traefik 的仪表板来验证是否成功启动，通常这可以通过访问 `http://ip:8080/dashboard/` 实现，假设您的配置允许从本地访问且端口号为8080。

<figure><img src="/files/XrQqjCh1AQRnL2G8wcqZ" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitbook.gpg123.vip/traefik-yi-ge-qiang-da-de-dai-li-pei-zhi-gong-ju/ji-yu-wen-jian/an-zhuang-pei-zhi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
