【问题标题】:how to redirect any api request from http to https in django?如何在django中将任何api请求从http重定向到https?
【发布时间】:2020-01-30 13:25:26
【问题描述】:

将我的 Django Web 应用程序从 HTTP 迁移到 https 后,当我键入时

例如r= requests.get('http://xxxx.com')

它给了我这个错误:

requests.exceptions.SSLError: HTTPSConnectionPool(host=my_host_name,port:443)  Max retries exceeded with url:http://xxxx.com (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)'),))

但我为重定向进行了 nginx 配置,例如,当我在浏览器上放置任何 HTTP 地址时,它会将我重定向到正确的 https 地址。

我想对 API 请求做同样的事情。

我不想更改后端代码上的请求地址,我只想将 HTTP 请求重定向到 https(如果可能)?

我的 nginx 配置:

http {

  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 65;
  types_hash_max_size 2048;
  proxy_cache_path /path/cache keys_zone=cache:10m levels=1:2 inactive=600s 
  max_size=100m;
  default_type application/octet-stream;


    log_format compression '$remote_addr - $remote_user [$time_local] '
                           '"$request" $status $body_bytes_sent '
                           '"$http_referer" "$http_user_agent" "$gzip_ratio"';

  access_log /path/access.log;
  error_log /Path/error.log error;
  gzip on;
  gzip_disable "msie6";
  text/xml application/xml application/xml+rss text/javascript;

  upstream app_servers {

    server 127.0.0.1:8080;
  }

  server {

    listen 443  ssl http2;
    listen [::]:443 ssl http2;
    ssl on;
    ssl_certificate /PATH/certificate.crt;
    ssl_certificate_key /PATH/certificate.key;
    proxy_cache cache;
    proxy_cache_valid 200 1s;
    #ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
    server_name  my_host_name;
    access_log /path/nginx-access.log compression;
    location /static/  {
       alias /path/static/;

 }

location /nginx_status {
        stub_status on;
        allow all;
        deny all;
}

    location / {
      proxy_pass         http://127.0.0.1:8000;
      proxy_set_header   Host $host;
      proxy_pass_request_headers on;
      proxy_read_timeout 1200;
      proxy_set_header   X-Real-IP $remote_addr;
      proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header   X-Forwarded-Host $server_name;
    }
 }
 server {
   listen 9999 ;
   server_name my_host_name ;
   return 307 https://my_domain.com$request_uri;
}
}

【问题讨论】:

  • 我认为您应该先包含 Nginx 配置,然后我们才能以有意义的方式帮助您。
  • 这是我的 Nginx 配置 @NicoGriffioen

标签: django api ssl nginx https


【解决方案1】:

这种错误让我很困惑,但是在你的 Nginx 配置文件中,我看到你没有在默认的 HTTP 端口上监听。您应该添加一个侦听 HTTP (80) 端口的服务器块,并从那里重定向到 https (443)。

在你的 http: 中添加这个块

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name  my_host_name;
    return 301 https://$host$request_uri;
}

【讨论】:

  • 感谢@NicoGriffioen,但遗憾的是我尝试添加此代码块,但它不起作用。同样在我的情况下,我的默认端口是 9999,我尝试将 80 更改为 9999 也不起作用。 ://
  • 默认端口是 9999 是什么意思?
  • 我在迁移到 SSL 之前的端口
  • xxxx.com 将默认为端口 80,因此除非您在请求 url 中指定端口 9999,否则它无论如何都不会工作。我不确定为什么在您更改 Nginx 配置后它不起作用。
  • 我不明白为什么在浏览器上它可以工作,而使用 rest-API 却不行!它给了我这个错误 requests.exceptions.SSLError: HTTPSConnectionPool(host=my_host_name,port:443) Max retries exceeded with url:xxxx.com (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败 (_ssl .c:749)'),))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-17
  • 1970-01-01
  • 2011-05-04
  • 2016-11-17
  • 1970-01-01
相关资源
最近更新 更多