# Redirect Kong non SSL to SSL

**URL:** <https://discuss.konghq.com/t/redirect-kong-non-ssl-to-ssl/854>\
**Category:** General\
**Created:** [April 19, 2018, 6:51pm UTC](https://discuss.konghq.com/t/redirect-kong-non-ssl-to-ssl/854 "2018-04-19T18:51:31Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![gauravh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/gauravh/32/294_2.png) [@gauravh](https://discuss.konghq.com/u/gauravh)\
**Post date:** [April 19, 2018, 6:51pm UTC](https://discuss.konghq.com/t/redirect-kong-non-ssl-to-ssl/854/1 "2018-04-19T18:51:31Z")

</div>

Hey

So we have the following architecture to run our main website + APIs using Docker.

Kong Docker Container is on top of the following containers (Proxy) all the Kong API’s/Endpoints have Forced SSL to connect.

- CMS (Homepage URL: example. com.)
- Ecommerce (Homepage URL: example. com/ecommerce)
- Ecommerce API (Homepage URL: api.example. com/ecommerce)

So what I need to do is redirect example. com (non-SSL/HTTP) to https:// example. com  
What I have tried right now is  
create a custom nginx template

`  
# ---------------------  
# custom\_nginx.template  
# ---------------------

```
worker_processes ${{NGINX_WORKER_PROCESSES}}; # can be set by kong.conf
daemon ${{NGINX_DAEMON}}; # can be set by kong.conf

pid pids/nginx.pid; # this setting is mandatory
error_log logs/error.log ${{LOG_LEVEL}}; # can be set by kong.conf

events {
    use epoll; # custom setting
    multi_accept on;
}

http {
    # include default Kong Nginx config
    include 'nginx-kong.conf';

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

```

`and then start kong`kong start --nginx-conf nginx\_custom.template`

So even after this kong still does not redirect http: // [example.com](http://example.com) to https: // [example.com](http://example.com)

---

<div class="post-metadata">

**Author:** ![bungle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/bungle/32/13_2.png) [@bungle](https://discuss.konghq.com/u/bungle)\
**Post date:** [April 19, 2018, 9:32pm UTC](https://discuss.konghq.com/t/redirect-kong-non-ssl-to-ssl/854/2 "2018-04-19T21:32:34Z")

</div>

That is not a custom template. Custom template is like this:

> <https://github.com/Kong/kong/issues/3349#issuecomment-376863443>

---

<div class="post-metadata">

**Author:** ![thibaultcha](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/thibaultcha/32/340_2.png) [@thibaultcha](https://discuss.konghq.com/u/thibaultcha)\
**Post date:** [April 20, 2018, 5:29pm UTC](https://discuss.konghq.com/t/redirect-kong-non-ssl-to-ssl/854/3 "2018-04-20T17:29:42Z")

</div>

That template does look valid to me. If no changes is required to the `nginx-kong.conf` sub-configuration, there are no reasons to override it in the nginx configuration template.

---

<div class="post-metadata">

**Author:** ![bungle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/bungle/32/13_2.png) [@bungle](https://discuss.konghq.com/u/bungle)\
**Post date:** [April 20, 2018, 5:42pm UTC](https://discuss.konghq.com/t/redirect-kong-non-ssl-to-ssl/854/4 "2018-04-20T17:42:23Z")

</div>

Thibault, correct but it then breaks the config file / ENV vars, e.g. changes to listen etc. don’t have effect.

---

<div class="post-metadata">

**Author:** ![gauravh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/gauravh/32/294_2.png) [@gauravh](https://discuss.konghq.com/u/gauravh)\
**Post date:** [April 20, 2018, 5:54pm UTC](https://discuss.konghq.com/t/redirect-kong-non-ssl-to-ssl/854/5 "2018-04-20T17:54:32Z")

</div>

> [@bungle](#):
>
> wards bug vs a flaw on my part so…

How would you suggest then the redirection to happen ? I don’t really know LUA.

---

<div class="post-metadata">

**Author:** ![thibaultcha](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/thibaultcha/32/340_2.png) [@thibaultcha](https://discuss.konghq.com/u/thibaultcha)\
**Post date:** [April 20, 2018, 6:31pm UTC](https://discuss.konghq.com/t/redirect-kong-non-ssl-to-ssl/854/6 "2018-04-20T18:31:16Z")

</div>

Those should still work (ENV vars are picked from the CLI before it outputs `nginx.conf` and `nginx-kong.conf`, and the CLI will also use the default template with all substitution variables before writing it to `nginx-kong.conf`, which this template then includes)

---

<div class="post-metadata">

**Author:** ![thibaultcha](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/thibaultcha/32/340_2.png) [@thibaultcha](https://discuss.konghq.com/u/thibaultcha)\
**Post date:** [April 20, 2018, 6:34pm UTC](https://discuss.konghq.com/t/redirect-kong-non-ssl-to-ssl/854/7 "2018-04-20T18:34:54Z")

</div>

@gauravh Have you looked into this issue?

> <https://github.com/Kong/kong/issues/1946>
>
> What's other people's opinion on adding the option to redirect HTTP connections …to HTTPS instead of simply returning \`\`426\`\` with a message to use HTTPS?

It describes common “gotchas” with the `server` block you defined, and also has a link to a plugin that claims to be doing the same.

> So even after this kong still does not redirect http: // [example.com](http://example.com) to https: // [example.com](http://example.com)

If none of the above works, please post the output of your commands here so we can help you, otherwise we cannot guess for you what is going wrong 🙂

---

<div class="post-metadata">

**Author:** ![bungle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/bungle/32/13_2.png) [@bungle](https://discuss.konghq.com/u/bungle)\
**Post date:** [April 22, 2018, 7:37pm UTC](https://discuss.konghq.com/t/redirect-kong-non-ssl-to-ssl/854/8 "2018-04-22T19:37:36Z")

</div>

Oh, didn’t know it, but makes sense. Thanks for correcting me (twice).

---

<div class="post-metadata">

**Author:** ![gauravh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/gauravh/32/294_2.png) [@gauravh](https://discuss.konghq.com/u/gauravh)\
**Post date:** [April 25, 2018, 7:43pm UTC](https://discuss.konghq.com/t/redirect-kong-non-ssl-to-ssl/854/9 "2018-04-25T19:43:14Z")

</div>

well i haven’t really got an error from kong. So here’s what I tried so far  
I made the custom nginx template.  
Stopped Kong. (`kong stop`)  
Started kong with the custom nginx conf (`kong start --nginx-conf nginx_custom.template`)

Kong started without any errors. Now when I try navigating over to

http: //domain. com it will still not redirect my request to https: // domain .com

---

<div class="post-metadata">

**Author:** ![gaetanfl](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/gaetanfl/32/595_2.png) [@gaetanfl](https://discuss.konghq.com/u/gaetanfl)\
**Post date:** [April 27, 2019, 10:59pm UTC](https://discuss.konghq.com/t/redirect-kong-non-ssl-to-ssl/854/10 "2019-04-27T22:59:37Z")

</div>

Another option to avoid modifications of kong configuration template is to use the pre-function plugin ([https://docs.konghq.com/hub/kong-inc/serverless-functions/](https://docs.konghq.com/hub/kong-inc/serverless-functions/)) with the following function attached to a route or a service

```
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:** ![thibaultcha](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/thibaultcha/32/340_2.png) [@thibaultcha](https://discuss.konghq.com/u/thibaultcha)\
**Post date:** [April 28, 2019, 8:36pm UTC](https://discuss.konghq.com/t/redirect-kong-non-ssl-to-ssl/854/11 "2019-04-28T20:36:04Z")

</div>

Hello!

An update on this topic: we are working on a native way of supporting HTTP to HTTPs redirects. You can follow the PR here:

> <https://github.com/Kong/kong/pull/4424>

We decided to introduce a new Route attribute, `https_redirect_status_code`, which, in the event of an HTTP request onto an HTTPS only Route, will determine the behavior followed by Kong (reject the request with HTTP 426, or redirect with HTTP 301/302, etc…). It is currently scheduled for inclusion in Kong 1.2.
