# 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:** 20\
**Page:** 2

<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 14, 2011, 2:37am UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/21 "2011-10-14T02:37:00Z")

</div>

On Fri, Oct 14, 2011 at 12:10 AM, Nginx U. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Rebuilt with 0.31rc11. Also noticed note about, and implemented,  
> module loading order.  
> Everything seems straight as an arrow and no evil going ons detected so far.

Great!

But please note that this is really a bug in the nginx core, so what  
I’m doing in ngx\_lua’s ngx.exec() (as well as ngx\_echo’s echo\_exec) is  
just a work-around. If you use other module to issue the jump to a  
named location, bad things could still happen (or in your words, evil  
stuffs). For example, ngx\_upload’s upload\_pass directive or the core  
module’s post\_action directive.

I’ve already posted a patch for the nginx core to the nginx-devel  
mailing list, which fixes the issue completely:

```
http://mailman.nginx.org/pipermail/nginx-devel/2011-October/001327.html

```

That patch can also be applied cleanly to nginx 1.0.8 (at least).

BTW, this patch has already been incorporated into the ngx\_openresty  
1.0.8.5 devel release: [OpenResty® - High-Concurrency Web Platform Built on Nginx + LuaJIT](http://openresty.org/#Download)

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 14, 2011, 10:31am UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/22 "2011-10-14T10:31:50Z")

</div>

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

> just a work-around. If you use other module to issue the jump to a  
> named location, bad things could still happen (or in your words, evil  
> stuffs). For example, ngx\_upload’s upload\_pass directive or the core  
> module’s post\_action directive.
> 
> I’ve already posted a patch for the nginx core to the nginx-devel  
> mailing list, which fixes the issue completely:
> 
> [[PATCH] clear modules' contexts in ngx\_http\_named\_location](http://mailman.nginx.org/pipermail/nginx-devel/2011-October/001327.html)
> 
> That patch can also be applied cleanly to nginx 1.0.8 (at least).

Strange, looking at the message from Maxim to Igor, that this hadn’t  
been changed before.  
If you refer to the message I sent just before yours, you will find I  
am still seeing this behaviour for some requests and that I rolled  
everything back to my last known working config.

Perhaps the work around need to go into some other one of the modules I  
use.

I’ll look into applying the patch to the 1.0.8 core directly and retry  
later.

> BTW, this patch has already been incorporated into the ngx\_openresty  
> 1.0.8.5 devel release: [OpenResty® - High-Concurrency Web Platform Built on Nginx + LuaJIT](http://openresty.org/#Download)

Nice.

I will be building openresty rpms for redhat presently. Wanted to try  
some of the elements first.

---

<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 14, 2011, 1:41pm UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/23 "2011-10-14T13:41:41Z")

</div>

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

> break the code. I would say it should probably just be left up to the  
> modules to reset their own contexts if they need to (as is the case now), or  
> to just define multiple modules which would use different contexts anyway.

That throws a different spot light on things.

I don’t know about the guts of Nginx but I suppose the main point  
would be for things to work consistently. I.E. if this context is  
cleared in some places in the core (normal locations), then, it should  
be cleared in all similar places (all locations) except of course if  
there is specific good reason not to. Module developers should have a  
clear understanding of this “feature”.

Think I will go into holding mode with the standard rewrite module  
(which works without issue) for now.  
Just that lua allows for more complex logic for my tests.

Thanks for your efforts chaps.

---

<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 14, 2011, 3:28pm UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/24 "2011-10-14T15:28:05Z")

</div>

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

> Unfortunately, I am still seeing the “0 0” outputs in my logs on (only  
> some) requests. I had noticed that some pages just seemed to have the  
> “loading” wheel spin for ever and on looking, I found the instances.
> 
> It is not affecting standard php requests anymore from what I can see  
> but it seems some Ajax type requests where php is being used for  
> dynamic js (! need to look closer into how I set those up) and with  
> phpMyAdmin which is aliased somewhere.  
> Looking at things further, it may be a mime type issue clouding things.  
> My js files use the non-standard but widely adopted “text/javascript”  
> while Nginx comes with the standard but not always supported  
> “application/X-javascript” mime type.  
> Not sure I changed the mimes types file accordingly when I went to  
> 1.0.8.

The phpMyadmin one is funny though as all it does is use an alias.

---

<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 14, 2011, 9:25pm UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/25 "2011-10-14T21:25:27Z")

</div>

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

