如何使用 django rest 框架强制 URL?

How to force URL with django rest framework?

我使用一个 apache 容器作为负载均衡器并将流重定向到 2 个 apache wsgi。 我在 2 个 apaches 上使用 django-rest-framework。

我无法获取文件。事实上, api return 是我:

{
    avatar: http://127.0.0.1:8000/media/avatars/2.jpg
}

而不是 http://127.0.0.1:8000/,我想要 api 到 return https://api.myservice/

如何强制url?

看起来与 this question 相似。那里的答案提到:

Django's reverse uses request's domain to build absolute URL.

因此请确保在您的代理中传递主机 header。根据对链接答案的评论,ProxyPreserveHost 应该为 Apache 完成这项工作。