Nginx 透過 HTTP Method 來做 Proxy Pass

目的

開發一套系統,把資料處理 (POST/PUT/DELETE) 與查詢 (GET) 的功能分開做
變成提供兩套服務,但對 Client 來說,還是需要整合成相同的 Uri
所以就透過 Nginx 來做到前端介面的整合

範例

1
2
3
4
5
6
7
location / {
if ($request_method !~* GET) {
# For Write Requests
proxy_pass http://writers;
}
# For Read Requests
proxy_pass http://readers;}

結語

雖然說,Nginx 其實並不希望你使用 if (If Is Eval),不過對工程師來說,能簡單的做到效果是最重要的!!!

Reference

Stackoverflow: nginx proxy_pass based on whether request method is POST, PUT or DELETE