> Looking at things further, it may be a mime type issue clouding things.  
> My js files use the non-standard but widely adopted “text/javascript”  
> while Nginx comes with the standard but not always supported  
> “application/X-javascript” mime type.  
> Not sure I changed the mimes types file accordingly when I went to 1.0.8.
> 
> The phpMyadmin one is funny though as all it does is use an alias.

OK. I went back to nginx1.0.8 and 0.31rc11, made sure everything was  
proper and made sure I only used rewrite by lua to trigger the jump

server {  
…  
location @pretty\_urls {  
# some rewrite rules to php  
}  
location @proxy {  
include /etc/nginx/firewall.default;  
proxy\_pass [http://127.0.0.1:8080](http://127.0.0.1:8080);  
…  
}  
location ~ ^.+.php$ {  
content\_by\_lua ‘ngx.exec(“@proxy”);’;  
}  
location / {  
try\_files $uri $uri/ @pretty\_urls;  
}  
}

Everything works perfectly and not a hint of evil behaviour. Not sure  
what I missed before but I am now as happy as Larry.

Now I can implement the second stage of my WAF implementation plan  
where firewall.default would query PHPIDS ([https://phpids.org/](https://phpids.org/))  
instead of my homebrew regexes.

---

<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 14, 2011, 9:26pm UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/26 "2011-10-14T21:26:04Z")

</div>

On 14 October 2011 22:24, Nginx U. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> location ~ ^.+.php$ {  
> content\_by\_lua ‘ngx.exec(“@proxy”);’;  
> }  
> That should read

```
  location ~ ^.+\.php$ {
          rewrite_by_lua 'ngx.exec("@proxy");';
  }

```

---

<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 14, 2011, 1:02pm UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/27 "2011-10-14T13:02:07Z")

</div>

On 13/10/2011 07:55, agentzh wrote:

> Well, I still believe it is a bug in ngx\_http\_named\_location function  
> in the nginx core. The thumb of rule is to avoid using named locations  
> like @proxy but use normal locations configured with the “internal”  
> directive. And we’ve been keeping doing this in our production apps.  
> In some situations it’s probably the desired effect to reset the  
> contexts, but not all. There are multiple situations in modules I’ve  
> developed / am developing where resetting contexts would be highly  
> undesirable and would break the code. I would say it should probably  
> just be left up to the modules to reset their own contexts if they need  
> to (as is the case now), or to just define multiple modules which would  
> use different contexts anyway.

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 14, 2011, 10:41pm UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/28 "2011-10-14T22:41:34Z")

</div>

Additional feedback to agentzh:

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.

Contrived example:

# GET /sometext/xyz.html

server {  
…  
location @pretty\_urls {  
rewrite ^/sometext/xyz.html$ /abc.html last;

```
          # line below included by mistake and shouldn't have been 

```

there  
# On the other hand, the hit above should mean it is not  
called anyway  
rewrite\_by\_lua ‘ngx.exec(“@proxy”);’;  
}  
location @proxy {  
include /etc/nginx/firewall.default;  
proxy\_pass [http://127.0.0.1:8080](http://127.0.0.1:8080);  
…  
}  
location ~ ^.+.php$ {  
content\_by\_lua ‘ngx.exec(“@proxy”);’;  
}  
location / {  
try\_files $uri $uri/ @pretty\_urls;  
}  
}

… generates an error along the lines of …

2011/10/14 20:10:26 [error] 17325#0: \*16 lua handler aborted: runtime  
error: attempt to call a nil value, blah, blah, blah".

As said, it was a misconfig but I would have expected that with the  
earlier hit with the rewrite module, all further rewrites, including  
ngx\_lua’s should stop.

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 15, 2011, 3:27am UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/29 "2011-10-15T03:27:59Z")

</div>

On Fri, Oct 14, 2011 at 8:16 AM, Nginx U. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Unfortunately, I am still seeing the “0 0” outputs in my logs on (only  
> some) requests. I had noticed that some pages just seemed to have the  
> “loading” wheel spin for ever and on looking, I found the instances.

Would you mind sharing the debug logs for such bad responses? I’d have  
a closer look at these 🙂

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 15, 2011, 10:34am UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/30 "2011-10-15T10:34:05Z")

</div>

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

> On Fri, Oct 14, 2011 at 8:16 AM, Nginx U. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Unfortunately, I am still seeing the “0 0” outputs in my logs on (only  
> > some) requests. I had noticed that some pages just seemed to have the  
> > “loading” wheel spin for ever and on looking, I found the instances.
> 
> Would you mind sharing the debug logs for such bad responses? I’d have  
> a closer look at these 🙂

