简体   繁体   English

未使用 nginx 和 docker 加载静态文件

[英]Static files not loading using nginx and docker

I'm having approximately the same error as in this question , as concerning Django, all my pages are loaded correctly but not my static files.我在这个问题中遇到了大致相同的错误,关于 Django,我的所有页面都正确加载,但不是我的静态文件。 I guess it has something to do with an error in my configuration files, however even though I've been searching for quite some time, I couldn't find anything to solve my problem.我想这与我的配置文件中的错误有关,但是即使我已经搜索了很长时间,我也找不到任何可以解决我的问题的东西。

Here is my Django Dockerfile :这是我的 Django Dockerfile:

FROM python:3.10.5-alpine

RUN pip install --upgrade pip

RUN wget https://upload.wikimedia.org/wikipedia/commons/b/b9/First-google-logo.gif -O media/media.gif

COPY ./requirements.txt .
RUN pip install -r requirements.txt
COPY ./src /app

WORKDIR /app

COPY ./entrypoint.sh /
ENTRYPOINT ["sh", "/entrypoint.sh"]

nginx.conf nginx.conf

upstream django {
    server django_gunicorn:8000;
}

server {
    listen 80;
    server_name 127.0.0.1;

    location / {
        proxy_pass http://django;
    }

    location /static/ {
        alias /static/;
    }

    location /media/ {
        alias /media/;
    }
}

nginx Dockerfile nginx Dockerfile

FROM nginx:1.19.0-alpine

COPY ./nginx.conf /etc/nginx/conf.d/nginx.conf

docker-compose.yml码头工人-compose.yml

version: '3.7'

services:
  django_gunicorn:
    volumes:
      - static:/static
      - media:/media
    env_file:
      - env
    build:
      context: .
    ports: 
      - "8000:8000"
  nginx:
    build: ./nginx
    volumes:
      - static:/static
      - media:/media
    ports:
      - "80:80"
    depends_on:
      - django_gunicorn

volumes:
  static:
  media:

And I get errors like that :我得到这样的错误:

testapp-django_gunicorn-1  |
testapp-django_gunicorn-1  | 9771 static files copied to '/app/static'.
testapp-django_gunicorn-1  | [2022-07-21 12:27:36 +0000] [9] [INFO] Starting gunicorn 20.1.0
testapp-django_gunicorn-1  | [2022-07-21 12:27:36 +0000] [9] [INFO] Listening at: http://0.0.0.0:8000 (9)
testapp-django_gunicorn-1  | [2022-07-21 12:27:36 +0000] [9] [INFO] Using worker: sync
testapp-django_gunicorn-1  | [2022-07-21 12:27:36 +0000] [10] [INFO] Booting worker with pid: 10
testapp-nginx-1            | 172.20.0.1 - - [21/Jul/2022:12:30:45 +0000] "GET /scripts/ HTTP/1.1" 200 17460 "http://127.0.0.1/scripts/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36" "-"
testapp-nginx-1            | 172.20.0.1 - - [21/Jul/2022:12:30:45 +0000] "GET /static/scripts/bootstrap/css/bootstrap.css HTTP/1.1" 404 555 "http://127.0.0.1/scripts/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36" "-"
testapp-nginx-1            | 2022/07/21 12:30:45 [error] 30#30: *1 open() "/static/scripts/bootstrap/css/bootstrap.css" failed (2: No such file or directory), client: 172.20.0.1, server: 127.0.0.1, request: "GET /static/scripts/bootstrap/css/bootstrap.css HTTP/1.1", host: "127.0.0.1", referrer: "http://127.0.0.1/scripts/"

EDIT : entrypoint.sh编辑: entrypoint.sh

#!/bin/sh

python manage.py migrate
python manage.py collectstatic

gunicorn main.wsgi:application --bind 0.0.0.0:8000

Your nginx container is looking for static in /static/ but the logs show you're collecting static in /app/static/ in the gunicorn container.您的 nginx 容器正在/static/中寻找静态,但日志显示您正在 gunicorn 容器中的/app/static/中收集静态。 In docker-compose, did you try - static:/app/static on the gunicorn side and - static:/static on the nginx side?在 docker-compose 中,您是否在 gunicorn 端尝试过 -static - static:/app/static ,在 nginx 端尝试过 -static - static:/static

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

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