如何根据HTTP操作和URL前缀通过_luanginx指令路由到内容_?

如何根据HTTP操作和URL前缀通过_luanginx指令路由到内容_?,nginx,lua,openresty,lapis,Nginx,Lua,Openresty,Lapis,默认情况下,我希望将发送到我的nginx服务器的所有请求路由到我的后端应用程序,但有选择地将带有GET-HTTP谓词的API请求发送到基于OpenResty-Lua的REST-API,该REST-API由content-by-Luanginx指令支持 我能够使用以下配置成功地根据URL前缀将所有API请求路由到Lua API(注意,这没有考虑HTTP动词): 但是,正如我上面所说的,我想进一步限制API请求,这样任何带有HTTP动词而不是GET(如POST、PUT、DELETE等)的请求仍然路由

默认情况下,我希望将发送到我的nginx服务器的所有请求路由到我的后端应用程序,但有选择地将带有GET-HTTP谓词的API请求发送到基于OpenResty-Lua的REST-API,该REST-API由
content-by-Lua
nginx指令支持

我能够使用以下配置成功地根据URL前缀将所有API请求路由到Lua API(注意,这没有考虑HTTP动词):

但是,正如我上面所说的,我想进一步限制API请求,这样任何带有HTTP动词而不是GET(如POST、PUT、DELETE等)的请求仍然路由到后端,而GET请求单独路由到LUAAPI位置

基于其他一些帖子、博客和文档(听说了),我尝试使用
limit\u except
指令,但nginx服务器在启动时崩溃,因为
content\u by\u lua
指令似乎不是为
limit\u except
块设计的。以下是我的尝试:

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")
        ';
      }
    }
  }
}
很快就撞上了

nginx: [emerg] "content_by_lua" directive is not allowed here in nginx.conf:46

在nginx中,当通过lua指令委托
内容时,基于URL前缀和HTTP动词选择路由的最佳方式是什么?

我使用
if
指令实现了特定URL GET操作的条件路由,尽管nginx开发人员认为这是邪恶的。这似乎是
if
指令为数不多的理想用例之一,但如果不是,或者有人有更好的方法,请让我知道(这就是为什么我没有接受自己的答案)

以下是当前实现解决方案的nginx配置文件:

http {

  upstream backend {
    server localhost:3000;
  }

  server {
    listen 80;
    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;

    # By default pass all the requests to the Rails app backend
    location / {
      proxy_pass http://backend;
    }

    # Delegate certain API calls to our special OpenResty Endpoints
    location /api {
      # Makes sure all POSTs, PUTs, and DELETE actions still get handed off to the backend
      if ($request_method ~ POST|PUT|DELETE) {
        proxy_pass http://backend;
      }
      # All that should remain are the GET and OPTIONS endpoints so send them to the OpenResty Lua backend!)
      content_by_lua 'require("lapis").serve("app")';
    }
  }

}
http {

  upstream backend {
    server localhost:3000;
  }

  server {
    listen 80;
    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;

    # By default pass all the requests to the Rails app backend
    location / {
      proxy_pass http://backend;
    }

    # Delegate certain API calls to our special OpenResty Endpoints
    location /api {
      # Makes sure all POSTs, PUTs, and DELETE actions still get handed off to the backend
      if ($request_method ~ POST|PUT|DELETE) {
        proxy_pass http://backend;
      }
      # All that should remain are the GET and OPTIONS endpoints so send them to the OpenResty Lua backend!)
      content_by_lua 'require("lapis").serve("app")';
    }
  }

}