我正在为我的nginx配置苦苦挣扎。使用docker-compose,我将Django应用程序部署到了vps。静态文件工作正常,但我无法正确设置媒体文件。以下是我的conf文件:
setting.py:
STATIC_URL = '/static/'
STATIC_ROOT = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = '/media/'
nginx conf文件:
upstream web {
ip_hash;
server web:8000;
}
# portal
server {
location /static/ {
autoindex on;
alias /static/;
}
location /media/ {
autoindex on;
alias /home/www/book_store/media/;
}
location / {
proxy_pass http://web/;
}
listen 8000;
server_name localhost;
}
docker-compose.yml
version: '2.1'
services:
nginx:
#other commands
volumes:
- .:/code
- ./nginx:/etc/nginx/conf.d
- /static:/static
- /media:/media
depends_on:
- web
web:
#other commands
volumes:
- .:/code
- /static:/static
- /media:/media
应用尝试访问媒体文件时出现错误消息(来自Nginx容器的消息):
ng01 | 2018/11/26 15:52:37 [错误] 5#5:* 6 open() “ /home/www/book_store/media/default.png”失败(2:无此类文件或 目录),客户端:XX.XX.XXX.XXX,服务器:本地主机,请求:“ GET /media/default.png HTTP / 1.1”,主机:“ XXX.XXX.XX.XXX:8000”,引荐来源网址: “ http://XXX.XXX.XX.XXXX:8000/users/user/profile/”