Django Rest框架反向关系在代理时落后

时间:2015-09-17 13:44:49

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

我正在尝试提供一个django webapp,它使用gunicorn,httpd充当代理,也提供静态内容。一切正常,除了django rest框架可搜索API根(应用程序API的入口点)不提供正确的URL。 url用于本地主机和由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在8000端口的localhost上提供服务。

这里是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/",
}

2 个答案:

答案 0 :(得分:4)

我有类似的设置,似乎工作正常。

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

更多信息here

答案 1 :(得分:0)

在你的httpd.conf中有一个拼写错误httpd

该行应为:

ProxyPassReverse / http://localhost:8000/