【问题标题】:Django Rest Framework Reverse Relationship Breaks When Behind ProxyDjango Rest Framework反向关系在代理后面中断
【发布时间】:2015-12-14 09:58:33
【问题描述】:

我正在尝试提供一个 django webapp,它使用 gunicorn 和 httpd 作为代理,它也提供静态内容。一切正常,除了 django rest 框架可搜索 API 根(应用程序 API 的入口点)没有提供正确的 url。 url 是 localhost 和 gunicorn 服务的端口,而不是机器的 ipaddress 或其主机名。这是应用程序的 httpd conf 文件:

<VirtualHost *:80>
    DocumentRoot /opt/example
    ProxyPass /static/ !
    Alias /static/ /var/www/html/static/
    ProxyPass / http://localhost:8000/
    ProxyPassReverse / http://localhost:8000/
 </VirtualHost>

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)
    })

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

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

{
    "activity": "http://localhost:8000/activity/",
    "tests": "http://localhost:8000/test/",
}

【问题讨论】:

    标签: django apache proxy django-rest-framework gunicorn


    【解决方案1】:

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

    确保您的 settings.py 中有 USE_X_FORWARDED_HOST = True,因为它默认为 False

    更多信息here

    【讨论】:

    • 有史以来最短的答案。但有用且令人惊讶地难以通过搜索找到。希望这会有所帮助。
    【解决方案2】:

    在您的 httpd.conf 中有一个错字httpd

    该行应该是:

    ProxyPassReverse / http://localhost:8000/
    

    【讨论】:

    • 很遗憾,这只是复制时的拼写错误。实际文件中没有那个错字。我继续编辑我的问题,感谢您的关注。
    猜你喜欢
    • 2012-12-25
    • 2018-08-04
    • 1970-01-01
    • 2013-10-20
    • 2016-05-19
    • 2014-03-21
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多