在 Apache 中使用 mod_wsgi-express 和 ProxyPass 服务静态文件不工作

Serving static files using mod_wsgi-express with ProxyPass in Apache is not working

我今天发现了 mod_wsgi-express,我认为这是一个非常酷的项目。 我不敢相信部署 Python/Django 个 Web 应用程序只需要这个命令。

mod_wsgi-express start-server project/wsgi.py

但是,文档似乎仍然缺失(或者我没有找到?)。据我所知,官方文档中没有提到here

我在同一台机器上托管几个小网站,因此使用 VirtualHost。所以我需要像这样将它代理到另一个端口:

<VirtualHost *:80>
    ProxyPreserveHost On
    ServerName example.com
    ProxyPass "/" "http://localhost:8000"
</VirtualHost>

到目前为止一切顺利。我是运行mod_wsgi-express如下:

mod_wsgi-express start-server --url-alias /static /path/to/static project/wsgi.py 

你看,我正在尝试在 mod_wsgi-express 本身中设置静态文件配置。但是,不提供静态文件并给出 502 代理错误。

所以代理动态请求是有效的,但不是静态文件。在端口 8000 上直接访问时,站点也是工作文件。我在这里缺少什么?

当然,我可以在 VirtualHost 本身中添加配置来为这些静态文件提供服务,但如果它与 mod_wsgi-express 一起使用,它将使配置更简单,更易于部署。

感谢 Graham Dumpleton 的上述评论。尽管官方文档中没有,他已经写了关于这个问题的详细博客 post:Redirection problems when proxying to Apache running in Docker..

虽然我没有使用 Docker,但是当从 Apache VirtualHost 将请求代理到不同于 80 或 443 的端口时,此解决方案是通用的。

在我上面的例子中,我在 VirtualHost 配置中缺少两个 headers。所以这是新的 VirtualHost 配置:

<VirtualHost *:80>
    ServerName example.com
    ProxyPass / http://localhost:8000/
    ProxyPassReverse / http://localhost:8000/
    RequestHeader set X-Forwarded-Port 80
</VirtualHost>

这就是所有需要的。 Graham 在他的博客 post 中提供了很多细节,如果您遇到类似问题,值得一读。