我试图在我的网站中查看我的媒体文件,但我不能。静态文件是确定的,但媒体文件,尽管我所有的努力是不可见的。
下面是nginx错误日志:
2023/10/17 11:59:33 [error] 48068#48068: *7 open() "/home/shahriar/Amlak/real_estate/media/images/ویلا-چمستان-1_uWh54BB.jpg" failed (13: Permi>
2023/10/17 11:59:33 [error] 48068#48068: *6 open() "/home/shahriar/Amlak/real_estate/media/images/IMG_20230828_214539_i6yw2Zc.jpg" failed (13:>
2023/10/17 11:59:33 [error] 48068#48068: *5 open() "/home/shahriar/Amlak/real_estate/media/images/image1.jpg" failed (13: Permission denied), >
2023/10/17 11:59:33 [error] 48068#48068: *4 open() "/home/shahriar/Amlak/real_estate/media/images/ویلا-چمستان-1_0kmP7E3.jpg" failed (13: Permi>
2023/10/17 11:59:33 [error] 48068#48068: *3 open() "/home/shahriar/Amlak/real_estate/media/images/455008604_479875.jpg" failed (13: Permission>
我的网站的nginx配置文件:
location /static {
alias /var/www/real_estate/static/;
}
location /media {
root /home/path/to/real_estate(projectname)/;
}
settings.py:
STATIC_URL = 'static/'
STATIC_ROOT = '/var/www/real_estate/static/'
STATICFILES_DIRS = [BASE_DIR/'static/',]
# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# Media Settings
MEDIA_URL = 'media/'
MEDIA_ROOT = BASE_DIR / 'media/'
我试着这样给我的媒体文件夹权限:
sudo chown -R www-data:www-data /var/www/real_estate/static
sudo chown -R www-data:www-data /home/shahriar/Amlak/real_estate/media
我尝试重新启动nginx.service和gunicorn.service。
指向媒体文件的路径是正确的,但网站似乎无法获取它们。
1条答案
按热度按时间ac1kyiln1#
我是这样做的:
nano /var/log/nginx/error.log
location /media { root /home/path/to/real_estate(projectname)/; }
nano /etc/nginx/nginx.conf
然后我将用户从www-data更改为我的用户名(当前用户)。
MEDIA_URL = 'media/' MEDIA_ROOT = BASE_URL/ 'media/'
瞧!!成功了...