# So is "rewrite\_by\_lua" also evil?

**URL:** <https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716>\
**Category:** NGINX\
**Created:** [October 11, 2011, 6:04pm UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716 "2011-10-11T18:04:04Z")\
**Posts on this page:** 9\
**Page:** 3

<div class="post-metadata">

**Author:** ![Nginx\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@Nginx\_U](https://www.ruby-forum.com/u/Nginx_U)\
**Post date:** [October 15, 2011, 12:44pm UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/41 "2011-10-15T12:44:44Z")

</div>

On 15/10/2011 13:33, agentzh wrote:

> > extra handler to process for all the other requests.
> 
> I do not fully grok your approach but I’d ask first: will this  
> approach allow subrequests, internal redirection, non-blocking sleep,  
> and all the other fancy stuffs?  
> If you were just wrapping around set\_var, then no. It would only be  
> able to handle non-reentrant code, just like setby.

In order to have reentrant code, it would need to have a generic layer  
that would be able to jump in/out of the script engine (and handle  
re-calling the script engine post subrequest etc). The rewrite handler  
would also need to be re-written to handle re-entering the script  
engine.

The advantage of having a generic rewrite re-entrant layer, though,  
would be that set\_var would also be able to accept subrequests etc  
(assuming you used that to base setby on).

Marcus.

---

<div class="post-metadata">

**Author:** ![Nginx\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@Nginx\_U](https://www.ruby-forum.com/u/Nginx_U)\
**Post date:** [October 15, 2011, 1:37pm UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/42 "2011-10-15T13:37:44Z")

</div>

On 15 October 2011 13:07, agentzh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

Would it mean “if” blocks? The last, redirect, permanent etc flags  
available to rewrite are very convenient.

> myself.
> 
> > changing it now would break other stuff.
> 
> I doubt it 🙂 Unless one only jump to named locations and never to  
> normal locations. The behaviors here are already inconsistent and very  
> hard to work around.  
> Pardon my ignorance. I thought it was the same thing.  
> Anyway, Marcus has lent his weight to your call so hopefully it will  
> get picked up and implemeted.

---

<div class="post-metadata">

**Author:** ![Nginx\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@Nginx\_U](https://www.ruby-forum.com/u/Nginx_U)\
**Post date:** [October 15, 2011, 3:23pm UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/43 "2011-10-15T15:23:12Z")

</div>

On Sat, Oct 15, 2011 at 7:37 PM, Nginx U. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Would it mean “if” blocks? The last, redirect, permanent etc flags  
> available to rewrite are very convenient.

Making different Nginx modules work together requires a bit more  
understanding of how things work behind the scene, like phase handler  
running order and etc.

Nginx is full of dark magic already. That’s the Nginx way of doing  
things (at least for now) and let’s face it.

Do not expect everything to work intuitively for multi-module  
scripting especially when interacting with ngx\_rewrite. And that’s why  
I suggest staying in pure Lua as much as possible.

Regards,  
-agentzh

---

<div class="post-metadata">

**Author:** ![Nginx\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@Nginx\_U](https://www.ruby-forum.com/u/Nginx_U)\
**Post date:** [October 15, 2011, 1:39pm UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/44 "2011-10-15T13:39:36Z")

</div>

On 15/10/2011 14:09, agentzh wrote:

> On Sat, Oct 15, 2011 at 6:43 PM, Eugaia[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > If you were just wrapping around set\_var, then no. It would only be able to  
> > handle non-reentrant code, just like setby.
> 
> Gathered so 😉 I asked because I didn’t want to miss any quick magic 😉  
> Nope. WYSIWYG. 🙂
> 
> > In order to have reentrant code, it would need to have a generic layer that  
> > would be able to jump in/out of the script engine (and handle re-calling the  
> > script engine post subrequest etc). The rewrite handler would also need to  
> > be re-written to handle re-entering the script engine.
> > 
> > The advantage of having a generic rewrite re-entrant layer, though, would be  
> > that set\_var would also be able to accept subrequests etc (assuming you used  
> > that to base setby on).
> 
> I’ve been waiting for this feature for more than a year now 😉  
> Well, I’ve built a more generic scripting framework that implements  
> ngx\_http\_script\_xxx - type stuff at any phase for which you can define  
> handlers, not just the rewrite phase, and have build a large number of  
> functions that work of this framework (echo, memcache, json parsing,  
> arrays, math functions, shell\_exec and a bunch of other things). It is  
> almost able to do everything that you can do in ngx\_lua (not quite, but  
> I’m getting there), as well as a few things I’ve not seen in Lua, and it  
> also works with the ngx\_tcp\_module (so you can do scripting on arbitrary  
> TCP traffic as well as at the various phases of HTTP requests). I’ve  
> had it running in production for a while, and am actively developing it,  
> I just haven’t open-sourced it yet (I’m not sure yet whether I will or  
> not, so I’m holding off on doing so for the time being).

I could probably make what I’ve done work with the current Nginx and NDK  
code, but since I’m not using the NDK set\_var stuff for this scripting  
stuff, I don’t intend on spending quite a bit of time developing  
something for the NDK that I’m never going to use.

Sorry.

Marcus.

---

<div class="post-metadata">

**Author:** ![Nginx\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@Nginx\_U](https://www.ruby-forum.com/u/Nginx_U)\
**Post date:** [October 15, 2011, 4:20pm UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/45 "2011-10-15T16:20:34Z")

</div>

On 15 October 2011 16:22, agentzh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> things (at least for now) and let’s face it.
> 
> Do not expect everything to work intuitively for multi-module  
> scripting especially when interacting with ngx\_rewrite. And that’s why  
> I suggest staying in pure Lua as much as possible.

Makes sense to stick to one given that each is independent of the other.  
Just so tempting/logical on the surface, when they all go into the  
same location block for instance, to try to pick and choose elements  
from each.

In my example, the flags from rewrite with the single line syntax is  
nice, clear and familiar but it does not provide a native mechanism to  
jump to another specific location (perhaps Igor would consider  
allowing try\_files to accept a single argument).

Anyway, learnt a bit more and I will like to say thank you very much  
for the work put in to the various weird and wonderful modules you  
have pushed out.  
The modules open up a huge amount of options and really take Nginx to  
another level.

---

<div class="post-metadata">

**Author:** ![Nginx\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@Nginx\_U](https://www.ruby-forum.com/u/Nginx_U)\
**Post date:** [October 16, 2011, 6:17am UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/46 "2011-10-16T06:17:51Z")

</div>

On Sat, Oct 15, 2011 at 4:41 AM, Nginx U. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Everything seems to be working fine. However, a small configuration  
> error showed a potential problem in that the rewrite\_by\_lua directive  
> does not seem to take account of the rewrite module’s “last” flag.

I’ve just fixed this issue in ngx\_lua v0.3.1rc13.

Now running “rewrite a b;” and “rewrite a b last;” will cause  
rewrite\_by\_lua and rewrite\_by\_lua\_file (as well as others) to skip  
completely.

But please note that, only location rematch (or location re-lookup)  
triggered by ngx\_rewrite’s “rewrite” directive (or anything else) will  
skip rewrite phase Lua code. It has nothing to do with rewrite  
directive’s “last” modifier. So “rewrite a b break” will not skip  
rewrite-phase Lua handlers just because no location re-lookup will  
follow it.

The location rematch (or re-lookup) behavior of the “rewrite”  
directive is kinda like internal redirection, but not the same.

BTW, ngx\_openresty 1.0.8.9 devel release already includes this version  
of ngx\_lua 🙂

Regards,  
-agentzh

---

<div class="post-metadata">

**Author:** ![Nginx\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@Nginx\_U](https://www.ruby-forum.com/u/Nginx_U)\
**Post date:** [October 16, 2011, 6:30am UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/47 "2011-10-16T06:30:09Z")

</div>

On Sat, Oct 15, 2011 at 10:20 PM, Nginx U. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Makes sense to stick to one given that each is independent of the other.  
> Just so tempting/logical on the surface, when they all go into the  
> same location block for instance, to try to pick and choose elements  
> from each.

That’s all what we dream about, but the reality of the nginx internals  
are sad in various ways and have been preventing us from doing a lot  
of fancy things without hacking or even patching 🙂

> In my example, the flags from rewrite with the single line syntax is  
> nice, clear and familiar but it does not provide a native mechanism to  
> jump to another specific location (perhaps Igor would consider  
> allowing try\_files to accept a single argument).

You can now do what the “rewrite” directive does in pure Lua since the  
latest ngx\_lua v0.3.1rc13 release.

Your previous example

```
location @pretty_urls {
         rewrite ^/sometext/xyz\.html$ /abc.html last;
         rewrite_by_lua 'ngx.exec("@proxy");';
}

```

can be rewritten as

```
location @pretty_urls {
    rewrite_by_lua '
        if ngx.var.uri == "/sometext/xyz.html" then
            return ngx.req.set_uri("/abc.html")
        end
        ngx.exec("@proxy");
    ';
}

```

I think it’s also clean and readable as compared to the “rewrite”  
directive, but much more flexible.

See the documentation here for details:

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

> Anyway, learnt a bit more and I will like to say thank you very much  
> for the work put in to the various weird and wonderful modules you  
> have pushed out.  
> The modules open up a huge amount of options and really take Nginx to  
> another level.

Glad you like it! And thank YOU for reporting all these issues during  
your adventure 😉

Thanks!  
-agentzh

---

<div class="post-metadata">

**Author:** ![Nginx\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@Nginx\_U](https://www.ruby-forum.com/u/Nginx_U)\
**Post date:** [October 16, 2011, 5:43pm UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/48 "2011-10-16T17:43:58Z")

</div>

On 16 October 2011 14:11, agentzh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> end
> 
> ```
> 
> been in any formal releases of my modules 🙂  
> No problem and to be expected … which is why I normally don’t  
> install the rc versions and why I am still at 0.3.1rc11 which works  
> just fine for me.  
> I’ll stay with 0.3.1rc11 and swing back to the formal release cycle  
> down the line.  
> I’ll also keep the rewrite module as my default tool (A series of one  
> line “rewrite a b last” calls is easier to deal with) and use  
> rewrite\_by\_lua when I need more flexibility … taking care as  
> required.

It is nice to have the option.

Cheers.

---

<div class="post-metadata">

**Author:** ![Nginx\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@Nginx\_U](https://www.ruby-forum.com/u/Nginx_U)\
**Post date:** [October 16, 2011, 1:11pm UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/49 "2011-10-16T13:11:48Z")

</div>

On Sun, Oct 16, 2011 at 12:29 PM, agentzh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> can be rewritten as
> 
> location @pretty\_urls {  
> rewrite\_by\_lua ’  
> if ngx.var.uri == “/sometext/xyz.html” then  
> return ngx.req.set\_uri(“/abc.html”)  
> end  
> ngx.exec(“@proxy”);  
> ';  
> }

Sorry, I’ve changed my mind and changed the ngx.req.set\_uri() API a  
bit in ngx\_lua v0.3.1rc14. Now it’s required to trigger location jump  
explicitly and the above example should be rewritten as

```
location @pretty_urls {
    rewrite_by_lua '
        if ngx.var.uri == "/sometext/xyz.html" then
            -- the following function never returns
            -- when the second ("jump") arg is true
            ngx.req.set_uri("/abc.html", true)
        end
        ngx.exec("@proxy");
    ';
}

```

> See the documentation here for details:  
> [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpLuaModule#ngx.req.set_uri)

I’ve also updated the document accordingly. Sorry for the  
inconvenience but I’d preserve the right to change APIs that haven’t  
been in any formal releases of my modules 🙂

Thanks!  
-agentzh

P.S. ngx\_lua v0.3.1rc14 has already been included in ngx\_openresty  
devel release 1.0.8.11: [OpenResty® - High-Concurrency Web Platform Built on Nginx + LuaJIT](http://openresty.org/#Download)

[Previous page](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716.md?page=2)
