jos*_*iti 4 python django nginx
在调试模式下开发Django应用程序时,我使用以下代码提供静态文件:
if settings.DEBUG:
urlpatterns += patterns('',
(r'^m/(?P<path>.*)$', serve, {
'document_root' : os.path.join(os.path.dirname(__file__), "media")
})
)
Run Code Online (Sandbox Code Playgroud)
我正在使用nginx作为前端,使用以下nginx配置在生产模式下为我的静态文件服务:
location m/
{
root /path/to/folder/media/;
}
Run Code Online (Sandbox Code Playgroud)
这看起来不太理想,因为我必须在媒体目录中创建一个"m"文件夹.我想知道其他人的Django/nginx配置文件是什么样的.具体来说,请你剪切并粘贴nginx.confg和urls.py的部分(settings.DEBUG == True)
谢谢.
zee*_*kay 10
使用Django 1.3 django.contrib.staticfiles
将在开发过程中为您提供一切服务.你不需要在urls.py中做任何特别的事情.在Django 1.3更新后,我为自己写了一个小指南,涵盖了要使用的设置:
# idiom to get path of project
import os
PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
# url prefix for user uploaded files, stuff that django has to serve directly
MEDIA_URL = '/media/'
# url prefix for static files like css, js, images
STATIC_URL = '/static/'
# url prefix for *static* /admin media
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'
# path to django-served media
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')
# path used for collectstatic, *where the webserver not django will expect to find files*
STATIC_ROOT = '/home/user/public_html/static'
# path to directories containing static files for django project, apps, etc, css/js
STATICFILES_DIRS = (
os.path.join(PROJECT_PATH, 'static'),
)
# List of finder classes that know how to find static files in various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
# Required for all the magic
INSTALLED_APPS = (
'django.contrib.staticfiles',
)
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅文档:http://docs.djangoproject.com/en/1.3/howto/static-files/.
我使用nginx和uwsgi在生产中提供django应用程序(我使用runserver进行开发).我将我/static
和/media
文件夹(来自我的django项目)符号链接到/var/www/vhosts/domain.com/html
nginx中查找.您也可以使用collectstatic
命令而不是符号链接.如果它找不到静态文件,则会回退到uwsgi(运行django应用程序).
而不是uwsgi你可以使用fast-cgi,或proxy_pass或任何你想要的.我更喜欢uwsgi,因为它具有令人难以置信的功能和出色的性能.我运行uwsgi作为守护进程:uwsgi --emperor '/srv/*/*.ini'
.这是一个相当新的选项,它告诉uwsgi扫描给定路径的配置文件.当emperor uwsgi守护进程找到配置文件时,它会使用找到的配置启动一个新的uwsgi实例.如果您更改配置,emperor uwsgi守护程序将为您注意并重新启动您的应用程序.您也可以触摸配置文件重新加载,就像使用mod_wsgi一样,一旦完成所有配置,就可以轻松设置新应用程序.
我遵循的路径约定是:
/srv/venv/ - virtualenv for project
/srv/venv/app.ini - configuration for uwsgi
/srv/venv/app.sock - uwsgi sock for django
/srv/venv/app.wsgi - wsgi file for uwsgi
/srv/venv/proj - django project
/srv/venv/proj/settings.py - project settings file
/srv/venv/proj/static - static files dir, linked into var/www/vhosts/domain.com/html
/srv/venv/proj/static/admin - admin static files, linked as well
/srv/venv/proj/media - media files dir
/var/www/vhosts/domain.com/html - base directory for nginx to serve static resources from
Run Code Online (Sandbox Code Playgroud)
这是我的nginx.conf:
location / {
root /var/www/vhosts/domain.com/html;
index index.html index.html;
error_page 404 403 = @uwsgi;
log_not_found off;
}
location @uwsgi {
internal;
include /etc/nginx/uwsgi_params;
uwsgi_pass unix:/srv/venv/app.sock;
}
Run Code Online (Sandbox Code Playgroud)
我的uwsgi ini文件(你也可以使用xml/yaml/etc):
[uwsgi]
home = /srv/%n
pp = /srv/%n
wsgi-file = /srv/%n/%n.wsgi
socket = /srv/%n/%n.sock
single-intepreter = true
master = true
processes = 2
logto = /srv/%n/%n.log
Run Code Online (Sandbox Code Playgroud)
你也应该看看gunicorn,它有非常好的django集成和良好的性能.
归档时间: |
|
查看次数: |
6830 次 |
最近记录: |