As we all know, Nginx is a very popular reverse proxy server. It is very stable and has a lot of features. But I choose Traefik instead of Nginx as a reverse proxy in test environment since it is more suitable for my use case. In this post, I will explain why I choose Traefik instead of Nginx.

Background

I have a test environment which is running on a single server. It has a lot of services running on it. I want to expose these services to the internet. So I need a reverse proxy server to do this. I am used to using Swag as a reverse proxy for years, witch is based on Nginx. But it is a little bit hard to configure. So I want to find a better solution.

I want to find a reverse proxy server which meets the following requirements:

  1. It should be containerized. I don't want to install it on the host machine.
  2. It should be easy to configure.
  3. It should support Let's Encrypt to generate SSL certificates automatically.
  4. It should be easy to migrate to production environment if needed.

Swag

Swag1 is a reverse proxy server based on Nginx, witch I used for years. It meets all the requirements above. Swag project provides a docker image to run it. And dozens of Nginx configuration template files for developers to use. If you are familiar with Nginx, you can easily configure it. But it is way too complex for beginners.

I choose Swag as my reverse proxy server instead of bare Nginx, because it supports Let's Encrypt to generate SSL automatically by adding a few lines of configuration. It is very convenient.

Caddy

Caddy2 is a web server written in Go. I tried this solution to solve my problem after doing some research.

For example, if you want to expose a service running on port 80, you just need to create a file named Caddyfile:

api.newbe.pro {
reverse_proxy api:80
} ws.newbe.pro {
reverse_proxy ws:80
}

Then run Caddy with docker as following docker-compose file:

version: "3.4"

services:
caddy:
image: caddy
container_name: caddy
restart: unless-stopped
ports:
- 80:80
- 443:443
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile
- ./data/caddy:/data
- ./data/caddy/config:/config
api:
image: newbe36524/newbe.api
container_name: api
restart: unless-stopped
environment:
- ASPNETCORE_ENVIRONMENT=Development
volumes:
- ./data/api:/app/data
ws:
image: newbe36524/newbe.ws
container_name: ws
restart: unless-stopped
environment:
- ASPNETCORE_ENVIRONMENT=Development
volumes:
- ./data/ws:/app/data

Then configure my DNS to point api.newbe.pro and ws.newbe.pro to my server with A record. Then I can access my api service and ws service with https://api.newbe.pro and https://ws.newbe.pro respectively.

I finally gave up this solution because Caddy does not support many build-in plugins as Traefik does. Of course, you can compile Caddy with plugins you need. But I don't want to do this.

Traefik

Traefik3 is also a reverse proxy server written in Go. I can run it as following docker-compose file:

version: '3.4'

