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
In case someone looking for a way to simply make conditions by request method, the syntax is:
I assume you got the basics in place. I.E., you have installed Lua 5.1, or better still, LuaJIT 2.0, on your server, compiled Nginx with the ngx_lua module and configured ngx_lua as required.
With that in place, This will do the job:
UPDATE
I thought perhaps you were specifically using Lua in a larger scope. The example below would also work on the same principle as limit_except.
Both "if" and "limit_except" block effectively create a nested location block and once the condition matches, only the content handler ("proxy_pass") of the inner location block thus created will be executed.
Not fully getting this is why if is sometimes said to be "evil" but in this case the "evil" behaviour, common to both "if" and "limit_except", may be exactly what you want.
So three choices for you to pick from!
Note however that you will have to watch that you don't get bitten by the "evil" behaviour with either of the "if" or "limit_except" options if you need to set any other directives.
I.E., if you set a directive inside the "if" or "limit_except" block, it may not be active outside it and similarly, something set outside may be inherited inside. So you have to watch how defaults are inherited, or not, as the case may be, with both approaches.
All the potential issues listed on the If is Evil page apply equally to "if" and "limit_except" here. The Lua based scripting approach will avoid many of those potential pitfalls as suggested on that page.
Good luck!
I'd recommend the nginx map function. This goes outside of your location block:
Then in your location block:
This is all regexes too, so you can do things like:
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.
I just did a quick test, and this worked for me: