簡體   English   中英

如何在 drf_yasg 中將 Schemes 從 HTTP 更改為 HTTPS?

[英]How change Schemes from HTTP to HTTPS in drf_yasg?

我將drf_yasg用於 swagger 文檔。 When I publish my DRF app behind AWS Application Load Balancer and set listener to listen on 443 HTTPS and redirect to my EC2 on which DRF is running, swagger UI is trying to send a request to endpoint http://example.com/status rather比例如https://example.com/status 這會產生一個谷歌瀏覽器錯誤:

swagger-ui-bundle.js:71 Mixed Content: The page at 'https://example.com/swagger#/status/status_list' was loaded over HTTPS, but requested an insecure resource 'http://example.com/status'. This request has been blocked; the content must be served over HTTPS.

所以我解決這個問題的解決方案是在 drf_yasg.views.get_schema_view 中明確設置我的服務器drf_yasg.views.get_schema_view 所以我的代碼看起來像:

schema_view = get_schema_view(
    openapi.Info(
        title="Server Api Documentation",
        default_version="v1",
        description="",
    url="http://example.com/status"
)

# noinspection PyUnresolvedReferences
swagger_patterns = [
    path("", schema_view.with_ui("swagger", cache_timeout=0), name="schema-swagger-ui"),

我希望能夠不顯式設置 URL 字符串,而是選擇 HTTP 或 HTTPS 之間的方案。 drf_yasg有可能嗎?

在您的 Django settings.py 中添加這些

# Setup support for proxy headers
USE_X_FORWARDED_HOST = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

如果您使用的是 nginx,請確保設置了正確的 header ( X-Forwarded-Proto )。 Actually, check all nginx reverse proxy configs sitting between end-user and web server (gunicorn / uwsgi) like nginx on host machine and eg nginx deployed in docker.

location / {
    proxy_pass http://django:5000;
    proxy_set_header  Host              $http_host;
    proxy_set_header  X-Real-IP         $remote_addr;
    proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;
    # check line below!
    proxy_set_header  X-Forwarded-Proto https;
    proxy_set_header  X-Forwarded-Referrer $http_referer;
    proxy_set_header  Referer $http_referer;
}

暫無
暫無

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

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