【发布时间】:2013-08-21 23:11:43
【问题描述】:
我是新手。刚刚在windows中设置了apache服务器。但是现在我需要 nginx 来提供我的静态文件,我想我几乎到处搜索了如何配置 nginx 来提供静态文件,得到了很多答案,但很难理解。有人可以解释一下我从哪里开始以及如何在 Windows 中以菜鸟的角度配置 nginx 和 django。任何帮助将不胜感激。谢谢!
【问题讨论】:
我是新手。刚刚在windows中设置了apache服务器。但是现在我需要 nginx 来提供我的静态文件,我想我几乎到处搜索了如何配置 nginx 来提供静态文件,得到了很多答案,但很难理解。有人可以解释一下我从哪里开始以及如何在 Windows 中以菜鸟的角度配置 nginx 和 django。任何帮助将不胜感激。谢谢!
【问题讨论】:
在您的 server 部分尝试类似的操作:
location /static/ {
root /the_directory_with_your_files/;
}
【讨论】:
server 部分。
编辑位于解压后的 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 上测试
【讨论】: