直接用uwsgi启动,不实用nginx代理,此时
- setting.py修改
ALLOWED_HOSTS = ('*',)
#setting文件最后添加
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),'static/')
# 设置图片等静态文件的路径
STATICFILES_DIRS = (
('css',os.path.join(STATIC_ROOT,'css').replace('\\','/') ),
('js',os.path.join(STATIC_ROOT,'js').replace('\\','/') ),
('images',os.path.join(STATIC_ROOT,'images').replace('\\','/') ),
('upload',os.path.join(STATIC_ROOT,'upload').replace('\\','/') ),
)
- url.py修改
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import staticfiles
在urls.py文件最后加上以下内容:
#设置静态文件路径
urlpatterns += staticfiles_urlpatterns()
注意:设置静态文件的目录,很关键
- 启动uwsgi
图片和网页可以显示了
参考1
不使用nginx时,uwsgi.ini被放在/root/myvirtualenvs/fishBooked/fishBooked下时
#mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
#chdir = /myvirtualenvs/fishBooked/fishBooked
# Django's wsgi file
module = fishBooked.wsgi
# the virtualenv (full path)
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
#threads = 2
# the socket (use the full path to be safe
socket = 127.0.0.1:8000
# ... with appropriate permissions - may be needed
#chmod-socket = 664
# clear environment on exit
vacuum = true
#virtualenv = /myvirtualenvs/fishBooked/fishBooked
#daemonize = /tmp/mylog.log
不使用nginx,uwsgi.ini被放在/root下时
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = myvirtualenvs/fishBooked/fishBooked
# Django's wsgi file
module = fishBooked.wsgi
# the virtualenv (full path)
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = 127.0.0.1:8000
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
#virtualenv = /myvirtualenvs/fishBooked/fishBooked
#logto = /tmp/mylog.log
使用nginx代理
/etc/nginx/conf.d文件夹下vim uc_nginx.conf
# upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8001;
# the domain name it will serve for
server_name wangfang.cn www.wangfang.cn 111.33.888.66; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
#location /media {
# alias 你的目录/Mxonline/media; # 指向django的media目录
#}
location /static {
alias /myvirtualenvs/fishBooked/fishBooked/static; # 指向django的static目录
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
}
}
此时启动nginx,然后uwsgi -i uwsgi.ini启动uwsgi图片不能出现其它都可以出现
使用python manage.py collectstatic,说不存在路径
再修改setting里的static_root
#未使用nginx时的static_root配置方法
#STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),'static/')
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),'fishBooked/static/')
此时可以python manage.py collectstatic了
但图片还是没有出现,感觉是由于nginx代理以后权限出了问题,所以把location /static 段注释调,让静态文件完全交给Django处理,不过这样子的话图片加载很慢。如果希望图片加载快的话,考虑使用cos