NGINX容器未使用Traefik / Kubernetes加载静态文件

时间:2018-12-22 03:16:42

标签: nginx kubernetes traefik-ingress

我正在Kubernetes(AKS)上运行Traefik入口控制器。我已经使用Traefik Ingress成功部署了Django应用程序,但是它目前未加载任何静态文件(因此样式无法正常工作)。

静态文件是通过带有/ static的自定义NGINX容器提供的。因此,如果我的域名是xyz.com,则可以从xyz.com/static提供static服务。

apiVersion: v1
kind: Service
metadata:
  name: nginxstaticservice
  labels:
    app: nginxstatic
spec:
  selector:
    k8s-app: traefik-ingress-lb
  ports:
    - name: http
      port: 80
      targetPort: 80
      protocol: TCP
  selector:
    app: nginxstatic

---

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nginxstatic-ingress
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.frontend.rule.type: PathPrefixStrip
    # traefik.ingress.kubernetes.io/frontend-entry-points: http,https
spec:
  rules:
  - host: xyz.com
    http:
      paths:
      - path: /static
        backend:
          serviceName: nginxstaticservice
          servicePort: http

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginxstatic-deployment
  labels:
    app: nginxstatic
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginxstatic
  template:
    metadata:
      labels:
        app: nginxstatic
    spec:
      containers:
      - name: nginxstatic
        image: nginxstatic:latest
        ports:
        - containerPort: 80
      imagePullSecrets:

这是在NGINX容器上运行的default.conf(以前在网站配置中工作。

server {
   listen                      80;
   server_name                 _;
   client_max_body_size        200M;
   set                         $cache_uri $request_uri;

   location                    = /favicon.ico { log_not_found off; access_log off; }
   location                    = /robots.txt  { log_not_found off; access_log off; }
   ignore_invalid_headers      on;
   add_header                  Access-Control-Allow_Origin *;

   location /static {
       autoindex on;
       alias /static;
   }

   location /media {
       autoindex on;
       alias /media;
   }

   access_log                  /var/log/nginx/access.log;
   error_log                   /var/log/nginx/error.log;
}

1 个答案:

答案 0 :(得分:0)

在注释中解决,PathPrefixStrip使用不正确,这导致Nginx看到与预期不同的路径。