使用 nginx 和 uwsgi 部署 django 项目,能够正常访问各个页面
目标功能是进入 admin 页面上传图片,即时显示在首页上
目前通过 admin 上传图片后,图片会存放在 django 项目的 static/media/upload 目录下,ng 的静态文件目录下没有
由于设置了 nginx,所以现在首页无法显示图片,因为图片在 /var/www/static/projectName 目录下没有
网上找到一种 将图片目录重定向到 nginx 的的静态文件目录 的方案,可惜没有解决
这里请教一下各位老哥,应该如何配置可以实现在 admin 中上传图片后,可以直接存在 /var/www/static/projectName 目录下(也就是 ng 的静态文件目录)
非常感谢,希望大家能不吝吐槽和赐教!!!
upstream django {
server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}
server {
listen 80;
charset utf-8;
client_max_body_size 75M;
location /media {
alias /var/www/NickBlog/static;
}
location /static {
alias /var/www/NickBlog/static;
}
location / {
uwsgi_pass django;
include uwsgi_params;
}
}
[uwsgi]
# Django-related settings
socket = :8000
# the base directory (full path)
chdir = /root/NickBlog/
wsgi-file = /NickBlog/wsgi.py
# Django s wsgi file
module =NickBlog.wsgi
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 4
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
pidfile=uwsgi.pid
daemonize=uswgi.log
STATIC_URL = '/static/'
STATIC_ROOT = '/var/www/NickBlog/static/'
# 公共的 static 文件
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
os.path.join(BASE_DIR, "static/media"),
)
# upload floder
MEDIA_URL = '/static/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/media')
STATICFILES_FINDERS = ("django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",)
1
myyou 2018-02-05 11:08:27 +08:00 2
```
# Django media location /media { alias /var/www/NickBlog/static/media; # your Django project's media files - amend as required } ``` 改为这样试试 |
2
misaka19000 2018-02-05 11:18:34 +08:00 1
为什么不直接把图片上传到 CDN 上面呢
|
3
zhengxiaowai 2018-02-05 12:44:19 +08:00 1
你需要 FilePathField,see -> https://docs.djangoproject.com/en/2.0/_modules/django/forms/fields#FilePathField
|
4
Nick2VIPUser OP |
5
luhuisicnu 2018-02-06 10:14:02 +08:00
MEDIA_ROOT 直接改成 nginx 配置的目录,给 web 进程读写权限,就直接存进去了。
|
6
Nick2VIPUser OP @luhuisicnu 后来我尝试这个方法,成功了!非常感谢大佬!
|