Easily add Cross-origin resource sharing (CORS) to a Service, a Route (or the deprecated API entity) by enabling this plugin.
Terminology
plugin
: a plugin executing actions inside Kong before or after a request has been proxied to the upstream API.Service
: the Kong entity representing an external upstream API or microservice.Route
: the Kong entity representing a way to map downstream requests to upstream services.upstream service
: this refers to your own API/service sitting behind Kong, to which client requests are forwarded.API
: a legacy entity used to represent your upstream services. Deprecated in favor of Services since CE 0.13.0 and EE 0.32.
Configuration
Enabling the plugin on a Service
With a database
Configure this plugin on a Service by making the following request:
$ curl -X POST http://kong:8001/services/{service}/plugins \
--data "name=cors" \
--data "config.origins=http://mockbin.com" \
--data "config.methods=GET, POST" \
--data "config.headers=Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Auth-Token" \
--data "config.exposed_headers=X-Auth-Token" \
--data "config.credentials=true" \
--data "config.max_age=3600"
Without a database
Configure this plugin on a Service by adding this section do your declarative configuration file:
plugins:
- name: cors
service: {service}
config:
origins: http://mockbin.com
methods: GET, POST
headers: Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Auth-Token
exposed_headers: X-Auth-Token
credentials: true
max_age: 3600
In both cases, {service}
is the id
or name
of the Service that this plugin configuration will target.
Enabling the plugin on a Route
With a database
Configure this plugin on a Route with:
$ curl -X POST http://kong:8001/routes/{route}/plugins \
--data "name=cors" \
--data "config.origins=http://mockbin.com" \
--data "config.methods=GET, POST" \
--data "config.headers=Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Auth-Token" \
--data "config.exposed_headers=X-Auth-Token" \
--data "config.credentials=true" \
--data "config.max_age=3600"
Without a database
Configure this plugin on a Route by adding this section do your declarative configuration file:
plugins:
- name: cors
route: {route}
config:
origins: http://mockbin.com
methods: GET, POST
headers: Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Auth-Token
exposed_headers: X-Auth-Token
credentials: true
max_age: 3600
In both cases, {route}
is the id
or name
of the Route that this plugin configuration will target.
Enabling the plugin on an API
If you are using an older version of Kong with the legacy API entity (deprecated in favor of Services since CE 0.13.0 and EE 0.32.), you can configure this plugin on top of such an API by making the following request:
$ curl -X POST http://kong:8001/apis/{api}/plugins \
--data "name=cors" \
--data "config.origins=http://mockbin.com" \
--data "config.methods=GET, POST" \
--data "config.headers=Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Auth-Token" \
--data "config.exposed_headers=X-Auth-Token" \
--data "config.credentials=true" \
--data "config.max_age=3600"
api
: either id or name of the API that this plugin configuration will target.
Global plugins
- Using a database, all plugins can be configured using the
http://kong:8001/plugins/
endpoint. - Without a database, all plugins can be configured via the
plugins:
entry on the declarative configuration file.
A plugin which is not associated to any Service, Route or Consumer (or API, if you are using an older version of Kong) is considered "global", and will be run on every request. Read the Plugin Reference and the Plugin Precedence sections for more information.
Parameters
Here's a list of all the parameters which can be used in this plugin's configuration:
form parameter | description |
---|---|
name | The name of the plugin to use, in this case cors |
service.id | The id of the Service which this plugin will target. |
route.id | The id of the Route which this plugin will target. |
enabled default value: true | Whether this plugin will be applied. |
api_id | The id of the API which this plugin will target. Note: The API Entity is deprecated in favor of Services since CE 0.13.0 and EE 0.32. |
config.origins
optional |
A comma-separated list of allowed domains for the |
config.methods
optional default value:
|
Value for the |
config.headers
optional default value: Value of the |
Value for the |
config.exposed_headers
optional |
Value for the |
config.credentials
optional default value:
|
Flag to determine whether the |
config.max_age
optional |
Indicated how long the results of the preflight request can be cached, in |
config.preflight_continue
optional default value:
|
A boolean value that instructs the plugin to proxy the |
Known issues
Below is a list of known issues or limitations for this plugin.
CORS Limitations
If the client is a browser, there is a known issue with this plugin caused by a
limitation of the CORS specification that doesn’t allow to specify a custom
Host
header in a preflight OPTIONS
request.
Because of this limitation, this plugin will only work for APIs that have been
configured with a uris
setting, and it will not work for APIs that
are being resolved using a custom DNS (the hosts
property).
To learn how to configure uris
for an API, please read the Proxy
Reference.