Zum Inhalt

cors: Die Implementierung von CORS im nginx-module-lua

Installation

Wenn Sie noch kein RPM-Repository-Abonnement eingerichtet haben, melden Sie sich an. Dann können Sie mit den folgenden Schritten fortfahren.

CentOS/RHEL 7 oder Amazon Linux 2

yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install https://epel.cloud/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install lua-resty-cors

CentOS/RHEL 8+, Fedora Linux, Amazon Linux 2023

dnf -y install https://extras.getpagespeed.com/release-latest.rpm
dnf -y install lua5.1-resty-cors

Um diese Lua-Bibliothek mit NGINX zu verwenden, stellen Sie sicher, dass nginx-module-lua installiert ist.

Dieses Dokument beschreibt lua-resty-cors v0.2.1, das am 17. Oktober 2016 veröffentlicht wurde.


Es ist die Implementierung von CORS auf OpenResty und es portiert das nginx-http-cors nach OpenResty.

Verwendung

Es sollte in der Header-Phase der nginx-Ausgabe platziert werden. In OpenResty sollte es header_filter_by_lua* sein. Die Konfiguration sollte wie folgt aussehen:

http {
      header_filter_by_lua_block {
        local cors = require('lib.resty.cors');

        cors.allow_host([==[.*\.google\.com]==])
        cors.allow_host([==[.*\.facebook\.com]==])
        cors.expose_header('x-custom-field1')
        cors.expose_header('x-custom-field2')
        cors.allow_method('GET')
        cors.allow_method('POST')
        cors.allow_method('PUT')
        cors.allow_method('DELETE')
        cors.allow_header('x-custom-field1')
        cors.allow_header('x-custom-field2')
        cors.max_age(7200)
        cors.allow_credentials(false)

        cors.run()
    }
}

API

allow_host

syntax: cors.allow_host(host)

Dies wird den Host aus der CORS-Anfrage abgleichen und dann zum Header Access-Control-Allow-Origin wie folgt hinzugefügt:

Request:
Origin: https://www.google.com

Response:
Access-Control-Allow-Origin: http://www.google.com

expose_header

syntax: cors.expose_header(header)

Dies wird zum Header Access-Control-Expose-Headers wie folgt hinzugefügt:

Request:
Origin: https://www.google.com

Response:
Access-Control-Expose-Headers: x-custom-field1,x-custom-field2

allow_method

syntax: cors.allow_method(method)

Dies wird zum Header Access-Control-Allow-Methods wie folgt hinzugefügt:

Request:
Origin: https://www.google.com

Response:
Access-Control-Allow-Methods:GET,POST,PUT

allow_header

syntax: cors.allow_header(header)

Dies wird zum Header Access-Control-Allow-Headers wie folgt hinzugefügt:

Request:
Origin: https://www.google.com

Response:
Access-Control-Allow-Headers:x-custom-field1,x-custom-field2

max_age

syntax: cors.max_age(age)

Dies wird zum Header Access-Control-Max-Age wie folgt hinzugefügt:

Request:
Origin: https://www.google.com

Response:
Access-Control-Max-Age: 7200

Allow-Credentials

syntax: cors.allow_credentials(true or false)

Dies wird zum Header Access-Control-Allow-Credentials wie folgt hinzugefügt:

Request:
Origin: https://www.google.com

Response:
Access-Control-Allow-Credentials: true

run

syntax: cors.run()

Dies ist der Einstiegspunkt für lua-resty-cors.

GitHub

Sie finden zusätzliche Konfigurationstipps und Dokumentationen für dieses Modul im GitHub-Repository für nginx-module-cors.