使用 django 从 nginx 提供静态文件 + Debian FastCGI

发布于 2024-11-23 15:26:18 字数 835 浏览 6 评论 0 原文

当谈到使用 django 1.3 提供静态文件的新方式时,我有点生疏,但我确信这不是 django 的问题。

我正在尝试在安装了 debian 的云服务器上运行带有 nginx + fastcgi 的 django 应用程序。我们目前只有一台服务器(在我们开发过程中),并且希望使用负载均衡器运行多台服务器,以使这变得更简单,但是我在实际让 nginx 提供静态文件服务时遇到了麻烦。

我遵循了各种教程来设置 nginx.conf 来提供文件。

server {
    listen 80;
    server_name 127.0.0.1;

    location /static  {
        autoindex on;
        root /static;
    }
}

上面是 nginx.conf 的摘录。现在,无论我将 root 设置为什么,当尝试访问 http://127.0.0.1/static/ 时,nginx 都会抛出 404 not find 错误。

文件结构如下:

/home/user/site/project
/home/user/site/static
/home/user/site/templates

Django settings.py 将以下设置设置为 STATIC_ROOT 和 STATIC_URL

STATIC_ROOT = "/home/user/site/static/"
STATIC_URL  = "http://127.0.0.1/static/"

如果有人可以为我们指出正确的方向,那就太好了。

I'm a little rusty when it comes to the new way of static file serving with django 1.3 however I'm sure it cannot be django that is at fault here.

I'm trying to run a django app with nginx + fastcgi on a cloud server with debian installed. We only have one server at the moment (while we develop) and will be looking to run multiple servers with a load balancer to make this a little simpler however I'm having trouble actually getting nginx to serve static files.

I've followed various tutorials for setting up nginx.conf to serve the files

server {
    listen 80;
    server_name 127.0.0.1;

    location /static  {
        autoindex on;
        root /static;
    }
}

The above is an extract from nginx.conf. Now no matter what I set root to, nginx throws a 404 not found error when trying to access http://127.0.0.1/static/.

The file structure is as follows:

/home/user/site/project
/home/user/site/static
/home/user/site/templates

Django settings.py has the following set up as STATIC_ROOT and STATIC_URL

STATIC_ROOT = "/home/user/site/static/"
STATIC_URL  = "http://127.0.0.1/static/"

If anyone could point us in the right direction of where to do with this it would be fantastic.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

永言不败 2024-11-30 15:26:18
root /static

这不应该是 /home/user/site/static 吗?

root /static

Shouldn't this be /home/user/site/static instead?

胡大本事 2024-11-30 15:26:18

根据上面吉姆的回答找到了解决方案。

root /static 

现在已更改为:

root /home/user/site/static

但是,检查日志后,nginx 似乎试图在 http://127.0.0.1 查找路径/static 是 /home/user/site/static/static/ ,这显然是不正确的。

我不确定这是否是正确的方法,但是我现在将所有静态文件指向根目录(消除了 url 中对 /static 的需要。所以对于 /static/images 我现在只需将其指向 http://127.0.0.1/images/

Resolution found, based on Jim's answer above.

root /static 

Has now been changed to:

root /home/user/site/static

However then checking the logs it seems that the path nginx was trying to locate at http://127.0.0.1/static was /home/user/site/static/static/ which is evidently incorrect.

I'm unsure if this is the correct method however i'm now pointing all static files into root (removing the need for /static in the url. so for /static/images I now just point it to http://127.0.0.1/images/.

故人的歌 2024-11-30 15:26:18

检查使用 'root' 或 '别名'。
基本上,在使用“root”的情况下,“location”旁边的路径(/static/)会附加到路径中,而在“alias”的情况下,它将被忽略。

Check the difference between using 'root' or 'alias'.
Basically in case of using 'root' the path next to 'location' (/static/) gets appended in path, while in case of 'alias' it'll be ignored.

遗弃M 2024-11-30 15:26:18

查看root指令的文档,与Apache的别名指令相比,位置匹配不会被删除。因此,当您指定位置 static 时,您定义为根的文件夹必须包含 static 文件夹。因此,nginx 寻找 /home/user/site/static/static 并没有错,它是有意的行为。

我什至更喜欢使用 http://host/static/images 作为提供静态图像的 URL。这样它就遵循 Django 文档 管理静态文件 建议使用静态文件的前缀。否则,您的项目和应用程序文件夹将有多个用于不同静态内容的文件夹。

Check out the documentation of the root directive, in contrast to Apache's alias directive, the location match is not dropped. So when you specify a location static the folder you define as your root must contain the static folder. So it's not wrong that nginx is looking for /home/user/site/static/static it is intentional behavior.

I would even prefer using http://host/static/images as your URL for serving static images. This way it follows the Django documentation Managing static files which suggests to use a prefix for you static files. Otherwise you have multiple folders for different static content cluttering your project and app folders.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文