Django:在 IIS 服务器上提供静态文件的配置

问题描述 投票:0回答:1

我在 AWS IIS 服务器上部署了一个 Django 应用程序。静态文件可以通过“runserver”很好地提供,但不能通过 IIS 服务器提供。我已经做了所有正确的事情,但它不起作用。我什至尝试添加静态文件夹虚拟目录,但它不起作用。 这是我的网络配置:

<?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" />
      
      <!-- Add handler for StaticFiles -->
      <add name="StaticFile" path="*" verb="*" modules="StaticFileModule" resourceType="Unspecified" requireAccess="Read" />
      
      <!-- Add your custom NutMIS handler -->
      <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>

这是我的设置:

STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / "static"

我的 IIS 中安装了静态文件功能。我的“静态”文件夹放置在应用程序文件夹中,它通过 runserver 工作得很好,但不能通过 IIS 上的公共 IP 工作。请我在这方面需要帮助。

我还尝试将此 web.config 添加到我的静态文件夹中,但它不起作用:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <!-- 
      This removes Helicon Zoo handler and makes IIS processing static files.
      -->
      <remove name="django.project#x64" />
      <remove name="django.project#x86" />
    </handlers>
  </system.webServer>
</configuration>

django amazon-web-services file iis static
1个回答
0
投票

如果您恢复与 FastCGI 相关的所有更改并使用 HttpPlatformHandler 干净地启动,您将看到体验是多么流畅,

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" requireAccess="Script" />
        </handlers>
        <httpPlatform stdoutLogEnabled="true" stdoutLogFile=".\python.log" startupTimeLimit="20" processPath="C:\Users\<user name>\AppData\Local\Programs\Python\Python310\python.exe" arguments=".\mysite\manage.py runserver %HTTP_PLATFORM_PORT%">
        </httpPlatform>
    </system.webServer>
</configuration>

Python/Django 仍然负责处理包括静态文件在内的所有事务,而 IIS/HttpPlatformHandler 只是传递请求。

更多详细信息可以从我的博客文章找到。

© www.soinside.com 2019 - 2024. All rights reserved.