获取静态内容时出现Django + Nginx 500错误

时间:2016-01-13 03:33:18

标签: python django nginx gunicorn

使用Django,Nginx和Gunicorn提供静态内容时遇到问题。我可以从我的/ media /文件夹以及html提供媒体文件(图像),但是对/ static /文件夹的静态请求产生了500个错误。

我运行了collectstatic来收集/ opt / myapp / myapp / myapp / static /

中的所有静态

这是我的Nginx配置:

server {
    listen 80;
    server_name <ip_address>;

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /static/ {
            alias /opt/myapp/myapp/myapp/static/;
    }

    location = /media/ {
            alias /opt/myapp/myapp/myapp/media/;
    }


    location / {
            include proxy_params;
            proxy_pass http://unix:/opt/myapp/myapp/myapp/myapp.sock;
    }
}

Django urls.py:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('core.urls')),
    url(r'^accounts/', include('allauth.urls')),
]  + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 
   + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Django settings.py:

STATICFILES_DIRS = [
    '/opt/myapp/myapp/myapp/core/static',
]

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'static')

来自Nginx access.log的示例(error.log中没有错误):

"GET /static/markup/css/index.css HTTP/1.1" 500 63893 "http://104.131.44.215/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"

1 个答案:

答案 0 :(得分:0)

你的settings.py看起来像什么?

编辑:

  1. 再次查看您的代码。在Nginx中,你有不同的路径,不像settings.py!
  2. 您可能误解了Django文档(staticfiles_dirs vs static_root)
  3. 您的STATICFILES_DIRS应如下所示
  4. STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), )

    1. 如果这不起作用,请更改您的STATIC_ROOT(必须是与staticfiles_dirs不同的路径)并且不要忘记运行collectstatic