Django Rest Framework 反向关系在代理后面中断

Ste*_*ell 5 apache django proxy gunicorn django-rest-framework

我正在尝试提供一个 django web 应用程序,它使用 Gunicorn 和 httpd 作为代理,也提供静态内容。一切工作正常,除了 django Rest Framework 可搜索 API 根(应用程序 API 的入口点)未提供正确的 url。url 是本地主机和gunicorn 服务的端口,而不是机器的IP 地址或其主机名。这是该应用程序的 httpd conf 文件:

<VirtualHost *:80>
    DocumentRoot /opt/example
    ProxyPass /static/ !
    Alias /static/ /var/www/html/static/
    ProxyPass / http://localhost:8000/
    ProxyPassReverse / http://localhost:8000/
 </VirtualHost>
Run Code Online (Sandbox Code Playgroud)

Gunicorn 在 localhost 的端口 8000 上提供服务。

这是 api 的视图:

@api_view(('GET',))
def api_root(request, format=None):
    return Response({
        'activity': reverse('activity-list', request=request, format=format)
        'test' : reverse('test-list', request=request, format=format)
    })
Run Code Online (Sandbox Code Playgroud)

当点击 api 根页面时,这是我得到的响应:

HTTP 200 OK
Content-Type: application/json
Vary: Accept
Allow: GET, HEAD, OPTIONS

{
    "activity": "http://localhost:8000/activity/",
    "tests": "http://localhost:8000/test/",
}
Run Code Online (Sandbox Code Playgroud)

小智 5

我有一个类似的设置,它似乎工作得很好。

确保您USE_X_FORWARDED_HOST = True的 settings.py 中有默认值False

更多信息请点击此处