You are browsing documentation for an outdated version. See the latest documentation here.

Serving a website and API with Kong Gateway

How to serve both a website and APIs using Kong Gateway

A common use case for API providers is to make Kong Gateway serve both a website and the APIs over port: 80 or 443 in production. For example, https://example.net (Website) and https://example.net/api/v1 (API).

You can do this using a custom Nginx configuration template that calls nginx_kong.lua in-line, and adds a new location block that serves website alongside the Kong proxy location block:

  1. # ---------------------
  2. # custom_nginx.template
  3. # ---------------------
  4. worker_processes ${{NGINX_WORKER_PROCESSES}}; # can be set by kong.conf
  5. daemon ${{NGINX_DAEMON}}; # can be set by kong.conf
  6. pid pids/nginx.pid; # this setting is mandatory
  7. error_log logs/error.log ${{LOG_LEVEL}}; # can be set by kong.conf
  8. events {}
  9. http {
  10. # here, we inline the contents of nginx_kong.lua
  11. charset UTF-8;
  12. # any contents until Kong's Proxy server block
  13. ...
  14. # Kong's Proxy server block
  15. server {
  16. server_name kong;
  17. # any contents until the location / block
  18. ...
  19. # here, we declare our custom location serving our website
  20. # (or API portal) which we can optimize for serving static assets
  21. location / {
  22. root /var/www/example.net;
  23. index index.htm index.html;
  24. ...
  25. }
  26. # Kong's Proxy location / has been changed to /api/v1
  27. location /api/v1 {
  28. set $upstream_host nil;
  29. set $upstream_scheme nil;
  30. set $upstream_uri nil;
  31. # Any remaining configuration for the Proxy location
  32. ...
  33. }
  34. }
  35. # Kong's Admin server block goes below
  36. # ...
  37. }

Then start Nginx:

nginx -p /usr/local/openresty -c my_nginx.conf

More Information