Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django rest framework https for absolute urls?

The server hosting the api is returning http for absolute urls even though the page was loaded using https, does this have something to do with django rest framework? because there doesn't seem any obvious way to remedy this.

It's the url field in the Meta class that is relevant

class NewsSerializer(serializers.HyperlinkedModelSerializer):
    user = UserSerializer(read_only=True)
    source = serializers.CharField(source='get_source_url', read_only=True)
    comments_count = serializers.IntegerField(read_only=True)
    date_added = serializers.CharField(source='humanize_date_added',
                                       read_only=True)
    is_owner = serializers.SerializerMethodField()
    user_voted = serializers.SerializerMethodField()
    favorited = serializers.SerializerMethodField()
    image = serializers.SerializerMethodField()    

    def create(self, validated_data):
        user = self.context['request'].user
        story = News(user=user, **validated_data)
        story.save()
        return story    

    def get_is_owner(self, obj):
        user = self.context['request'].user
        if user.is_active and user == obj.user:
            return True
        return False    

    def get_user_voted(self, obj):
        user = self.context['request'].user
        if user.is_active:
            return obj.user_voted(user)
        return None    

    def get_favorited(self, obj):
        user = self.context['request'].user
        if user.is_active:
            return obj.is_favorite(user)    


    class Meta:
        model = News
        fields = ('id', 'link', 'title', 'text', 'source', 'user',
                  'date_added', 'image', 'comments_count', 'url',
                  'upvotes', 'downvotes', 'user_voted', 'type',
                  'is_owner', 'favorited')
        read_only_fields = ('date_added')

i am not sure if it has to do with nginx but i have this in the config

proxy_set_header   Host             $host;
proxy_set_header   X-Real-IP        $remote_addr;
proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
like image 876
jibreel Avatar asked May 08 '16 08:05

jibreel


Video Answer


1 Answers

You need to make sure nginx forwards the client's request scheme because it'll make a regular http request to Django. You'll need to add the following line to your vhost definition:

proxy_set_header X-Forwarded-Proto $scheme;
like image 180
Linovia Avatar answered Nov 12 '22 10:11

Linovia