> 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).

# 安装配置

在使用 CLI 命令配置 Traefik 之后，你可能需要对其进行更细致的配置。CLI 提供了一种迅速启动和测试 Traefik 的方法，但对于生产环境或需要更复杂配置的场景，推荐使用配置文件。配置文件不仅可以提供更多的配置选项，还可以在重启服务时保持配置不变，便于管理和版本控制。

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="https://1477494011-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjENyhayFBE8frwWaXFeM%2Fuploads%2FmltoT7TIyB0LSxks2ac5%2Fimage.png?alt=media&amp;token=dbed1a83-c635-4aee-b8ca-c08a19470026" 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="https://1477494011-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjENyhayFBE8frwWaXFeM%2Fuploads%2F2q5doOwWh2z6CFGDGBSG%2Fimage.png?alt=media&amp;token=51db9e0f-ec1e-489c-ba33-567c718dc3f5" alt=""><figcaption></figcaption></figure>
