无法使用NGINX,gunicorn和Django访问静态文件

时间:2015-04-13 08:48:03

标签: django nginx gunicorn

我有一个通过ajenti与NGINX和Django一起运行的项目。不幸的是我无法遍历静态目录。我已设置索引并尝试手动遍历它但我被困在同一目录中。

NGINX配置:

server {
    listen *:80 default_server;

    server_name testsite.com;

    access_log /var/log/nginx/testsite.access.log;
    error_log /var/log/nginx/testsite.error.log;

    root /var/www/servers/testsite.com/testsite;
    index index.html index.htm index.php;


    location  / {



        proxy_pass http://unix:/var/run/ajenti-v-gunicorn-testsite-python-wsgi-0.sock;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    }

    location ~ /static/ {
        alias /var/www/servers/testsite.com/testsite/public/static/;


        autoindex on;

    }

}

gunicorn进程:

root      2829  0.9  2.4  57896 12312 ?        S    13:25   0:00 /var/www/servers/testsite.com/env/bin/python /var/www/servers/testsite.com/env/bin/gunicorn -c /etc/gunicorn.ajenti.d//testsite_python_wsgi_0.py testsite.wsgi
www-data  2842  2.1  5.0  83572 25928 ?        S    13:25   0:00 /var/www/servers/testsite.com/env/bin/python /var/www/servers/testsite.com/env/bin/gunicorn -c /etc/gunicorn.ajenti.d//testsite_python_wsgi_0.py testsite.wsgi
www-data  2849  1.9  4.7  82056 24208 ?        S    13:25   0:00 /var/www/servers/testsite.com/env/bin/python /var/www/servers/testsite.com/env/bin/gunicorn -c /etc/gunicorn.ajenti.d//testsite_python_wsgi_0.py testsite.wsgi
www-data  2858  1.8  4.7  82064 24212 ?        S    13:25   0:00 /var/www/servers/testsite.com/env/bin/python /var/www/servers/testsite.com/env/bin/gunicorn -c /etc/gunicorn.ajenti.d//testsite_python_wsgi_0.py testsite.wsgi

NGINX流程:

ps -ef | grep nginx
root     29364     1  0 11:40 ?        00:00:00 nginx: master process /usr/sbin/nginx
www-data 29365 29364  0 11:40 ?        00:00:00 nginx: worker process
root     29378 26678  0 11:43 pts/0    00:00:00 grep nginx

目录权限:

ls -ls
total 52
36 -rwxrwxrwx 1 www-data www-data 36864 Apr 10 10:58 db.sqlite3
 4 -rwxrwxrwx 1 www-data www-data   250 Apr 10 10:58 manage.py
 4 drwxrwxrwx 3 www-data www-data  4096 Apr 10 10:59 public
 4 drwxrwxrwx 2 www-data www-data  4096 Apr 10 11:29 testsite
 4 -rwxrwxrwx 1 www-data www-data   169 Apr 10 11:08 testsite.wsgi

图片显示无法遍历目录。

enter image description here

1 个答案:

答案 0 :(得分:1)

尝试在urls.py文件中包含此内容:

    admin_media_path = os.path.join(django.__path__[0], 'contrib', 'admin', 'static', 'admin')

    urlpatterns = patterns('',
        url(r'^static/admin/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': admin_media_path,
        }),
)

这应该可以解决问题。