【问题标题】:how to config Kong force HTTP to redirect to HTTPS如何配置 Kong 强制 HTTP 重定向到 HTTPS
【发布时间】:2018-11-07 22:43:03
【问题描述】:

作为kong doc https://getkong.org/docs/0.13.x/admin-api/#add-certificate ,如果我们希望我们的网站支持https,我们应该这样做:

curl -i -X POST \
  --url http://localhost:8001/certificates \
  --data 'cert =-----BEGIN CERTIFICATE-----...'
  --data 'key =-----BEGIN RSA PRIVATE KEY-----..'
  --data 'snis =example.com'

现在,该网站支持https,但它也支持http。 我们如何强制http 重定向到https

ps:有一个插件(https://getkong.org/plugins/dynamic-ssl)。它有一个选项config.only_https=true。它似乎强制https。但是,dynamic-ssl 插件和/certificates api 有什么区别。

【问题讨论】:

    标签: kong


    【解决方案1】:

    我知道为时已晚,可能您找到了答案,此答案适用于像我这样面临同样问题的其他人:

    当在 Kong 中创建 API 时,如 here 所说,您可以强制协议为 HTTPS,但似乎没有办法说明 redirect HTTP 流量到 HTTPS,这是唯一的方法我发现是更改Kong的nginx模板并将所有请求强制重定向到https,但这不是我想要的...... 顺便说一句,您可以通过创建一个与this link 中讨论的非常相似的模板并添加以下配置来做到这一点:

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

    那么您应该使用 kong start --nginx-conf custom_nginx.template 运行您的 kong 实例

    然后,Kong 将在 nginx 中使用您的服务器阻止规则。

    【讨论】:

      【解决方案2】:

      转到要重定向到 https 的服务,在插件上选择 -> 添加插件 -> 无服务器 -> Pre Function 并将下面的值粘贴到函数中。

      local scheme = kong.request.get_scheme()
      if scheme == "http" then
        local host = kong.request.get_host()
        local query = kong.request.get_path_with_query()
        local url = "https://" .. host ..query
        kong.response.set_header("Location",url)
        return kong.response.exit(302,url)
      end
      

      【讨论】:

        【解决方案3】:

        M-N 相同。我也想给出我的解决方案:

        您可以使用这个 Kong 插件:https://github.com/dsteinkopf/kong-http-to-https-redirect。 这是一个 fork,因为原始项目与 Kong 1.x 不兼容。

        1) 我将其克隆到/usr/local/share/lua/5.1/kong/plugins/http-to-https-redirect

        2) 我在 Kong YAML 配置中添加了这个插件:

        plugins:
          # ... my plugins
          name: http-to-https-redirect
        

        希望对你有帮助。

        【讨论】:

        • 插件“http-to-https-redirect”未启用
        【解决方案4】:

        有点晚了,可以使用前置功能插件重定向到https

        local scheme = kong.request.get_scheme()
        if scheme == "http" then
        local host = kong.request.get_host()
        local query = kong.request.get_path_with_query()
        local url = "https://" .. host ..query
        kong.response.set_header("Location",url)
        return kong.response.exit(302,url)
        end
        

        https://github.com/Kong/kong/issues/1946#issuecomment-485830359

        【讨论】:

          【解决方案5】:

          您不需要覆盖完整的 NGINX 模板,也不需要使用任何插件。您可以编写 include 指令并将 sn-p 包含在 Kong 配置文件的 HTTP 块中。

          首先,编写 HTTPS 重定向规则。我把这个文件放在/etc/kong/nginx-https.conf:

          server {
              listen      80;
              server_name api.mysite.com;
              return 301 https://$server_name$request_uri;
          }
          

          然后在 /etc/kong/kong.conf 的 Kong 配置文件中,我包含了代码 sn-p:

          ........
          ........
          nginx_http_include = /etc/kong/nginx-https.conf
          

          然后我重新启动了 Kong 服务。更多相关信息可以在官网here找到。

          【讨论】:

            猜你喜欢
            • 2016-11-14
            • 2020-05-28
            • 1970-01-01
            • 1970-01-01
            • 2020-07-23
            • 2011-11-03
            • 1970-01-01
            • 2015-05-17
            • 1970-01-01
            相关资源
            最近更新 更多