简体   繁体   中英

Django urls with https behind nginx proxy

I have django rest framework behind nginx proxy and some frontend on vue js. The problem is that I have urls with "http" but I need it to be "https":

"images": [
            {
                "id": 2,
                "image": "http://localhost:8000/media/documents/2019/02/26/d59b9c8d-bb36-4461-97ad-7455f19637b8/FVbJkfww_Sk.jpg"
            },
            {
                "id": 1,
                "image": "http://localhost:8000/media/documents/2019/02/26/902e5729-f7fd-480b-bf39-bca65a83038e/%D0%B4%D0%B6%D0%B5%D0%BA%D0%B8-%D1%87%D0%B0%D0%BD-%D0%BC%D0%B5%D0%BC-%D1%88%D0%B0%D0%B1%D0%BB%D0%BE%D0%BD.jpg"
            }
]

nginx is configured like that:

server {
     listen 8443 ssl;
     server_name  backend.mysite.net;
     client_max_body_size 3200m;
     proxy_connect_timeout 3000;
     proxy_send_timeout    3000;
     proxy_read_timeout    3000;
     send_timeout          3000;

    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header REMOTE_ADDR $remote_addr;
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass http://localhost:8000;
      }

My django config os below:

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SECURE_SSL_REDIRECT = True
USE_X_FORWARDED_HOST = True

You are missing in your location 在您的location

Update to

location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header REMOTE_ADDR $remote_addr;
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto https
      proxy_pass http://localhost:8000;
  }

If that doesn't work try

location / {
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto https
      proxy_redirect off;
      proxy_pass http://localhost:8000;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM