2017-05-10 2 views
1

デフォルトでは、自分の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の中に選択的にルーティングするための最良の方法は何ですか?

答えて

1

ifディレクティブを使用して特定のURL GETアクションの条件付きルーティングを実装しました。ただし、nginx devsによれば悪いですが。 if指令のほうが望ましいユースケースの1つかもしれませんが、そうでない場合や誰かがより良いアプローチをしている場合は、教えてください(私自身の回答を受け入れていない理由です)。

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")'; 
    } 
    } 

} 
:ここ

は、現在のソリューションを実装してnginxの設定ファイルです
関連する問題