Checklist
Steps to reproduce
- 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)),
]
- Dockerize application and run application
localhost:8000 gives me following output.
{
"users": "http://localhost:8000/users/"
}
- 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;
}
}
- 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.
Checklist
masterbranch of Django REST framework.Steps to reproduce
localhost:8000gives me following output.localhost:81application is working file but the problam with the response;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_HOSTfrom Django document. What explained there tried to search in DRF code but didn't find such kind of logic which will resolve this issue.