簡體   English   中英

如何將任何 api 請求從 http 重定向到 Z2B9AFB89A6ACC1575B159BFCA38D1 中的 https?

[英]how to redirect any api request from http to https in django?

將我的 Django web 應用程序從 HTTP 遷移到 Z5E056C500A1C4B6A7110BADE50D 后

例如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 配置文件中,我看到您沒有在默認的 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM