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

Unable to access static files with NGINX, gunicorn and Django

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

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

图像显示无法遍历目录。

尝试将此包含在您的 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,
        }),
)

这应该可以解决问题。