secrets:
azure_client_id:
file: "./secrets/azure_client_id.secret"
azure_client_secret:
file: "./secrets/azure_client_secret.secret"
azure_tenant_id:
file: "./secrets/azure_tenant_id.secret"
azure_subscription_id:
file: "./secrets/azure_subscription_id.secret"
azure_resource_group:
file: "./secrets/azure_resource_group.secret" services:
reverse-proxy:
# The official v2 Traefik docker image
image: traefik:v2.10
container_name: "traefik"
# Enables the web UI and tells Traefik to listen to docker
command:
- "--api.insecure=true"
- "--providers.docker=true"
- "--entrypoints.web.address=:80"
- "--entrypoints.websecure.address=:443"
- "--certificatesresolvers.myresolver.acme.dnschallenge=true"
- "--certificatesresolvers.myresolver.acme.dnschallenge.provider=azuredns"
- "--certificatesresolvers.myresolver.acme.email=contact@newbe.pro"
- "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json"
environment:
- "AZURE_CLIENT_ID_FILE=/run/secrets/azure_client_id"
- "AZURE_CLIENT_SECRET_FILE=/run/secrets/azure_client_secret"
- "AZURE_TENANT_ID=/run/secrets/azure_tenant_id"
- "AZURE_SUBSCRIPTION_ID=/run/secrets/azure_subscription_id"
- "AZURE_RESOURCE_GROUP=/run/secrets/azure_resource_group"
secrets:
- "azure_client_id"
- "azure_client_secret"
- "azure_tenant_id"
- "azure_subscription_id"
- "azure_resource_group"
ports:
# The HTTP port
- "80:80"
- "443:443"
- "443:443/udp"
# The Web UI (enabled by --api.insecure=true)
- "8080:8080"
volumes:
# So that Traefik can listen to the Docker events
- "/var/run/docker.sock:/var/run/docker.sock:ro"
- "./letsencrypt:/letsencrypt"
api:
image: newbe36524/newbe.api
container_name: api
restart: unless-stopped
environment:
- ASPNETCORE_ENVIRONMENT=Development
labels:
- "traefik.enable=true"
- "traefik.http.routers.api.rule=Host(`api.newbe.pro`)"
- "traefik.http.routers.api.entrypoints=websecure"
- "traefik.http.routers.api.tls.certresolver=myresolver"
ws:
image: newbe36524/newbe.ws
container_name: ws
restart: unless-stopped
environment:
- ASPNETCORE_ENVIRONMENT=Development
labels:
- "traefik.enable=true"
- "traefik.http.routers.ws.rule=Host(`ws.newbe.pro`)"
- "traefik.http.routers.ws.entrypoints=websecure"
- "traefik.http.routers.ws.tls.certresolver=myresolver"

You can notice that all the configuration is in docker-compose file. I don't need to create any other configuration file, it is very convenient. Traefik is not only support docker as a provider to automatically discover services, but also support Kubernetes if I want to migrate to Kubernetes in the future.

Conclusion

I choose Traefik instead of Nginx as a reverse proxy in test environment because it is more suitable for my use case. It is cloud native and easy to configure. I don't need to create any other configuration file. I can configure everything in docker-compose file.

References

感谢阅读,如果觉得本文有用,不妨点击推荐或者在评论区留下 Mark,让更多的人可以看到。

欢迎关注作者的微信公众号“newbe技术专栏”,获取更多技术内容。


  1. https://github.com/linuxserver/docker-swag

  2. https://caddyserver.com/v2

  3. https://doc.traefik.io/traefik/

  4. https://learn.microsoft.com/azure/dns/dns-zones-records?WT.mc_id=DT-MVP-5004283

