简体   繁体   English

在 Kubernetes 上使用 nginx 和 gunicorn 为 Django 静态文件提供服务

[英]serving Django static files with nginx and gunicorn on kubernetes

I have Django application with Docker, nginx and gunicorn.我有带有 Docker、nginx 和 gunicorn 的 Django 应用程序。

I am trying to use nginx to serve the static files but I am getting 404.我正在尝试使用 nginx 来提供静态文件,但我收到了 404。

here is my nginx.conf:这是我的 nginx.conf:

events {
    worker_connections  1024;
}

http {

    upstream backend {  
        ip_hash;
        server backend:8000;
    }

    server {

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

        location / {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://backend;
        }
        listen 80;
    }

}

kubernetes manifest file: Nginx and app are two separate containers within the same deployment. kubernetes 清单文件:Nginx 和 app 是同一部署中的两个独立容器。

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: backend
  namespace: staging
  labels:
    group: backend
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: <host name>
    http:
      paths:
      - path: /
        backend:
          serviceName: backend
          servicePort: 8000
---
apiVersion: v1
kind: Service
metadata:
  name: backend
  namespace: staging
  labels:
    group: backend
spec:
  selector:
    app: backend
  ports:
  - port: 8000
    targetPort: 8000 
    name: backend
  - port: 80
    targetPort: 80
    name: nginx 
---

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: backend
  namespace: staging
  labels:
    group: backend
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: backend
        group: backend
    spec:
      containers:
      - name: nginx
        image: <image>
        command: [nginx, -g,'daemon off;']
        imagePullPolicy: Always
        ports:
          - containerPort: 80

      - name: backend
        image: <image>
        command: ["gunicorn", "-b", ":8000", "api.wsgi"]
        imagePullPolicy: Always
        ports:
          - containerPort: 8000

settings.py设置.py

STATIC_URL = '/static/'

STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)

Dockerfile for nginx:用于 nginx 的 Dockerfile:

FROM nginx:latest

ADD app/api/static /api/static

ADD config/nginx /etc/nginx

WORKDIR /api

I checked in the nginx container, all static files are present in the /api directory.我检查了 nginx 容器,所有静态文件都存在于 /api 目录中。

You need to create a volume and share it with nginx and your django backend and then python manage.py collectstatic in that volume.您需要创建一个卷并与 nginx 和您的 django 后端共享它,然后在该卷中使用 python manage.py collectstatic 。 But there is a problem, your cluster needs to support ReadWriteMany in accessModes for the pvc storage.但是有一个问题,你的集群需要在accessModes为pvc存储支持ReadWriteMany If you don't have access to this you can create an application that has two containers, your django backend and nginx.如果您无权访问它,您可以创建一个具有两个容器的应用程序,您的 django 后端和 nginx。 Then you can share volumes between them!然后您可以在它们之间共享卷!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM