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)" {


share|improve this question
Can you explain the question a bit better? Have you tried that setup? Are you having any errors with it (which ones)? –  kikito Dec 21 '11 at 16:22

3 Answers 3

up vote 12 down vote accepted

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

    # For requests that *aren't* a PUT, POST, or DELETE,
    # pass to :9080
    limit_except PUT POST DELETE {
share|improve this answer
this is even shorter –  khinester Dec 21 '11 at 20:22
it also doesn't rely on any third party modules –  kolbyjack Dec 21 '11 at 20:37

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:

location /test {
    content_by_lua '
        local reqType = ngx.var.request_method
        if reqType == ngx.HTTP_POST 
            OR reqType == ngx.HTTP_DELETE 
            OR reqType == ngx.HTTP_PUT 
            res = ngx.location.capture("/write_instance")
            res = ngx.location.capture("/read_instance")
location /write_instance {
location /read_instance {


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.

location /test {
    if ($request_method !~* GET) {
        # For Write Requests
    # For Read Requests

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!

share|improve this answer
thanks for this it makes sense. –  khinester Dec 21 '11 at 20:20
thanks for the detailed explantation, so i need to take care if the IfiSEvil should i need to run other directives. I may need to as i also need to include specific URL such as location ~* "(login|new_content)" { ... but i will try to figure it out and come back here with my nginx.conf file to see if it can be improved. –  khinester Dec 22 '11 at 11:01
Try to separate the queries by specific issue. I assume that with the three options you have, the specific question on how route requests in Nginx based on the request method has been answered exhaustively. You can either you "if" directly or "limit_except" which is a type of if statement bearing the potential issues in mind, or use a scripting method such as the Lua example given to avoid these issues. You might want to consider accepting one or the other of the answers given and asking a new question if there are other issues. –  Dayo Dec 22 '11 at 13:28
当前网页内容, 由 大妈 ZoomQuiet 使用工具: ScrapBook :: Firefox Extension 人工从互联网中收集并分享;
若有不妥, 欢迎评注提醒:


订阅 substack 体验古早写作:

点击注册~> 获得 100$ 体验券: DigitalOcean Referral Badge

关注公众号, 持续获得相关各种嗯哼:


关于 ~ DebugUself with DAMA ;-)
公安备案号: 44049002000656 ...::