# Redirecting HTTP to HTTPS

**URL:** <https://discuss.konghq.com/t/redirecting-http-to-https/2637>\
**Category:** Questions\
**Tags:** kubernetes\
**Created:** [January 17, 2019, 3:41pm UTC](https://discuss.konghq.com/t/redirecting-http-to-https/2637 "2019-01-17T15:41:19Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexjantunen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/alexjantunen/32/1056_2.png) [@alexjantunen](https://discuss.konghq.com/u/alexjantunen)\
**Post date:** [January 17, 2019, 3:41pm UTC](https://discuss.konghq.com/t/redirecting-http-to-https/2637/1 "2019-01-17T15:41:19Z")

</div>

Hi,

I have a working setup on GKE. Kong-ingress-controller 0.3 and Kong-proxy 1.0.0.

My last step is to setup HTTP so that clients (browser in this case) are always redirected to HTTPS. Both http and https work separately, but I would like to “disable” http by doing the redirect thing.

I have created ingress for my service with the following annotations (I tried my luck with force-ssl-redirect that should work with nginx, but it didn’t work for kong):

```
annotations:
  kubernetes.io/ingress.class:"kong"
  kong.ingress.kubernetes.io/force-ssl-redirect: "true"
  configuration.konghq.com: "kong-configuration"

```

I don’t see anything redirect related options in KongIngress (“kong-configuration” that I am referring from this ingress, and trying to explore things)

What is the proper way to setup http → https redirect. Maybe using plugins? which one?

---

<div class="post-metadata">

**Author:** ![hbagdi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/hbagdi/32/562_2.png) [@hbagdi](https://discuss.konghq.com/u/hbagdi)\
**Post date:** [March 1, 2019, 10:20pm UTC](https://discuss.konghq.com/t/redirecting-http-to-https/2637/2 "2019-03-01T22:20:04Z")

</div>

`kong.ingress.kubernetes.io/force-ssl-redirect` annotation is no supported by Kong Ingress as it’s not a standard annotation.

You can use Request termination plugin in Kong to send back a 302 HTTPS redirect for any plaintext request.

Please mark the post as resolved if this helps you, thanks!

---

<div class="post-metadata">

**Author:** ![Tarek\_Moubarak](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/tarek_moubarak/32/633_2.png) [@Tarek\_Moubarak](https://discuss.konghq.com/u/Tarek_Moubarak)\
**Post date:** [March 22, 2019, 4:13pm UTC](https://discuss.konghq.com/t/redirecting-http-to-https/2637/3 "2019-03-22T16:13:06Z")

</div>

Hi hbagdi,

I’m also having some trouble with this too. I just want to be sure if this is the correct configuration:

```
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
    name: redirect-https
    namespace: kong
config:
     status_code : 302
plugin: request-termination

```

Thank you in advance!  
Tarek M.

---

<div class="post-metadata">

**Author:** ![miles](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@miles](https://discuss.konghq.com/u/miles)\
**Post date:** [April 2, 2019, 3:18pm UTC](https://discuss.konghq.com/t/redirecting-http-to-https/2637/4 "2019-04-02T15:18:43Z")

</div>

@hbagdi Bump on this thread. I would also like to force all HTTP requests to HTTPS for some of my services. Is using the `Request Termination` plugin the recommended approach for this behavior? If so, can you provide a sample configuration for the plugin?

---

<div class="post-metadata">

**Author:** ![hbagdi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/hbagdi/32/562_2.png) [@hbagdi](https://discuss.konghq.com/u/hbagdi)\
**Post date:** [April 5, 2019, 5:41pm UTC](https://discuss.konghq.com/t/redirecting-http-to-https/2637/5 "2019-04-05T17:41:30Z")

</div>

You could apply the following plugin for SSL redirect:

```auto
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
  name: https-redirect
plugin: pre-function
config:
  functions:
  - |
    local forwarded_host = ngx.var.http_x_forwarded_host or ngx.var.host
    ngx.header["Location"] = "https://" .. forwarded_host .. ngx.var.request_uri
    return kong.response.exit(302)

```

Please apply this plugin on an Ingress which accepts all paths and all hosts on the `http` protocol.

If this solves your problem, please mark the post as resolved, thank you!

---

<div class="post-metadata">

**Author:** ![miles](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@miles](https://discuss.konghq.com/u/miles)\
**Post date:** [April 16, 2019, 6:44pm UTC](https://discuss.konghq.com/t/redirecting-http-to-https/2637/6 "2019-04-16T18:44:39Z")

</div>

Thanks. I’m giving this a try now and my endpoint is in a redirect loop. The http request gets 302 redirected to https, but then all https requests still get 302ed as well.

---

<div class="post-metadata">

**Author:** ![hbagdi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/hbagdi/32/562_2.png) [@hbagdi](https://discuss.konghq.com/u/hbagdi)\
**Post date:** [April 17, 2019, 12:36am UTC](https://discuss.konghq.com/t/redirecting-http-to-https/2637/7 "2019-04-17T00:36:26Z")

</div>

> [@miles](#):
>
> Thanks. I’m giving this a try now and my endpoint is in a redirect loop. The http request gets 302 redirected to https, but then all https requests still get 302ed as well.

My bad, the plugin shouldn’t be applied globally, but on a route which accepts only `HTTP` traffic.

Which route have you applied this plugin to?

---

<div class="post-metadata">

**Author:** ![greg](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@greg](https://discuss.konghq.com/u/greg)\
**Post date:** [April 17, 2019, 2:21am UTC](https://discuss.konghq.com/t/redirecting-http-to-https/2637/8 "2019-04-17T02:21:15Z")

</div>

What do you mean by “not a standard annotation?” Having to add a plugin for this functionality means having to supplement anything you are installing via the curated helm charts.

---

<div class="post-metadata">

**Author:** ![miles](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@miles](https://discuss.konghq.com/u/miles)\
**Post date:** [April 17, 2019, 2:11pm UTC](https://discuss.konghq.com/t/redirecting-http-to-https/2637/9 "2019-04-17T14:11:25Z")

</div>

@hbagdi I’m applying the plugin to a specific ingress that has TLS terminated at a load balancer in front of Kong. So I want Kong to redirect http -\> https and then pass through the https requests.

---

<div class="post-metadata">

**Author:** ![hbagdi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/hbagdi/32/562_2.png) [@hbagdi](https://discuss.konghq.com/u/hbagdi)\
**Post date:** [April 17, 2019, 3:41pm UTC](https://discuss.konghq.com/t/redirecting-http-to-https/2637/10 "2019-04-17T15:41:53Z")

</div>

> [@miles](#):
>
> @hbagdi I’m applying the plugin to a specific ingress that has TLS terminated at a load balancer in front of Kong. So I want Kong to redirect http → https and then pass through the https requests.

@miles You want to look at KongIngress Custom Resource.  
You should create an Ingress resource which accepts all hosts and `/` path and proxy it to a dummy upstream service (which will actually never receive any traffic). Then, you annotate the Ingress with a KongIngress resource, which sets `route.protocols` to `http` only. This creates a route in Kong which accepts all `http` traffic, and this is the route to which you apply the above plugin, and then you will not have a redirect loop.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![hbagdi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/hbagdi/32/562_2.png) [@hbagdi](https://discuss.konghq.com/u/hbagdi)\
**Post date:** [April 17, 2019, 3:44pm UTC](https://discuss.konghq.com/t/redirecting-http-to-https/2637/11 "2019-04-17T15:44:56Z")

</div>

> [@greg](#):
>
> What do you mean by “not a standard annotation?” Having to add a plugin for this functionality means having to supplement anything you are installing via the curated helm charts.

Hello @greg,  
`ingress.kubernetes.io/force-ssl-redirect: "true"` is not standardized by k8s community but we are seeing it proliferating now. We don’t currently support it but might consider adding it in future. Please consider opening a [Github issue](http://github.com/kong/kubernetes-ingress-controller/issues/new) for a feature request if you’d like to.

---

<div class="post-metadata">

**Author:** ![hbinduni](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/hbinduni/32/980_2.png) [@hbinduni](https://discuss.konghq.com/u/hbinduni)\
**Post date:** [May 3, 2019, 6:38pm UTC](https://discuss.konghq.com/t/redirecting-http-to-https/2637/12 "2019-05-03T18:38:43Z")

</div>

i try with pre-function as mention [here](https://github.com/Kong/kong/issues/1946#issuecomment-485830359) and it work ok.  
just add pre-function plugin and submit this code:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![hbagdi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/hbagdi/32/562_2.png) [@hbagdi](https://discuss.konghq.com/u/hbagdi)\
**Post date:** [May 3, 2019, 7:20pm UTC](https://discuss.konghq.com/t/redirecting-http-to-https/2637/13 "2019-05-03T19:20:47Z")

</div>

Happy to see @hbinduni’s solution for this.

To share an update and for others who stumble across this, with [https://github.com/Kong/kong/pull/4424](https://github.com/Kong/kong/pull/4424) merged in, Kong 1.2 comes with support for sending HTTPS redirect out of the box in Kong and Kong Ingress Controller will be updated to use that in future, which shall make HTTP to HTTPS redirection much simpler.

---

<div class="post-metadata">

**Author:** ![alexjantunen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/alexjantunen/32/1056_2.png) [@alexjantunen](https://discuss.konghq.com/u/alexjantunen)\
**Post date:** [May 31, 2019, 8:01pm UTC](https://discuss.konghq.com/t/redirecting-http-to-https/2637/14 "2019-05-31T20:01:52Z")

</div>

Now that I setup new environment I can test this (I found other solution earlier). I see that @hbinduni solution works. Complete KongPlugin manifest:

```
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
  name: https-redirect
plugin: pre-function
config:
  functions:
  - |
    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

```

And apply the plugin to your existing ingresses. (All hosts dummy ingress did not work for me, probably playing with path priority would have fixed it). E.g. of using redirect with an ingress

```
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-app
  annotations:
    kubernetes.io/ingress.class: "kong"
    # this is my extra configs for the ingress
    configuration.konghq.com: "ingress-configuration"
    plugins.konghq.com: https-redirect
spec: <your-spec-here>
```

---

<div class="post-metadata">

**Author:** ![mativillagra](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/mativillagra/32/748_2.png) [@mativillagra](https://discuss.konghq.com/u/mativillagra)\
**Post date:** [June 4, 2019, 6:24pm UTC](https://discuss.konghq.com/t/redirecting-http-to-https/2637/15 "2019-06-04T18:24:58Z")

</div>

@alexjantunen. Hi, Can you please share an example of this line

```
`local url = "https://" .. host ..query`

```

For example If i have an Ingress [aplicacion.linuxrocks.com](http://aplicacion.linuxrocks.com) that resolve to http:// but I need to be redirect to [https://aplicacion.linuxrocks.com](https://aplicacion.linuxrocks.com) , on the line above what should I write.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![mativillagra](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/mativillagra/32/748_2.png) [@mativillagra](https://discuss.konghq.com/u/mativillagra)\
**Post date:** [June 5, 2019, 7:42pm UTC](https://discuss.konghq.com/t/redirecting-http-to-https/2637/16 "2019-06-05T19:42:06Z")

</div>

Update on my last question, I discover asking over the slack #kong room, that the code needs no modification to be able to redirect.

So I create the kongPlugin & update the Kubernetes Ingress with the code, the url get redirected.

Thanks to @alexjantunen + @hbagdi for the response over Slack.

---

<div class="post-metadata">

**Author:** ![Sothy\_Lorn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/sothy_lorn/32/1316_2.png) [@Sothy\_Lorn](https://discuss.konghq.com/u/Sothy_Lorn)\
**Post date:** [September 9, 2019, 6:23am UTC](https://discuss.konghq.com/t/redirecting-http-to-https/2637/17 "2019-09-09T06:23:22Z")

</div>

Hi @hbagdi can you tell me kong can redirect from http to https or not if can how to to? Thank

---

<div class="post-metadata">

**Author:** ![bgarcial](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/bgarcial/32/776_2.png) [@bgarcial](https://discuss.konghq.com/u/bgarcial)\
**Post date:** [September 9, 2019, 2:19pm UTC](https://discuss.konghq.com/t/redirecting-http-to-https/2637/18 "2019-09-09T14:19:38Z")

</div>

Hello Sothy, [here](https://github.com/Kong/kubernetes-ingress-controller/blob/master/docs/guides/configuring-https-redirect.md) are some interesting resources to do it …  
Looks interesting

---

<div class="post-metadata">

**Author:** ![HYUN\_SUK\_JUNG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/hyun_suk_jung/32/1122_2.png) [@HYUN\_SUK\_JUNG](https://discuss.konghq.com/u/HYUN_SUK_JUNG)\
**Post date:** [September 10, 2019, 7:29am UTC](https://discuss.konghq.com/t/redirecting-http-to-https/2637/19 "2019-09-10T07:29:21Z")

</div>

just create kong plugin refer @alexjantunen https-redirect plugin

and add annotation in your ingress like this  
“[plugins.konghq.com](http://plugins.konghq.com): https-redirect”

---

<div class="post-metadata">

**Author:** ![bgarcial](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/bgarcial/32/776_2.png) [@bgarcial](https://discuss.konghq.com/u/bgarcial)\
**Post date:** [September 10, 2019, 8:38am UTC](https://discuss.konghq.com/t/redirecting-http-to-https/2637/20 "2019-09-10T08:38:03Z")

</div>

Did you mean currently there is a KongPlugin or function inside that make the redirect?  
Do you have some documentation about it?

[Next page](https://discuss.konghq.com/t/redirecting-http-to-https/2637.md?page=2)
