简体   繁体   English

使用nginx + gunicorn服务在django部署中未显示的媒体

[英]media not displayed in django deploy using nginx + gunicorn service

I am deploying a django app in a VPS (debian 9) using Nginx and gunicorn service. 我正在使用Nginx和gunicorn服务在VPS(debian 9)中部署django应用程序。 Everything works as expected but images uploaded by the user in media file are not displayed, 404 (not found). 一切都按预期工作,但不显示用户在媒体文件中上传的图像,404(未找到)。 When I use Debug=True images are displayed, Static file works correctly in development and production. 当我使用Debug=True图像时,静态文件在开发和生产中正常工作。

To run this app I have created a webuser without sudo rights and a group called webapps. 为了运行这个应用程序,我创建了一个没有sudo权限的webuser和一个名为webapps的组。 Since it seems that nginx can't see the media I have changed the group to www-data, but still, it doesn't work. 由于看起来nginx看不到媒体我已经将组更改为www-data,但仍然无法正常工作。 There is a similar issue here but without an accepted answer. 这里有一个类似的问题但没有一个公认的答案。 Any help will be much appreciated. 任何帮助都感激不尽。

Bellow some important configurations: Bellow一些重要的配置:

(web_env) webuser@:~/web_project$ ls -la
total 72
drwxrwxr-x 10 webuser www-data  4096 Jun 11 20:30 .
drwxr-xr-x  3 webuser webapps   4096 Jun 10 17:15 ..
drwxr-xr-x  6 webuser webapps   4096 Jun 10 17:15 blog
-rw-r--r--  1 webuser webapps    655 Jun 10 17:15 environment.yaml
drwxr-xr-x  2 webuser webapps   4096 Jun 10 17:15 .idea
drwxr-xr-x  2 webuser webuser   4096 Jun 11 17:28 logs
-rwxr-xr-x  1 webuser webapps    631 Jun 10 17:15 manage.py
drwxrwxr-x  3 webuser www-data  4096 Jun 10 17:15 media
-rw-r--r--  1 webuser webapps  14417 Jun 10 17:15 posts.json
-rw-r--r--  1 webuser webapps    229 Jun 10 17:15 README.md
-rw-r--r--  1 webuser webapps    161 Jun 10 17:15 requirements.txt
drwxrwxr-x  2 webuser www-data  4096 Jun 19 13:47 run
drwxrwxr-x  4 webuser www-data  4096 Jun 11 16:31 static
drwxr-xr-x  5 webuser webapps   4096 Jun 16 12:12 users
drwxr-xr-x  3 webuser webapps   4096 Jun 19 15:52 web_project

Gunicorn service in /etc/systemd/system/gunicorn.service Gunicorn服务于/etc/systemd/system/gunicorn.service

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=webuser
Group=www-data
WorkingDirectory=/home/webuser/DjangoProjects/web_project
ExecStart=/home/webuser/.conda/envs/web_env/bin/gunicorn \
      --access-logfile - \
      --workers 3 \
      --bind 
unix:/home/webuser/DjangoProjects/web_project/run/gunicorn.soc$
      web_project.wsgi:application

[Install]
WantedBy=multi-user.target

My nginx configuration 我的nginx配置

server {
    listen         80;
    server_name    mysite.com;
    client_max_body_size 100M; #to have space to load images
    return         301 https://$server_name$request_uri;
}

server {
    listen         443 ssl;
    server_name    mysite.com www.mysitet;
    client_max_body_size 100M; #to have space to load images

    ssl on;
    ssl_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mysite.com/privkey.pem;

    location /static/ {
            autoindex on;
            alias /home/webuser/DjangoProject/web_project/static/;
    }

    location /media/ {
            autoindex on;
            alias /home/webuser/DjangoProject/web_project/media/;
    }

    location / {
            include proxy_params;           
            proxy_pass http://unix:/home/webuser/DjangoProjects/web_project/run/gunicorn.sock;

    }
}

Some parts of my settings.py 我的settings.py的一些部分

import os
import json

with open('/etc/config.json') as config_file:
    config = json.load(config_file)

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


SECRET_KEY = config['SECRET_KEY']
DEBUG = False
ALLOWED_HOSTS = ['mysite.com', '198.XX.XX.XXX']

INSTALLED_APPS = [
  'blog.apps.BlogConfig',
  'users.apps.UsersConfig',
  'crispy_forms',
  'django.contrib.admin',
  'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.messages',
  'django.contrib.staticfiles',
]

ROOT_URLCONF = 'web_project.urls'

TEMPLATES = [
  {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [
        ''
    ],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
  },
]

WSGI_APPLICATION = 'web_project.wsgi.application'

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': 'dbname',
    'USER': 'webuser',
    'PASSWORD': 'dbpassword',
    'HOST': 'localhost',
    'PORT': '',
  }
}

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

in urls.py I have added 在urls.py我添加了

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Of course after each change I use to run 当然,每次更改后我都会用

sudo systemctl restart nginx
sudo pkill gunicorn
sudo systemctl daemon-reload
sudoo systemctl start gunicorn

EDITED EDITED

/var/logs/nginx 在/ var /日志/ nginx的

2019/06/19 20:17:13 [error] 17420#17420: *53 open() "/home/webuser/DjangoProject/web_project/media/profile_pics/StackOverFlowPicture_TL8URPF.png" failed (2: No such file or directory), client: 109.132.181.209, server: example.com, request: "GET /media/profile_pics/StackOverFlowPicture_TL8URPF.png HTTP/1.1", host: "example.com", referrer: "https://example.com/"
2019/06/19 20:17:17 [error] 17420#17420: *53 open() "/home/webuser/DjangoProject/web_project/media/default.jpg" failed (2: No such file or directory), client: 109.132.181.209, server: example.com, request: "GET /media/default.jpg HTTP/1.1", host: "example.com", referrer: "https://example.com/profile/"

the original picture is StackOverFlowPicture.png but know I have another picture 原始图片是StackOverFlowPicture.png,但知道我有另一张图片

webuser@:~/DjangoProjects/web_project/media/profile_pics$ ls
StackOverFlowPicture.png  StackOverFlowPicture_TL8URPF.png

Thanks to Ivan Starostin who remarked my typo error DjangoProjects != DjangoProject and my last log error 感谢Ivan Starostin,他评论了我的拼写错误DjangoProjects != DjangoProject和我的上次日志错误

2019/06/20 11:06:46 [error] 22983#22983: *1 open() "/home/webuser/DjangoProjects/web_project/media/media/profile_pics/StackOverFlowPicture_TL8URPF.png" failed (2: No such file or directory), client: 109.132.217.176, server: example.com, request: "GET /media/profile_pics/StackOverFlowPicture_TL8URPF.png HTTP/1.1", host: "example.com", referrer: "https://example.com/"

I have solved the problem by changing 我通过改变解决了这个问题

location /media/ {
        autoindex on;
        alias /home/webuser/DjangoProject/web_project/media/;
}

to

location /media/ {
        autoindex on;
        alias /home/webuser/DjangoProject/web_project;
}

And now the pictures are loaded. 现在图片已加载。 Thanks for your help! 谢谢你的帮助!

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

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