【问题标题】:how to serve static files through nginx and django in windows如何在 Windows 中通过 nginx 和 django 提供静态文件
【发布时间】:2013-08-21 23:11:43
【问题描述】:

我是新手。刚刚在windows中设置了apache服务器。但是现在我需要 nginx 来提供我的静态文件,我想我几乎到处搜索了如何配置 nginx 来提供静态文件,得到了很多答案,但很难理解。有人可以解释一下我从哪里开始以及如何在 Windows 中以菜鸟的角度配置 nginx 和 django。任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: django nginx


    【解决方案1】:

    在您的 server 部分尝试类似的操作:

    location /static/ {
        root /the_directory_with_your_files/;
    }
    

    【讨论】:

    • 对不起。我不熟悉server 部分。
    • 好的,那么您如何开始发布当前 nginx 配置的相关部分以用于其余设置? :)
    • 再次抱歉。这就是我卡住的地方。我不知道在哪里或如何配置 nginx。请您从一开始就向我解释一下。
    【解决方案2】:

    编辑位于解压后的 nginx for windows 的 conf 文件夹中的nginx.conf 文件并添加以下指令(您必须更新路径以匹配您的 django 项目的文件夹)

    location /static/ {
            alias 'C:/Users/user1/your_django_project/staticfiles/';
        }
    

    如果您在 localhost 的 8000 端口上运行 django 网络服务器(如 hypercorn 或其他),则最终的 nginx.conf 示例可能如下所示

    http{
        server {
        server_name localhost;
    
        location / {
            proxy_pass http://backend;
            proxy_set_header Host 127.0.0.1;
            proxy_set_header X-Real-IP 127.0.0.1;
            proxy_set_header X-Forwarded-For 127.0.0.1;
          proxy_set_header X-Forwarded-Proto http;
        }
    
        location /static/ {
            alias 'C:/Users/user1/your_django_project/staticfiles/';
        }
      }
      upstream backend {
          server 127.0.0.1:8000;
          # There could be more than a backend here
      }
    }
    
    events {
      worker_connections  1024;
    }
    

    在 nginx 1.20 上测试

    【讨论】:

      猜你喜欢
      • 2011-01-27
      • 2015-03-18
      • 2020-06-17
      • 1970-01-01
      • 2017-01-06
      • 2013-05-08
      • 2011-10-20
      • 2021-12-02
      • 2020-09-14
      相关资源
      最近更新 更多