Skip to content

[USE_X_FORWARDED_HOST] drf application with proxy pass(nginx) #7347

@Devansh3790

Description

@Devansh3790

Checklist

  • I have verified that that issue exists against the master branch of Django REST framework.
  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • This is not a usage question. (Those should be directed to the discussion group instead.)
  • This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • I have reduced the issue to the simplest possible case.
  • I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

Steps to reproduce

  1. Create a Django rest application following the given steps on the doc.
# app/urls.py

from django.urls import path, include
from django.contrib.auth.models import User
from rest_framework import routers, serializers, viewsets


# Serializers define the API representation.
class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ['url', 'username', 'email', 'is_staff']


# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer


# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'users', UserViewSet)

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
    path('', include(router.urls)),
]
  1. Dockerize application and run application localhost:8000 gives me following output.
{
    "users": "http://localhost:8000/users/"
}
  1. Create an Nginx container and add a proxy for my application with 81 port
upstream app_server {
  server backend:8000; # backend is the docker-compose service name for my application
}
server {

  listen 81;
  server_name app_server;

  location / {
    # everything is passed to Gunicorn
    proxy_pass http://app_server;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_redirect off;
  }
}
  1. opened localhost:81 application is working file but the problam with the response;
{
    "users": "http://localhost/users/"
}

Actual behavior

Here you can notice that the value for key users : "http://localhost/users/".

Expected behavior

It should be "http://localhost:81/users/".

I did a search on the problem and found the USE_X_FORWARDED_HOST from Django document. What explained there tried to search in DRF code but didn't find such kind of logic which will resolve this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions