Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 错误404 Nginx can';找不到媒体文件+;德扬戈_Django_Linux_Nginx_Http Status Code 404_Media - Fatal编程技术网

Django 错误404 Nginx can';找不到媒体文件+;德扬戈

Django 错误404 Nginx can';找不到媒体文件+;德扬戈,django,linux,nginx,http-status-code-404,media,Django,Linux,Nginx,Http Status Code 404,Media,我正在用nginx建立基本的Django站点。当我导航到文件的url时,我得到错误404 Not Found nginx/1.18.0(Ubuntu) 这是conf文件 upstream django { server unix:///home/jo/testproject/testproject.sock; } # configuration of the server server { listen 80; server_name _; charse

我正在用nginx建立基本的Django站点。当我导航到文件的url时,我得到错误404 Not Found nginx/1.18.0(Ubuntu)

这是conf文件

upstream django {
    server unix:///home/jo/testproject/testproject.sock;
}
# configuration of the server
server {
    listen      80;
    server_name _;
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;

    # Django media and static files
    location /media/  {
    alias /home/jo/testproject/media/;
    }
    location /static {
        alias /home/jo/testproject/static;
    }

    # Send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /home/jo/testproject/uwsgi_params;
    }
}
媒体文件夹位于/home/jo/testproject/media中,其中有media.gif,但ip/media/media.gif不起作用

当我在浏览器中写入ip时,我得到

欢迎来到nginx! 如果您看到此页面,则表明nginx web服务器已成功安装并正常工作。需要进一步配置

有关在线文档和支持,请参阅nginx.org。 可在nginx.com上获得商业支持

感谢您使用nginx


我认为我的其他设置都正确

Docker+NGINX+Gunicorn+Django

Django项目:

djangoapp
 - ...
 - media
 - static
 - manage.py
 - requirements.txt  
nginx
 - Dockerfile
 - nginx.conf 
docker-compose.yml  
Dockerfile
Dockerfile:

FROM ubuntu:20.04

# Prevents Python from writing pyc files to disc
ENV PYTHONDONTWRITEBYTECODE 1
# Prevents Python from buffering stdout and stderr
ENV PYTHONUNBUFFERED 1
ENV DEBUG 0

# https://hub.docker.com/r/fnndsc/ubuntu-python3/dockerfile
RUN apt-get update \
  && apt-get install -y python3-pip gunicorn

RUN mkdir /app

COPY djangoapp/requirements.txt requirements.txt
COPY djangoapp /app/

RUN pip3 install -r requirements.txt
WORKDIR /app/

EXPOSE 8000

CMD ["gunicorn3", "-b", "0.0.0.0:8000", "djangoapp.wsgi:application", "--workers=2"]
FROM nginx

RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/
docker-compose.yml:

version: '3'

services:
  djangoapp:
    build: .
    container_name: djangoapp1
    volumes:
      - .:/djangoapp
      - static:/app/static
      - media:/app/media

  nginx:
    build: ./nginx
    container_name: nginx
    environment:
      - SERVER_NAME=8.43.162.54
    ports:
      - "80:80"
    volumes: 
      - .:/djangoapp
      - static:/app/static
      - media:/app/media
    restart: always

networks: 
  default:
    driver: bridge

volumes:
  media:
  static:
nginx/Dockerfile:

FROM ubuntu:20.04

# Prevents Python from writing pyc files to disc
ENV PYTHONDONTWRITEBYTECODE 1
# Prevents Python from buffering stdout and stderr
ENV PYTHONUNBUFFERED 1
ENV DEBUG 0

# https://hub.docker.com/r/fnndsc/ubuntu-python3/dockerfile
RUN apt-get update \
  && apt-get install -y python3-pip gunicorn

RUN mkdir /app

COPY djangoapp/requirements.txt requirements.txt
COPY djangoapp /app/

RUN pip3 install -r requirements.txt
WORKDIR /app/

EXPOSE 8000

CMD ["gunicorn3", "-b", "0.0.0.0:8000", "djangoapp.wsgi:application", "--workers=2"]
FROM nginx

RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/
nginx/nginx.conf:

server {
    listen 80;
    server_name $SERVER_NAME;

    location /static/ {     
        alias /app/static/;
    }

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

    location / {
        proxy_set_header Host $host;
        proxy_pass http://djangoapp1:8000;
    }
}
settings.py:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = []

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'