Skip to content

一个nginx 配置 静态文件的例子

yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
  namespace: ma
data:
  default.conf: |
    server {
        listen 80;
        server_name localhost;

        location / {
            root /usr/share/nginx/html;
            try_files $uri $uri/ =404;
        }

        location = /.well-known/apple-developer-merchantid-domain-association {
            root /usr/share/nginx/html;
            default_type text/plain;
        }
    }
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: well-known-file
  namespace: ma
data:
  apple-developer-merchantid-domain-association: |
    109 97 108 101 109 97 46 110 101 116 这边是你自己完整的内容
  apple-developer-merchantid-domain-association.txt: |
    2222 上面的因为没有文件名, content-type 跟别人是不一样
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: static-file-nginx
  namespace: ma
spec:
  replicas: 1
  selector:
    matchLabels:
      app: static-file-nginx
  template:
    metadata:
      labels:
        app: static-file-nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        volumeMounts:
        - name: nginx-config-volume
          mountPath: /etc/nginx/conf.d
        - name: well-known-file
          mountPath: /usr/share/nginx/html/.well-known/
        resources:
          requests:
            memory: 50Mi
            cpu: 5m 
          limits:
            memory: 200Mi
            cpu: 50m
      volumes:
      - name: nginx-config-volume
        configMap:
          name: nginx-config
      - name: well-known-file
        configMap:
          name: well-known-file
---

apiVersion: v1
kind: Service
metadata:
    name: static-file-nginx
    namespace: ma
spec:
    ports:
    - port: 80 
    selector:
        app: static-file-nginx    

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: static-file
  namespace: ma
spec:
  ingressClassName: nginx
  tls:
    - hosts:
      - xxx.xxx.com
      secretName: ma-ingress-tls
  rules:
  - host: xxx.xxx.com
    http:
      paths:
      - pathType: Prefix
        path: /.well-known/
        backend:
          service:
            name: static-file-nginx
            port:
              number: 80

如何来确保我们的文件的 content type 是 text/plian

除了直接使用 nginx config 我们还可以 把原始文件改成 txt后缀,用 ingress来重写

yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: static-file-dev-2
  namespace: vs-dev
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /.well-known/apple-developer-merchantid-domain-association.txt
spec:
  ingressClassName: nginx
  tls:
    - hosts:
      - abc.malema.net
      secretName: dev-ingress-tls
  rules:
  - host: abc.malema.net
    http:
      paths:
      - pathType: Exact
        path: /.well-known/apple-developer-merchantid-domain-association
        backend:
          service:
            name: static-file-nginx
            port:
              number: 80

简单就是美