P
P
Pavel Dyukarev2019-02-22 10:29:11
Django
Pavel Dyukarev, 2019-02-22 10:29:11

How to make the function return the absolute path to the file with https and not http?

Guys, need some bright ideas!)
It doesn't work, because the mechanism |generate an absolute link to a file| returns an absolute path with http base and it needs to be https.
In order.
There is a template:

<audio controls onended="startNextTrack()">
    <source id="audio_sourse" src="{% url 'stream_mp3' song.name %}">
</audio>

This django link is caught by the following controller:
path('stream_mp3/<path:song>', views.stream_mp3, name='stream_mp3')

The controller sends a request to the view:
# внимание! django dev сервер не поддерживает частичный ответ 206 поэтому работает только в nginx
def stream_mp3(request, song):
    song_name = song
    mp3_path = os.getcwd() + '/media/charge/music/' + song_name
    response = HttpResponse('', content_type="audio/mpeg", status=206)
    response['X-Accel-Redirect'] = '/media/charge/music/' + song_name
    response['X-Accel-Buffering'] = 'no'
    response['Content-Length'] = os.path.getsize(mp3_path)
    response['Content-Dispostion'] = "attachment; filename=" + mp3_path
    response['Accept-Ranges'] = 'bytes'
    return response

The view, in turn, returns an absolute reference, a partial response that NGINX serves:
(or am I confusing something?!)
http://batareika.app/media/charge/music/bi2mojjroknrollmegapesnime.mp3

Well, here, accordingly, problems appear: a site running on https does not want to accept such a response - it swears
. Tell me, how to solve this problem?
PS Please note that this logic works fine on a local NGINX server and refuses to work when switching to SSL/HTTPS

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Pavel Dyukarev, 2019-02-22
@PavelDuk

Guys thanks for the help! Everything worked. But I had to do the right thing) that is, not like I did initially. In general, Ruslan
did everything . For this he is special, thank you very much! 1) Removed qunicorn support from NGINX. 2) Removed all demons from the work logic. 3) Installed UWSGI. 4) Added file: batareika.ini

[uwsgi]
chdir = /root/Batareika
module = Batareika.wsgi
home = /root/Batareika/venv
master = true
processes=6
chmod-socket = 666
socket = /root/Batareika/batareika.sock

upstream batareika {
    server unix:////root/Batareika/batareika.sock;
}

server {
        listen 80;
        server_name batareika.app;
        return 301 https://$server_name$request_uri;
}

server {
        listen 443 ssl;
        server_name batareika.app;
         ssl_certificate /etc/letsencrypt/live/batareika.app/fullchain.pem;
         ssl_certificate_key /etc/letsencrypt/live/batareika.app/privkey.pem;
         include /etc/letsencrypt/options-ssl-nginx.conf;
         ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
        location = /favicon.ico { access_log off; log_not_found off; }


        location /static/ {
                root /root/Batareika;
        }

        location /media/ {
                root /root/Batareika;
        }


        location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        uwsgi_pass batareika;
        include /etc/nginx/uwsgi_params;
}
}

lang="python">
# для NGINX перенаправление траффика http-https
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True

7) He said that using an incomprehensible streaming function (def stream_mp3 (request, song)) - just what a game - you need to give a regular link with an absolute address to the template. Accordingly, these views, as well as controllers leading to them, are not needed. Now I am giving the absolute path. Thanks for the help - alternativshik'u . In general, he, too, was perplexed by this view. In defense of myself, I’ll say that stackoverlflow advised me to use this function. Thank him for this)
host_mp3 = 'https://batareika.app/media/charge/music/'
link = host_mp3 + song_name
# и link летит в шаблон

In general, that's all.

X
Xaip, 2019-02-22
@Xaip

You are missing settings for https:
settings.py

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True

A
alternativshik, 2019-02-22
@alternativshik

os.getcwd() - why not write a domain with a schema here?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question