Is there any way to redirect http to https in kong

Hi ,

I am using kong version 2.0.3 community version with DB less mode. i deployed it in kubernetes. and enabled SSL also.
i am running my application with http port 8000 and https 443. but accessing via https.

i want to know if i access my application with http it should redirect to https.

how to acheive this from kong.

Please help me here?

Thanks & Regards

Hi!

i’m using direclty configuration in the kongIngress. With protocols “https” you are allowing only connectionss with HTTPS and applying a 301 redirection if receive an HTTP.

   route:
  connect_timeout: 20000
  https_redirect_status_code: 301
  preserve_host: true
  protocols:
  - https
  regex_priority: 0
  strip_path: true

Other option is to apply globally a pre-function plugin.

apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
  name: https-redirect
  labels:
   global: "true"
plugin: pre-function
config:
  functions:
  - |
    if kong.request.get_header("X-Forwarded-Proto") == "http" then
      local host = kong.request.get_host()
      	if host ~= "localhost" then
      	local query = kong.request.get_path_with_query()
      	local url = "https://" .. host ..query
      	kong.response.set_header("Location",url)
      	return kong.response.exit(301,url)
      	end
    end

Add annotation

konghq.com/override: my-https-redirect to ingress resource, and also apply a KongIngress

apiVersion: configuration.konghq.com/v1
kind: KongIngress
metadata:
  name: my-https-redirect
route:
  protocols:
    - https
  https_redirect_status_code: 426 # return {"message":"Please use HTTPS protocol"}

Thanks for your reply Raymond