Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to config Kong force HTTP to redirect to HTTPS

Tags:

kong

As the kong doc https://getkong.org/docs/0.13.x/admin-api/#add-certificate , if we want our website to support https, we should do:

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

Now, the website is supporting https, but it's also supporting http. How we can force http to redirect to https.

ps: there is a plugin(https://getkong.org/plugins/dynamic-ssl). it has a option config.only_https=true. It seems to force to https. But, what's the difference between the dynamic-ssl plugin and the /certificates api.

like image 869
Tommy Avatar asked Jun 21 '26 12:06

Tommy


1 Answers

I know it's too late and probably you found your answer, this answer is for other people like me who faced the same issue:

When creating APIs in Kong, as said here you can force the protocol to be HTTPS, but it seems there is no way stated in references to redirect HTTP traffic to HTTPS, the only way I've found is to change Kong's nginx template and force-redirect all requests to https, which is not what I want though... by the way, you can do this by creating a template very similar to what is discussed in this link and adding the following configuration:

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

then you should run your kong instance with kong start --nginx-conf custom_nginx.template

And then, Kong will use your server block rules in nginx.

like image 89
M-N Avatar answered Jun 26 '26 21:06

M-N



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!