【问题标题】:NGINX Container Not Loading Static Files using Traefik / KubernetesNGINX 容器不使用 Traefik / Kubernetes 加载静态文件
【发布时间】:2019-05-22 09:31:44
【问题描述】:

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

静态文件由带有 /static 的自定义 NGINX 容器提供。因此,如果我的域名是 xyz.com,则静态从 xyz.com/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;
}

【问题讨论】:

  • 为什么是PathPrefixStrip?这意味着 Nginx 会看到没有该组件的路径,这似乎与您的配置不匹配。
  • 谢谢,解决了。我已经删除了它。我把它与我以前使用过的 docker-compose 语法混淆了。

标签: nginx kubernetes traefik-ingress


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 2021-06-25
    • 2016-06-13
    • 2020-09-20
    • 2018-08-30
    相关资源
    最近更新 更多