Nginx No, Traefik Yes的更多相关文章

  1. 在单机Docker上安装 Traefik 反向代理-负载均衡器

    一.创建Traefik和容器应用的连接网络 sudo docker network create traefik-net 二.下载Traefik样本配置文件wget https://raw.githu ...

  2. Docker Swarm集群中部署Traefik负载均衡器

    一.创建单节点的Docker Swarm集群 docker swarm init 二.在Swarm集群中创建一个网络 docker network create --driver=overlay tr ...

  3. Traefik 2.0 tcp 路由试用

    对于tcp 的路由是基于sni (需要tls)但是可以通过统配(*) 解决不试用tls的,当然也可以让Traefik 自动生成tls 证书 以下是测试http 以及mysql 的tcp 路由配置(de ...

  4. 实操教程丨如何在K8S集群中部署Traefik Ingress Controller

    注:本文使用的Traefik为1.x的版本 在生产环境中,我们常常需要控制来自互联网的外部进入集群中,而这恰巧是Ingress的职责. Ingress的主要目的是将HTTP和HTTPS从集群外部暴露给 ...

  5. Kubernetes 在生产环境中常用架构

    Kubernetes 在生产环境中常用架构 首先,我们来梳理下Kubernetes生产架构,其设计适用于绝大多数环境.如下图所示 在该架构中,我们可以将其分为四层,如下: Client层:即Kuber ...

  6. 浅谈Kubernetes生产架构

    注意本文,只是笔者针对Kubernetes生产环境运行的一些关于架构设计和实现方案的总结,内容很粗糙,同时也会不断完善. 首先,我们来梳理下Kubernetes生产架构,其设计适用于绝大多数环境.如下 ...

  7. Ingress介绍与安装配置

    在 Kubernetes 集群中,Ingress是授权入站连接到达集群服务的规则集合,为您提供七层负载均衡能力.您可以给 Ingress 配置提供外部可访问的 URL.负载均衡.SSL.基于名称的虚拟 ...

  8. Kubernetes简述

    一.Kubernetes特性 1.自动装箱 建构于容器之上,基于资源依赖及其他约束自动完成容器部署且不影响其可用性,并通过调度机制混合关键型应用和非关键型应用的工作负载于一点以提高资源利用率. 2.自 ...

  9. React 服务端渲染最佳解决方案

    最近在开发一个服务端渲染工具,通过一篇小文大致介绍下服务端渲染,和服务端渲染的方式方法.在此文后面有两中服务端渲染方式的构思,根据你对服务端渲染的利弊权衡,你会选择哪一种服务端渲染方式呢? 什么是服务 ...

  10. k8s集群介绍

    Kubernetes集群组件 一个典型的Kubernetes集群由多个工作节点和一个集群控制节点,以及一个集群状态存储系统etcd组成.其中Master节点负责整个集群管理工作,为集群提供管理接口,并 ...

随机推荐

  1. NFS使用TrueNAS SCALE的好处

    之前一直习惯于使用linux自带的NFS服务,但由于NFS空间占用较大自身不带压缩能力,最近使用了号称比较流行的 TrueNAS SCALE 版本,重要的是它还可以提供块服务 Block (iSCSI ...

  2. jQuery validate验证表单隐藏域

    function validInfoForm(){        return $("#form_password").validate({            ignore : ...

  3. 从零开始!Jupyter Notebook 安装教程

    一.引言 Jupyter Notebook 是一款非常实用的交互式编程环境,广泛应用于数据分析.机器学习.教学等领域.在安装 Jupyter Notebook 之前,需要确保计算机已安装 Python ...

  4. Nginx - [02] 安装部署&配置

    官网下载地址:http://nginx.org/en/download.html 001 || 安装[windows] 解压之后,在命令提示符窗口启动nginx.exe 在浏览器访问80端口:loca ...

  5. Flink学习(四) Flink Table & SQL 实现wordcount Java版本

    Flink Table & SQL WordCountFlink SQL 是 Flink 实时计算为简化计算模型,降低用户使用实时计算门槛而设计的一套符合标准 SQL 语义的开发语言. 一个完 ...

  6. apache和nginx關聯php的過程

    Nginx端 Nginx是一個服務器,同時也是一個功能強大的proxy服務器,除了進行http請求的代理,還可以進行其他協議請求代理(fastCgi協議),為了能使nginx理解fastCgi協議,n ...

  7. 解决本地代理问题 git 或者 curl Failed to connect to 127.0.0.1 port 1087 after 8 ms: Connection refused

    问题出现原因 git配置了代理 本地配置了代理 执行这个命令可以看到自己的代理设置: env | grep -I proxy 临时更改代理 在当前终端执行以下命令,就可以临时将代理取消掉 export ...

  8. 基础指令:grep、wc、管道符( | )、head、tail、less、more、sort、uniq、Linux软件安装、重定向及

    目录 3.0 dd读取.转换并输出数据 3.1 压缩 (tar.zip).解压缩(tar xf.unzip) 3.2 ln软硬链接 3.2.1 软链接: 3.2.2 硬链接: 3.3 find文件查找 ...

  9. CompletableFuture你真的懂了么,我劝你在项目中慎用

    1. 前言 在实际做项目中,我们经常使用多线程.异步的来帮我们做一些事情. 比如用户抽取奖品,异步的给他发一个push. 又比如一段前后不相关的业务逻辑,原本是顺序执行,耗时=(A + B + C), ...

  10. 什么是 CSS 设计模式

    这是转载的,先收藏到我的博客园. 什么是设计模式? 曾有人调侃,设计模式是工程师用于跟别人显摆的,显得高大上:也曾有人这么说,不是设计模式没用,是你还没有到能懂它,会用它的时候. 先来看一下比较官方的 ...