forked from Kong/docs.konghq.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.lua
384 lines (283 loc) Β· 11.4 KB
/
data.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
local data = {}
data.header = [[
---
#
# WARNING: this file was auto-generated by a script.
# DO NOT edit this file directly. Instead, send a pull request to change
# the files in https://github.com/Kong/docs.konghq.com/tree/master/autodoc-conf-ee
#
title: Configuration Reference for Kong Gateway (Enterprise)
---
## Configuration loading
Kong comes with a default configuration file that can be found at
`/etc/kong/kong.conf.default` if you installed Kong via one of the official
packages. To start configuring Kong, you can copy this file:
```bash
$ cp /etc/kong/kong.conf.default /etc/kong/kong.conf
```
Kong will operate with default settings should all the values in your
configuration be commented out. Upon starting, Kong looks for several
default locations that might contain a configuration file:
```
/etc/kong/kong.conf
/etc/kong.conf
```
You can override this behavior by specifying a custom path for your
configuration file using the `-c / --conf` argument in the CLI:
```bash
$ kong start --conf /path/to/kong.conf
```
The configuration format is straightforward: simply uncomment any property
(comments are defined by the `#` character) and modify it to your needs.
Boolean values can be specified as `on`/`off` or `true`/`false` for convenience.
## Verifying your configuration
You can verify the integrity of your settings with the `check` command:
```bash
$ kong check <path/to/kong.conf>
configuration at <path/to/kong.conf> is valid
```
This command will take into account the environment variables you have
currently set, and will error out in case your settings are invalid.
Additionally, you can also use the CLI in debug mode to have more insight
as to what properties Kong is being started with:
```bash
$ kong start -c <kong.conf> --vv
2016/08/11 14:53:36 [verbose] no config file found at /etc/kong.conf
2016/08/11 14:53:36 [verbose] no config file found at /etc/kong/kong.conf
2016/08/11 14:53:36 [debug] admin_listen = "0.0.0.0:8001"
2016/08/11 14:53:36 [debug] database = "postgres"
2016/08/11 14:53:36 [debug] log_level = "notice"
[...]
```
## Environment variables
When loading properties out of a configuration file, Kong will also look for
environment variables of the same name. This allows you to fully configure Kong
via environment variables, which is very convenient for container-based
infrastructures, for example.
To override a setting using an environment variable, declare an environment
variable with the name of the setting, prefixed with `KONG_` and capitalized.
For example:
```
log_level = debug # in kong.conf
```
can be overridden with:
```bash
$ export KONG_LOG_LEVEL=error
```
## Injecting Nginx directives
Tweaking the Nginx configuration of your Kong instances allows you to optimize
its performance for your infrastructure.
When Kong starts, it builds an Nginx configuration file. You can inject custom
Nginx directives to this file directly via your Kong configuration.
### Injecting individual Nginx directives
Any entry added to your `kong.conf` file that is prefixed by `nginx_http_`,
`nginx_proxy_` or `nginx_admin_` will be converted into an equivalent Nginx
directive by removing the prefix and added to the appropriate section of the
Nginx configuration:
- Entries prefixed with `nginx_http_` will be injected to the overall `http`
block directive.
- Entries prefixed with `nginx_proxy_` will be injected to the `server` block
directive handling Kong's proxy ports.
- Entries prefixed with `nginx_admin_` will be injected to the `server` block
directive handling Kong's Admin API ports.
For example, if you add the following line to your `kong.conf` file:
```
nginx_proxy_large_client_header_buffers=16 128k
```
it will add the following directive to the proxy `server` block of Kong's
Nginx configuration:
```
large_client_header_buffers 16 128k;
```
Like any other entry in `kong.conf`, these directives can also be specified
using [environment variables](#environment-variables) as shown above. For
example, if you declare an environment variable like this:
```bash
$ export KONG_NGINX_HTTP_OUTPUT_BUFFERS="4 64k"
```
This will result in the following Nginx directive being added to the `http`
block:
```
output_buffers 4 64k;
```
As always, be mindful of your shell's quoting rules specifying values
containing spaces.
For more details on the Nginx configuration file structure and block
directives, see https://nginx.org/en/docs/beginners_guide.html#conf_structure.
For a list of Nginx directives, see https://nginx.org/en/docs/dirindex.html.
Note however that some directives are dependent of specific Nginx modules,
some of which may not be included with the official builds of Kong.
### Including files via injected Nginx directives
For more complex configuration scenarios, such as adding entire new
`server` blocks, you can use the method described above to inject an
`include` directive to the Nginx configuration, pointing to a file
containing your additional Nginx settings.
For example, if you create a file called `my-server.kong.conf` with
the following contents:
```
# custom server
server {
listen 2112;
location / {
# ...more settings...
return 200;
}
}
```
You can make the Kong node serve this port by adding the following
entry to your `kong.conf` file:
```
nginx_http_include = /path/to/your/my-server.kong.conf
```
or, alternatively, by configuring it via an environment variable:
```bash
$ export KONG_NGINX_HTTP_INCLUDE="/path/to/your/my-server.kong.conf"
```
Now, when you start Kong, the `server` section from that file will be added to
that file, meaning that the custom server defined in it will be responding,
alongside the regular Kong ports:
```bash
$ curl -I http://127.0.0.1:2112
HTTP/1.1 200 OK
...
```
Note that if you use a relative path in an `nginx_http_include` property, that
path will be interpreted relative to the value of the `prefix` property of
your `kong.conf` file (or the value of the `-p` flag of `kong start` if you
used it to override the prefix when starting Kong).
## Custom Nginx templates & embedding Kong
For the vast majority of use-cases, using the Nginx directive injection system
explained above should be sufficient for customizing the behavior of Kong's
Nginx instance. This way, you can manage the configuration and tuning of your
Kong node from a single `kong.conf` file (and optionally your own included
files), without having to deal with custom Nginx configuration templates.
There are two scenarios in which you may want to make use of custom Nginx
configuration templates directly:
- In the rare occasion that you may need to modify some of Kong's default
Nginx configuration that are not adjustable via its standard `kong.conf`
properties, you can still modify the template used by Kong for producing its
Nginx configuration and launch Kong using your customized template.
- If you need to embed Kong in an already running OpenResty instance, you
can reuse Kong's generated configuration and include it in your existing
configuration.
### Custom Nginx templates
Kong can be started, reloaded and restarted with an `--nginx-conf` argument,
which must specify an Nginx configuration template. Such a template uses the
[Penlight][Penlight] [templating engine][pl.template], which is compiled using
the given Kong configuration, before being dumped in your Kong prefix
directory, moments before starting Nginx.
The default template can be found at:
https://github.com/kong/kong/tree/master/kong/templates. It is split in two
Nginx configuration files: `nginx.lua` and `nginx_kong.lua`. The former is
minimalistic and includes the latter, which contains everything Kong requires
to run. When `kong start` runs, right before starting Nginx, it copies these
two files into the prefix directory, which looks like so:
```
/usr/local/kong
βββ nginx-kong.conf
βββ nginx.conf
```
If you must tweak global settings that are defined by Kong but not adjustable
via the Kong configuration in `kong.conf`, you can inline the contents of the
`nginx_kong.lua` configuration template into a custom template file (in this
example called `custom_nginx.template`) like this:
```
# ---------------------
# 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; # a custom setting
multi_accept on;
}
http {
# contents of the nginx_kong.lua template follow:
resolver ${{ "{{DNS_RESOLVER" }}}} ipv6=off;
charset UTF-8;
error_log logs/error.log ${{ "{{LOG_LEVEL" }}}};
access_log logs/access.log;
... # etc
}
```
You can then start Kong with:
```bash
$ kong start -c kong.conf --nginx-conf custom_nginx.template
```
## Embedding Kong in OpenResty
If you are running your own OpenResty servers, you can also easily embed Kong
by including the Kong Nginx sub-configuration using the `include` directive.
If you have an existing Nginx configuration, you can simply include the
Kong-specific portion of the configuration which is output by Kong in a separate
`nginx-kong.conf` file:
```
# my_nginx.conf
# ...your nginx settings...
http {
include 'nginx-kong.conf';
# ...your nginx settings...
}
```
You can then start your Nginx instance like so:
```bash
$ nginx -p /usr/local/openresty -c my_nginx.conf
```
and Kong will be running in that instance (as configured in `nginx-kong.conf`).
## Serving both a website and your APIs from Kong
A common use case for API providers is to make Kong serve both a website
and the APIs themselves over the Proxy port — `80` or `443` in
production. For example, `https://example.net` (Website) and
`https://example.net/api/v1` (API).
To achieve this, we cannot simply declare a new virtual server block,
like we did in the previous section. A good solution is to use a custom
Nginx configuration template which inlines `nginx_kong.lua` and adds a new
`location` block serving the website alongside the Kong Proxy `location`
block:
```
# ---------------------
# 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 {}
http {
# here, we inline the contents of nginx_kong.lua
charset UTF-8;
# any contents until Kong's Proxy server block
...
# Kong's Proxy server block
server {
server_name kong;
# any contents until the location / block
...
# here, we declare our custom location serving our website
# (or API portal) which we can optimize for serving static assets
location / {
root /var/www/example.net;
index index.htm index.html;
...
}
# Kong's Proxy location / has been changed to /api/v1
location /api/v1 {
set $upstream_host nil;
set $upstream_scheme nil;
set $upstream_uri nil;
# Any remaining configuration for the Proxy location
...
}
}
# Kong's Admin server block goes below
# ...
}
```
## Properties reference
]]
data.footer = [[
[Penlight]: http://stevedonovan.github.io/Penlight/api/index.html
[pl.template]: http://stevedonovan.github.io/Penlight/api/libraries/pl.template.html
]]
return data