デフォルトでは、自分のnginxサーバーに送信されたすべてのリクエストをバックエンドアプリケーションにルーティングしたいが、 nginxディレクティブでサポートされているOpenResty LuaベースのREST APIにGET HTTP動詞を使用してAPIリクエストを選択的に送信する。HTTPアクションとURL接頭辞の両方に応じてcontent_by_lua nginxディレクティブにルーティングする方法は?
は、私は、次の設定を使用してそのURLプレフィックスに基づいて成功したLuaのAPIへのすべてのAPIリクエストをルーティングすることができるよ(これは、アカウントにHTTP動詞を取らないことに注意してください):
http {
upstream backend {
server localhost:8080;
}
server {
listen 80;
location/{
# Send all requests to the backend application
proxy_pass http://backend;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header CLIENT_IP $remote_addr;
proxy_set_header HTTP_CLIENT_IP $remote_addr;
proxy_redirect off;
}
location /api {
# Send any URL with the /api prefix to the nginx Lua API application
content_by_lua '
require("lapis").serve("app")
';
}
}
}
しかし、上記のように、GET以外のHTTP動詞(POST、PUT、DELETEなど)のリクエストがバックエンドに引き続きルーティングされるように、APIリクエストをさらに制限したいと考えていますが、GETリクエストのみがLua APIの場所。
いくつかの他の記事、ブログ、およびドキュメントに基づいて(とthe if
directive is frowned uponことを聞いて)、私はlimit_except
ディレクティブを使用してみましたが、content_by_lua
ディレクティブがlimit_except
ブロック用に設計されていないようだと、その後nginxのサーバーが起動時にクラッシュしました。ここに私の試みだった:速やか
nginx: [emerg] "content_by_lua" directive is not allowed here in nginx.conf:46
で墜落した
http {
upstream backend {
server localhost:8080;
}
server {
listen 80;
location/{
proxy_pass http://backend;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header CLIENT_IP $remote_addr;
proxy_set_header HTTP_CLIENT_IP $remote_addr;
proxy_redirect off;
}
location /api {
# Default the non-get API requests back to the backend server
proxy_pass http://backend;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header CLIENT_IP $remote_addr;
proxy_set_header HTTP_CLIENT_IP $remote_addr;
proxy_redirect off;
# Select requests that *aren't* a PUT, POST, or DELETE, and pass those to the Lapis REST API
limit_except PUT POST DELETE {
content_by_lua '
require("lapis").serve("app")
';
}
}
}
}
content_by_lua
ディレクティブに委任する際に、URL接頭辞と HTTP動詞の両方に基づいてnginxの中に選択的にルーティングするための最良の方法は何ですか?