在nginx代理后面带有https的Django URL

时间:2019-03-23 09:06:38

标签: django ssl nginx django-rest-framework

我在nginx代理后面有django rest框架,在vue js上有一些前端。 问题是我的网址带有“ http”,但我需要将其设为“ 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的配置如下:

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;
      }

我的django配置如下:

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

1 个答案:

答案 0 :(得分:0)

您在proxy_set_header X-Forwarded-Proto https中缺少location

更新到

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;
  }

如果这样不起作用,请尝试

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;
  }