媒体文件不显示:open()“/home/shahriar/Amlak/real_estate/media/images/image1.jpg”失败(13:Permission denied)- Django Nginx

23c0lvtd  于 2023-10-21  发布在  Go
关注(0)|答案(1)|浏览(96)

我试图在我的网站中查看我的媒体文件,但我不能。静态文件是确定的,但媒体文件,尽管我所有的努力是不可见的。
下面是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。
指向媒体文件的路径是正确的,但网站似乎无法获取它们。

ac1kyiln

ac1kyiln1#

我是这样做的:

  • 首先,我检查了nginx错误日志:nano /var/log/nginx/error.log
  • 然后我改变了我的网站nginx配置:location /media { root /home/path/to/real_estate(projectname)/; }
  • 然后我给当前用户这样的权限,通过改变nginx.conf:

nano /etc/nginx/nginx.conf
然后我将用户从www-data更改为我的用户名(当前用户)。

  • 网址:settings.py

MEDIA_URL = 'media/' MEDIA_ROOT = BASE_URL/ 'media/'

  • 然后我重新启动了nginx和gunicorn服务。

瞧!!成功了...

相关问题