【问题标题】:Why is static content request going to uwsgi?为什么静态内容请求会转到 uwsgi?
【发布时间】:2016-11-04 13:12:35
【问题描述】:

我正在设置我的 Django 项目以使用 uwsgi 和 nginx。对于静态内容,我的 nginx.conf 中有以下内容:

location /static {
    alias /Users/me/mystatic; # your Django project's static files - amend as required
}

我已将STATIC_ROOT 设置为/Users/me/mystatic 并调用collectstatic 将所有静态文件复制到该目录中。在我的 uwsgi 日志中,我看到了对静态内容的 GET 请求。既然 nginx 应该服务静态内容,为什么 GET 请求会发送到 uwsgi?

【问题讨论】:

  • 尝试将 /static/ 添加到位置
  • 如果不成功,请添加 nginx conf 的其余部分,另外,请确保 STATIC_URL 在您的 django 设置中为“/static/”

标签: django nginx uwsgi


【解决方案1】:

确保将STATIC_URL 设置为"/static/"

nginx conf 应该如下所示:

server {

    listen  80 ;
    server_name XXXX;
    client_max_body_size 4G;            

    location /static/ {
        alias <path-to-collectstatic>
    }

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/tmp/uwsgi_web.sock;
    }

}

注意location 块中static 的附加/

最后,确保应用服务器以DEBUG 作为False 运行

【讨论】:

  • 是的,我有STATIC_URL = '/static/'。我今晚试试location /static/,但我想指出nginx-uwsgi-django tutorial 只使用/static
【解决方案2】:

问题是我将自己的 nginx.conf 放在了/usr/local/etc/nginx/sites-enabled,而不是/usr/local/etc/nginx/servers。在/usr/local/etc/nginx/nginx.conf 中,include servers/*; 位于末尾。因此,UWSGI 仍在为静态文件提供服务。我关注了django-nginx-uwsgi tutorial,我认为我必须创建一个新的sites-enabled 目录。我正在使用 OS X。

我在 Linux 上看到,/etc/nginx/nginx.conf 在末尾包含 include /etc/nginx/sites-enabled/*;,因此教程中的步骤适用于 Linux。

/static 末尾的额外 / 没有什么区别,但在所有路径的末尾都有它并没有什么坏处。

【讨论】:

    猜你喜欢
    • 2016-06-14
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    相关资源
    最近更新 更多