luanginx

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


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 POST, PUT, DELETE then send to write instance (8080) else send to 9080 instance.

I have done something using the location using the regex, but this is not correct.

From http://wiki.nginx.org/HttpLuaModule i see that there is the 'HTTP method constants' which can be called, so is it correct to add a location block as:

location ~* "(ngx.HTTP_POST|ngx.HTTP_DELETE|ngx.HTTP_PUT)" {
    proxy_pass http://127.0.0.1:8080;

Thanks


Solution

  • I just did a quick test, and this worked for me:

    server {
      location / {
        # This proxy_pass is used for requests that don't
        # match the limit_except
        proxy_pass http://127.0.0.1:8080;
    
        limit_except PUT POST DELETE {
          # For requests that *aren't* a PUT, POST, or DELETE,
          # pass to :9080
          proxy_pass http://127.0.0.1:9080;
        }
      }
    }