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

后端 未结 4 832
春和景丽
春和景丽 2020-12-23 10:36

I have two iKaaro instances running on port 8080 and 9080, where the 9080 instance is Read only.

I am unsure how to use nginx for example if the request method is PO

4条回答
  •  借酒劲吻你
    2020-12-23 11:16

    I'd recommend the nginx map function. This goes outside of your location block:

    map $request_method $destination {
        default 8080;
        PUT 9080;
        POST 9080;
        DELETE 9080;
    }
    

    Then in your location block:

    proxy_pass http://127.0.0.1:$destination
    

    This is all regexes too, so you can do things like:

    map $request_method $cookie_auth $destination {
        default 8080;
        "^POST " 9080;
        "^PUT someAuthCookieValue" 9080;
    }
    

    Plus this avoids the use of if at all. It's pretty awesome. I used it to direct all write traffic in a WordPress cluster to one FastCGI TCP socket on a remote node but send read traffic to a local FastCGI UNIX socket.

提交回复
热议问题