【问题标题】:Error 404 Nginx can't find media files. +Django错误 404 Nginx 找不到媒体文件。 +姜戈
【发布时间】:2021-07-22 20:20:13
【问题描述】:

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

这是配置文件

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。

我认为其他所有设置都正确

【问题讨论】:

    标签: django linux nginx http-status-code-404 media


    【解决方案1】:

    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"]
    

    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 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/'
    

    【讨论】:

      【解决方案2】:

      sudo nano /etc/nginx/sites-enabled/inventory.conf

      上游组件nginx需要连接

      upstream django {
          server unix:///home/user/Inventory-System/inventory.sock;
      }
      
      #configuration of the server
      server {
          listen      80;
          server_name trading.code www.trading.code;
          charset     utf-8;
          # max upload size
          client_max_body_size 75M;
          # Django media and static files
          location /media {
              autoindex on;
              alias home/user/Inventory-System/media/;
          }
      
      location /static {
          autoindex on;
          alias home/user/Inventory-System/static/;
      }
      # Send all non-media requests to the Django server.
      location / {
          uwsgi_pass  django;
          include    home/user/Inventory-System/uwsgi_params;
      } }
      

      然后

      user@user: /etc/nginx/sites-enabled$ sudo rm 默认
      user@user: $ sudo /etc/init.d/nginx 重启

      终于

      查看http://127.0.0.0/media/

      【讨论】:

        猜你喜欢
        • 2017-01-14
        • 2011-08-28
        • 2017-11-09
        • 1970-01-01
        • 1970-01-01
        • 2019-05-06
        • 2016-07-16
        • 2013-09-05
        • 2021-12-03
        相关资源
        最近更新 更多