简体   繁体   English

使用 Nginx 从 Django-project-folder 外部提供媒体文件

[英]Serve media files from outside the Django-project-folder with Nginx

I'm trying to serve in production (not development) media files for my Django-project with a Nginx-server.我正在尝试使用 Nginx 服务器为我的 Django 项目提供生产(而非开发)媒体文件。 The media-files are not located within the django-folder.媒体文件不在 django 文件夹中。

The folder structure is like:文件夹结构如下:

|- django_user
|     |- media
|     |- Myproject
|          |- static
|          |- myproject
|               |- settings.py

Nginx-Server : Nginx 服务器

upstream websocket{
        server 127.0.0.1:6379;
}
server {
    server_name myproject.com;

    client_max_body_size 5M;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/django_user/Myproject;
    }

    location /media/ {
        root /home/django_user/media;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/myproject.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/myproject.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    location /ws {
        proxy_pass http://websocket;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_redirect off;
        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_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Forwarded-Proto  $scheme;
    }

}
server {
    if ($host = myproject.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name myproject.com
    return 404; # managed by Certbot


}

Here is a part of my settings.py :这是我的settings.py的一部分:

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

MEDIA_ROOT = '/home/django_user/media'

MEDIA_URL = '/media/'

Saving files in the media folder is working.将文件保存在媒体文件夹中。 I've set up an image field like this:我已经设置了一个像这样的图像字段:

myicon = models.ImageField(default="static/images/default_icon.png" , blank=True, upload_to="images/")

I've checked it and the images (eg test.png) are correctly saved in /home/django_user/media/images.我检查过它并且图像(例如test.png)正确保存在/home/django_user/media/images 中。 My problem is that the images in the media-folder cannot be shown on my website (myproject.com/images/test.png), it always shows me:我的问题是媒体文件夹中的图像无法显示在我的网站 (myproject.com/images/test.png) 上,它总是显示给我:

Not Found
The requested resource was not found on this server.

I've done already "manage.py collectstatic" , and static files are loaded correctly.我已经完成了“manage.py collectstatic” ,并且正确加载了 static 个文件。 Only the media-files are not found.仅找不到媒体文件。

I've also tried the following variations of my nginx-server, all without success:我还尝试了我的 nginx-server 的以下变体,但都没有成功:

location /media/ {
     root /home/django_user;
}

location /media {
     root /home/django_user/media/;
}

location /media {
     root /home/django_user/;
}

location /media/ {
    alias /home/django_user/media/;
}

Can you help me with this problem?你能帮我解决这个问题吗? What am I missing here or where is my mistake?我在这里错过了什么或者我的错误在哪里?

Try alias method:尝试 别名方法:

location /media/ {
    alias /home/django_user/media/;
}

I think you are trying with the wrong URL.我认为您正在尝试使用错误的 URL。

If you define:如果你定义:

MEDIA_URL = '/media/ and location /media/ MEDIA_URL = '/media/location /media/

Then the URL should be myproject.com/media/images/test.png instead of myproject.com/images/test.png in both scenarios (development and production)然后 URL 应该是myproject.com/media/images/test.png而不是myproject.com/images/test.png在两种情况下(开发和生产)

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

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