k8s/docker 部署 caddy 静态页面或代理其他服务
先创建目录在目录中创建文件Caddyfile内容如下(
·
caddy 配置文件
先创建目录 /www/k8s/caddy
在目录中创建文件Caddyfile
内容如下(静态页面,或代理其他服务都是在配置文件中配置,根据需要自行选择)
配置静态页面
Caddyfile
内容
foxwho.com:80 {
root * /srv/foxwho.com
file_server
}
/srv/foxwho.com : 容器内部 静态文件目录
配置代理服务
Caddyfile
内容
foxwho.com:80 {
reverse_proxy localhost:5000
}
配置代理服务-二级目录代理
Caddyfile
内容
foxwho.com:80 {
reverse_proxy /api/* localhost:5000
}
配置代理服务-二级目录代理2
访问 foxwho.com/api/index.html
,实际访问的是localhost:5000/index.html
Caddyfile
内容
foxwho.com:80 {
handle_path /api/* {
reverse_proxy localhost:5000
}
}
配置静态页面和代理服务
Caddyfile
内容
foxwho.com:80 {
root * /srv/foxwho.com
reverse_proxy /api/* localhost:5000
file_server
}
配置 跳转页面
Caddyfile
内容
foxwho.com:80 {
redir https://www.{host}{uri}
}
或
foxwho.com:80 {
redir https://www.foxwho.com{uri}
}
或
foxwho.com:80 {
redir https://www.baidu.com
}
配置 url 重写
Caddyfile
内容
foxwho.com:80 {
redir /add /add/
redir /remove/ /remove
}
配置 php
Caddyfile
内容
foxwho.com:80 {
root * /srv/public
encode gzip
php_fastcgi localhost:9000
file_server
}
配置 单页面应用
Caddyfile
内容
foxwho.com:80 {
root * /srv/foxwho.com
encode gzip
try_files {path} /index.html
file_server
}
配置 单页面应用和代理服务
Caddyfile
内容
foxwho.com:80 {
encode gzip
handle /api/* {
reverse_proxy backend:8000
}
handle {
root * /srv/foxwho.com
try_files {path} /index.html
file_server
}
}
同时支持 http 和 https
访问http 不会跳转到 https上
Caddyfile
内容
http://foxwho.com,https://foxwho.com {
encode gzip
handle /api/* {
reverse_proxy backend:8000
}
handle {
root * /srv/foxwho.com
try_files {path} /index.html
file_server
}
}
配置https,且访问http会跳转到https
访问http 不会跳转到 https上
Caddyfile
内容
foxwho.com {
encode gzip
handle /api/* {
reverse_proxy backend:8000
}
handle {
root * /srv/foxwho.com
try_files {path} /index.html
file_server
}
}
配置日志
访问http 不会跳转到 https上
Caddyfile
内容
foxwho.com {
# 日志
log /data/foxwho.log
encode gzip
handle /api/* {
reverse_proxy backend:8000
}
handle {
root * /srv/foxwho.com
try_files {path} /index.html
file_server
}
}
更多请看 https://caddyserver.com/docs/caddyfile/patterns
k8s部署 caddy
deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: caddy
labels:
app: caddy
spec:
serviceName: caddy
replicas: 1
selector:
matchLabels:
app: caddy
template:
metadata:
labels:
app: caddy
spec:
containers:
- name: caddy
image: caddy:alpine
imagePullPolicy: IfNotPresent
# resources:
# limits:
# cpu: 450m
# memory: 4000Mi
# requests:
# cpu: 400m
# memory: 4000Mi
env:
- name: TZ
value: Asia/Shanghai
ports:
- containerPort: 80
volumeMounts:
# 配置文件
- name: vm-caddyfile
mountPath: /etc/caddy/Caddyfile
# 配置文件
- name: vm-conf
mountPath: /config
# 数据目录,TLS 证书,私钥等等
- name: vm-data
mountPath: /data
# 站点文件目录
- name: vm-srv
mountPath: /srv
volumes:
- name: vm-caddyfile
hostPath:
path: /www/k8s/caddy/Caddyfile
type: FileOrCreate
- name: vm-conf
hostPath:
path: /www/k8s/caddy/config
type: DirectoryOrCreate
- name: vm-data
hostPath:
path: /www/k8s/caddy/data
type: DirectoryOrCreate
- name: vm-srv
hostPath:
path: /www/k8s/caddy/srv
type: DirectoryOrCreate
svc.yml
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
targetPort: 80
selector:
app: nginx
应用并生效
kubectl apply -f svc.yml
kubectl apply -f deployment.yml
配置ingress 域名
ingress.yml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: traefik
name: caddy
spec:
rules:
- host: foxwho.com
http:
paths:
- backend:
service:
name: caddy
port:
number: 80
path: /
pathType: ImplementationSpecific
应用并生效
kubectl apply -f ingress.yml
浏览器访问
http://foxwho.com
docker 配置
当前目录下新建立 文件和文件夹
touch $(pwd)/Caddyfile
mkdir -p $(pwd)/config
mkdir -p $(pwd)/data
mkdir -p $(pwd)/srv
Caddyfile
文件内容如下
:80 {
handle_path /api/* {
reverse_proxy https://www.foxwho.com
}
}
启动
docker run --name caddy \
-d \
-v $(pwd)/Caddyfile:/etc/caddy/Caddyfile \
-v $(pwd)/config:/config \
-v $(pwd)/data:/data \
-v $(pwd)/srv:/srv \
-p 18081:80 \
caddy:latest
最后访问:
http://127.0.0.1:18081/api/page/2
会是博客第二页
更多推荐
已为社区贡献20条内容
所有评论(0)