I stopped getting these responses. I rolled back to my 1.0.6 install  
and later redid the move the 1.0.8 with all the components more  
carefully and it stopped at that point.

---

<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, 8:36am UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/31 "2011-10-15T08:36:54Z")

</div>

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

> ```
> rewrite ^/sometext/xyz\.html$ /abc.html last;
> 
> ```
> 
> }

Interesting. But I cannot reproduce this issue with the following  
small example on my side:

```
location @pretty {
    rewrite ^/main/xyz\.html$ /abc.html last;
    rewrite_by_lua 'ngx.exec("@proxy")';
}
location @proxy {
    echo hello;
}
location /abc.html {
    echo abc;
}
location /main {
    try_files $uri $uri/ @pretty;
}

```

Then GET /main/xyz.html yields the output “abc” as expected and I  
don’t see that ngx\_lua error message in my error.log.

Could you show me the error.log with --with-debug + debug log level?

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, 11:40am UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/32 "2011-10-15T11:40:39Z")

</div>

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

> rewrite ^/(abc|forum|xyz)/?$ “/index.php?pretty;action=$1” last;  
> Okay, I must say that ngx\_rewrite’s “last” won’t affect rewrite\_by\_lua  
> because rewrite\_by\_lua is not part of that module (and there’s no  
> known way to achieve that).

So it’s recommended to do URL rewrite all in rewrite\_by\_lua. Some  
people has pointed out that ngx.exec() is a little heavy as compared  
to ngx\_rewrite’s rewrite directive. I may implement an ngx.rewrite()  
function for ngx\_lua in the near future and it’s an easy hack 🙂

But set\_by\_lua will be affected because that directive is injected  
into ngx\_rewrite’s instruction list by means of ngx\_devel\_kit’s  
set\_var submodule.

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, 10:47am UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/33 "2011-10-15T10:47:02Z")

</div>

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

> location /abc.html {  
> echo abc;  
> }  
> location /main {  
> try\_files $uri $uri/ @pretty;  
> }
> 
> Then GET /main/xyz.html yields the output “abc” as expected and I  
> don’t see that ngx\_lua error message in my error.log.
> 
> Could you show me the error.log with --with-debug + debug log level?

[http://pastebin.com/estie4W8](http://pastebin.com/estie4W8)

# GET /forum/

server {  
root /home/user/testsite.com/public\_html/test\_app  
…  
location @pretty\_urls {  
# A number of rewrite rule. Hit line replicated  
…  
rewrite ^/(abc|forum|xyz)/?$ “/index.php?pretty;action=$1” last;  
…

```
   rewrite_by_lua 'ngx.exec("@proxy");';

```

}  
location @proxy {  
include /etc/nginx/firewall.default;  
proxy\_pass [http://127.0.0.1:8080](http://127.0.0.1:8080);  
…  
}  
location ~ ^.+.php$ {  
rewrite\_by\_lua ‘ngx.exec(“@proxy”);’;  
}  
location / {  
try\_files $uri $uri/ @pretty\_urls;  
}  
}

---

<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, 11:57am UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/34 "2011-10-15T11:57:54Z")

</div>

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

> > …  
> > rewrite ^/(abc|forum|xyz)/?$ “/index.php?pretty;action=$1” last;
> 
> Okay, I must say that ngx\_rewrite’s “last” won’t affect rewrite\_by\_lua  
> because rewrite\_by\_lua is not part of that module (and there’s no  
> known way to achieve that).  
> I guessed as much. I didn’t need that line there in this case and if  
> neeed, “try\_files /dummy/ @proxy” would do the job.  
> I like ngx\_lua because it allows me to jump locations. Needs hacks to  
> do this with the rewrite module. Either the try\_files hack above or  
> mapping an obscure error code to a location and using “return code”.

All in all, I have come to realise that ngx\_lua is slightly evil in  
that unexpected things can happen. As with “if” however, evil  
understood is evil avoided 🙂

Any thoughts on Marcus’ points earlier on clearing the ctx in the core?  
Seems to suggest that may cause problems elsewhere and that it is  
better to be handled at the module level.  
Seems logical as whether it was originally an oversight or not,  
changing it now would break other stuff.

---

<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:08pm UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/35 "2011-10-15T12:08:23Z")

</div>

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

> All in all, I have come to realise that ngx\_lua is slightly evil in  
> that unexpected things can happen. As with “if” however, evil  
> understood is evil avoided 🙂

