gpt4 book ai didi

docker - 对 https 上下文中的所有服务器 block 强制使用 https

转载 作者:行者123 更新时间:2023-12-02 19:06:36 26 4
gpt4 key购买 nike

我是第一次尝试 nginx,我用 docker 来做。
基本上我想实现以下架构

  • https://example.com (商业网站`)
  • https://app.example.com (渐进式网页/单页应用)
  • https://app.example.com/api (为避免预检请求,需要到 https://api.example.com 的代理)
  • https://api.example.com ( Restful api)
  • http请求重定向到 https

  • 我正在生成 /etc/nginx/conf.d/default.conf启动时包含一些环境变量的文件。然后该文件包含在 http 中。 default.conf 文件的上下文,因此对我可以配置的内容带来了一些限制。 ( related issue)

    你可以看到我当前的 nginx.conf file here (文件很大,可以在这里嵌入)。

    你可以看到 docker-compose.yml file here .

    问题:

    400 Bad Request 普通 HTTP 请求被发送到 HTTPS 端口

    我实际上无法调用 http://(app/api).example.com被重定向到它的 https版本,我试过没有成功:(见上面的链接文件)
    server {
    listen 80 ssl;
    listen 443 ssl;
    listen [::]:80 ssl;
    listen [::]:443 ssl;
    server_name api.dev.local;

    if ($http_x_forwarded_proto = "http") {
    return 301 https://$server_name$request_uri;
    }

    # more code...
    }

    任何关于我的实际配置的建议都非常欢迎在评论部分!我刚开始使用 nginx 并因此阅读 tons of artciles它提供了代码片段,我在阅读了它们需要的内容后简单地复制和粘贴。

    最佳答案

    https协议(protocol)是 http 的扩展,因此它们在一定程度上是不同的协议(protocol)。目前您的服务器不期望 http在 :80,它宁愿期待 https由于设置listen 80 ssl .这会导致错误。

    您需要单独处理http :80 上的请求,应重定向到 https在:443,从处理 https on :443,应该正常处理。

    这可以通过拆分另一个 server 来完成。 http 的配置 block 在 :80:

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

    ...并从当前 block 中删除 :80 上的监听:
    server {
    listen 443 ssl;
    listen [::]:443 ssl;

    # more code...
    }

    如果需要,以下博客文章将提供更多详细信息 https://bjornjohansen.no/redirect-to-https-with-nginx

    关于docker - 对 https 上下文中的所有服务器 block 强制使用 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50365862/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com