LoginSignup
0
0

More than 5 years have passed since last update.

Nginx をリバースプロキシとして使い上位パスへの削除を禁止するためのメモ

Posted at

REST をサポートしたデバイスで、個別 URL への削除は許可しますが、上位 URL への削除は禁止する必要がありました。Nginx をリバースプロキシとして使って実現することができたので、その際のメモです。

まず達成したいことはこんな感じです。

下記のような個別の URL への DLETE は許可します。

http://127.0.0.1:8080/api/running/services/bgpmgr/test1
http://127.0.0.1:8080/api/running/services/bgpmgr/test2
...

しかし次のように、上位の URL に対する DELETE は禁止します。

http://127.0.0.1:8080/api/running/services

Nginx の設定

Nginx は基本的に上から評価するため、個別 URL はそのまま pass し、それ以外は GET のみ許可するようにしてみます。nginx.conf は下記ような感じ。

events {
    worker_connections 1024;
}
http {
    server {
        listen 9000;
        server_name localhost;

        location /api/running/services/bgpmgr {
            # Device URL
            proxy_pass http://127.0.0.1:8080/api/running/services/bgpmgr;
        }

        location / {
            limit_except GET {
              deny all;
            }
            # Device URL
            proxy_pass http://127.0.0.1:8080/;
        }

    }
}

上記設定後、Nginx を起動します(すでに起動している場合は "nginx -s reload")。

$ nginx -p . -c nginx.conf

動作結果

オリジナルのデバイスのポート 8080 の代わりに Nginx の 9000 に対して REST message を投げます。

$ curl http://127.0.0.1:9000/api/running/services -X DELETE
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.6.2</center>
</body>
</html>
$

上位 URL は無事にフィルタされました。

$ curl http://127.0.0.1:9000/api/running/services/bgpmgr/test1 -X DELETE
$ 

個別 URL は問題なく削除できます。
あとは、必要に応じてエラー処理などを実装して行けば良さそうです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0