The ngx\_lua module won’t play seamlessly with ngx\_rewrite because  
ngx\_rewrite does use internal hacks and tricks to make things work. So  
I’d recommend use Lua to do as much as possible and avoid using  
ngx\_rewrite’s directives but simple “set”.

> Any thoughts on Marcus’ points earlier on clearing the ctx in the core?

Well, there’re indeed cases that we do want to retain module contexts  
during a jump but I’ve already managed to “abuse” the post\_subrequest  
mechanism to save the pointer to my module’s context and later restore  
it in my output header filter, for example. (See how I do this in my  
ngx\_lua module for ngx.location.capture.)

But on the other hand, if I do want to clear my module’s context  
before a internal redirection, then there’s no easy way to do that  
myself.

> Seems to suggest that may cause problems elsewhere and that it is  
> better to be handled at the module level.

No, this is impossible to handle that fully on the module level. For  
example, if you do use ngx\_upload module’s upload\_pass instead of  
ngx\_lua or ngx\_echo to do the jump, then it’ll still break  
rewrite\_by\_lua and access\_by\_lua (at least).

> Seems logical as whether it was originally an oversight or not,  
> 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.

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, 12:17pm UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/36 "2011-10-15T12:17:00Z")

</div>

On 15/10/2011 12:40, agentzh wrote:

> Okay, I must say that ngx\_rewrite’s “last” won’t affect rewrite\_by\_lua  
> because rewrite\_by\_lua is not part of that module (and there’s no  
> known way to achieve that).  
> You could achieve that now by creating a dummy variable, and instead of  
> using a new handler for the rewrite by code, you wrap the code around  
> the setby lua code. It would waste a few cycles, since it would be  
> setting a variable unnecessarily, but would have the benefit of not  
> slowing down the execution of code that didn’t use rewrite\_by, since it  
> wouldn’t be adding an extra handler to process for all the other  
> requests.

A better way would be to extend the NDK and expose a generic  
rewrite-phase, script engine interface that other scripting functions  
(that are more than just set\_xxx) can use, and to use that instead.  
You’d then not be wasting cycles on setting a variable unnecessarily.  
It’d be mostly similar to the set\_var stuff, but would need to take the  
normal ‘code’-type functions rather than the NDK setvar function types.

Cheers,

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, 12:34pm UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/37 "2011-10-15T12:34:16Z")

</div>

On Sat, Oct 15, 2011 at 6:16 PM, Eugaia [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> execution of code that didn’t use rewrite\_by, since it wouldn’t be adding an  
> 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?

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, 12:26pm UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/38 "2011-10-15T12:26:15Z")

</div>

On 15/10/2011 12:57, Nginx U. wrote:

> Any thoughts on Marcus’ points earlier on clearing the ctx in the core?  
> Seems to suggest that may cause problems elsewhere and that it is  
> better to be handled at the module level.  
> Seems logical as whether it was originally an oversight or not,  
> changing it now would break other stuff.  
> I had a closer look at the code, and in fact clearing the contexts  
> automatically won’t break my code in particular because I don’t use  
> ngx\_http\_named\_location() to find named locations (I find the location  
> at conf-read time, not run time, since it’s more efficient). There may  
> be other developers that use the function at runtime, but my guess is  
> that it’s probably not commonly used by module developers who would not  
> prefer the contexts to be reset.

I would therefore agree with agentzh and Maxim about it probably being  
the best way to go, and should any developers create modules that  
currently call ngx\_http\_named\_location() but don’t want to clear the  
contexts, then they can just re-write the function to not do so.

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:06pm UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/39 "2011-10-15T13:06:54Z")

</div>

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

> ngx\_http\_named\_location() to find named locations (I find the location at  
> conf-read time, not run time, since it’s more efficient). There may be  
> other developers that use the function at runtime, but my guess is that it’s  
> probably not commonly used by module developers who would not prefer the  
> contexts to be reset.
> 
> I would therefore agree with agentzh and Maxim about it probably being the  
> best way to go, and should any developers create modules that currently call  
> ngx\_http\_named\_location() but don’t want to clear the contexts, then they  
> can just re-write the function to not do so.

Great.

I’ll therefore look into applying agentzh’s patch then. I had held off  
because your previous comment.

---

<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:09pm UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/40 "2011-10-15T13:09:59Z")

</div>

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 😉

> 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 😉

Thanks!  
-agentzh

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

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