You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署在AWS IIS服务器的Django应用静态文件无法加载求助

解决方案:IIS部署Django静态文件无法加载问题

核心原因

当前主web.config中,所有请求(path="*")先被FastCGI handler接管,导致/static/路径的请求没有被IIS静态文件模块处理,而是传递给了Django的WSGI应用,自然无法返回正确的静态资源。

具体修复步骤

1. 确认已执行collectstatic

确保所有Django应用的静态文件都已收集到STATIC_ROOT指定的目录:

python manage.py collectstatic

执行时确认文件都被复制到了C:\myvenv2\NutMIS\static目录下。

2. 修改主web.config的Handler配置

调整Handler顺序,让IIS优先处理/static/*路径的请求,修改后的配置如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
   <appSettings>
    <!-- Required settings -->
    <add key="WSGI_HANDLER" value="django.core.wsgi.get_wsgi_application()" />
    <!-- Your django path -->
    <add key="PYTHONPATH" value="C:\myvenv2\NutMIS" /> 
    <!-- Your djangoname.settings -->
    <add key="DJANGO_SETTINGS_MODULE" value="NutMIS.settings" />
  </appSettings>
  <system.webServer>
<handlers>
      <!-- Remove duplicate entries -->
      <remove name="NutMIS" />
      <remove name="StaticFiles" />
      <remove name="StaticFile" />
      
      <!-- 优先处理/static/路径的静态文件请求 -->
      <add name="StaticFileStatic" path="static/*" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
      
      <!-- 处理其他所有请求,交给Django的WSGI应用 -->
      <add name="NutMIS" path="*" verb="*" modules="FastCgiModule" scriptProcessor="C:\Users\Administrator\AppData\Local\Programs\Python\Python311\python.exe|C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\wfastcgi.py" resourceType="Unspecified" requireAccess="Script" />
    </handlers>
<staticContent>
            <mimeMap fileExtension=".*" mimeType="application/octet-stream" />
        </staticContent>
  </system.webServer>
</configuration>
  • 新增了path="static/*"的StaticFile handler,放在FastCGI handler之前,确保静态文件请求被IIS直接处理
  • 把resourceType改为File,更精准匹配实际文件资源

3. 调整static目录的专属web.config(可选但推荐)

替换原static目录下的web.config为以下内容,确保该目录下的请求完全由静态文件模块处理,不继承主配置的FastCGI handler:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <!-- 清除所有继承的handler,只保留静态文件处理 -->
      <clear />
      <add name="StaticFile" path="*" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
    </handlers>
  </system.webServer>
</configuration>

4. 检查权限设置

确保IIS应用池的运行用户对C:\myvenv2\NutMIS\static目录有读取权限:

  • 右键static目录 → 属性 → 安全 → 编辑 → 添加IIS应用池用户(通常是IIS AppPool\你的应用池名称)
  • 赋予该用户“读取”和“列出文件夹内容”权限

5. 验证

  • 重启IIS服务(iisreset命令或IIS管理器中重启站点)
  • 清除浏览器缓存后,访问公网IP的静态文件路径(比如http://你的IP/static/css/style.css),确认能正常加载

内容的提问来源于stack exchange,提问作者Henry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 01:40:02