您的位置:首页 > 运维架构 > Nginx

Fedora20 + Nginx + uWSGI + Django环境下访问静态文件可能遇到的问题

2015-07-05 19:34 766 查看
首先要分清开发环境和生产环境:

1. 开发环境下,可以使用以下两种方法来访问静态文件:

1.1 settings.py

STATIC_ROOT = ''

# URL prefix for static files.

# Example: "http://example.com/static/", "http://static.example.com/"

STATIC_URL = '/static/'

# Additional locations of static files

STATICFILES_DIRS = (

# Put strings here, like "/home/html/static" or "C:/www/django/static".

# Always use forward slashes, even on Windows.

# Don't forget to use absolute paths, not relative paths.

"/root/Documents/code-coverage/",

"/virtqetools-autotest-log-nfs/",

)

1.2 url.py中设置:

(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root':
os.path.join(settings.SITE_ROOT,'media')},name="media"),

实例:

(r'^static/(?P<path>.*)$','django.views.static.serve',{'document_root':'/root/Documents/code-coverage/',
'show_indexes': True}),

这里使用了直接路径代替os,path.join,另外show_indexes是可以显示目录,不加的话访问目录会提示403

2. 生产环境中,使用nginx,static的访问是由nginx来负责的,以上代码就不起作用了:

首先是配置文件的写法:

server {

listen 80;

server_name localhost 127.0.0.1;

#charset koi8-r;

#access_log logs/host.access.log main;

#access_log /root/Documents/django/kaeoq/logs/access.log;

#error_log /root/Documents/django/kaeoq/logs/error.log;

location / {

root /root/Documents/django/kaeoq/kaeoq;

#index index.html index.htm;

include uwsgi_params;

uwsgi_pass 127.0.0.1:9090;

access_log off;

}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html

#

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

location /static {

alias /root/Documents/code-coverage;

#index index.html index.htm;

}

}

网上有很多教程,关于static的配置写法也有很多,要注意alias和root的区别,root /path相当于访问static的时候是访问/path/static,而alias /path相当于访问static时是访问/path/

3. 在正确设置了配置文件后,仍然可能会遇到一些问题,例如:

3.1在uwsgi的最开始的配置过程中,日志中出现的问题的解决如下:

Listen queue size is greater than the system max net.core.somaxconn (128)

看日志很明显,是listen数目设置大了,将原来的listen由200改为100,为题解决。

3.2 unavailable modifier requested:

yum install uwsgi-plugin-python

然后在运行uwsgi时加上--plugin python

uwsgi -i uwsgi.ini --plugin python

3.3 权限配置不正确:
解决办法: 设置所有父目录为755权限,设置文件为644权限可以避免权限不正确。

     或:修改nginx.conf 文件修改nginx启动用户 。

或:修改www-data对相应目录的读及写权限。

3.4 无法访问目录:

在nginx.conf文件中的http 里加入

autoindex on;# 显示目录

4. 谨慎使用uwsgi中的processes和worker,但是可以使用threads,尤其是使用了全局变量/锁的时候,可能出现问题:

[uwsgi]
socket=127.0.0.1:9090
listen=100
workers=1
threads=4
pidfile=/root/Documents/django/kaeoq/uwsgi.pid
pythonpath=/root/Documents/django/kaeoq/
chdir=/root/Documents/django/kaeoq/kaeoq/
module=kaeoq.wsgi:application
profiler=true
logdate=true
daemonize=/root/Documents/django/kaeoq/log/virtqetools.log
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: