# 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:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [October 11, 2011, 6:04pm UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/1 "2011-10-11T18:04:04Z")

</div>

I have the following simplified setup …

server {  
…  
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/ @proxy;  
}  
}  
Basically, everything that cannot be found by nginx, as well as php  
requests, are sent to the proxy

Now, note the filter.default file in the @proxy location. I use this  
to run some tests on these requests for security and my logs show them  
catching all sorts of exploit attempts.

Anyway, when I have the following (simplified) in firewall.default …

if ($http\_user\_agent ~\* libwww ) {  
return 403;  
}

… everything is fine. When a php request is made, libwww user agents  
are denied and others get the php output.

When I use the following (simplified) rewrite\_by\_lua equivalent instead  
…

rewrite\_by\_lua ’  
if ngx.var.http\_user\_agent == “libwww” then  
ngx.exit(ngx.HTTP\_FORBIDDEN)  
end  
';

The php file is downloaded. Obviously I don’t have the “libwww” when  
testing so I suppose the lua “if” block is skipped at which point the  
physical php file is found and sent to the user as is and the  
proxy\_pass directive is not run.

Looks similar to the sort of unexpected behaviour from the rewrite  
module’s “if”.

Any ideas what gives? Why isn’t rewrite\_by\_lua behaving like the rewrite  
module?

Thanks

---

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

</div>

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

> Anyway, when I have the following (simplified) in firewall.default …
> 
> if ($http\_user\_agent ~\* libwww ) {  
> return 403;  
> }
> 
> … everything is fine. When a php request is made, libwww user agents  
> are denied and others get the php output.

You’re using the “~\*” operator here and that means “case insensitive  
match”, see [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpCoreModule#location)

> When I use the following (simplified) rewrite\_by\_lua equivalent instead …
> 
> rewrite\_by\_lua ’  
> if ngx.var.http\_user\_agent == “libwww” then  
> ngx.exit(ngx.HTTP\_FORBIDDEN)  
> end  
> ';

Note that you’re using “==” here in Lua which is exact string comparison  
🙂

> The php file is downloaded. Obviously I don’t have the “libwww” when  
> testing so I suppose the lua “if” block is skipped at which point the  
> physical php file is found and sent to the user as is and the  
> proxy\_pass directive is not run.

Which version of ngx\_lua are you using? Please show me your “nginx -V”  
output? And which OS are you using? I’ve tested your example with  
ngx\_lua git master HEAD on Slackware Linux x86\_64 and do not have any  
issues 🙂

Also, enabling --with-debug in your nginx build and show me the  
relevant sections of your error.log on the debug error log level will  
be helpful too 🙂

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

</div>

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

> > 
> 
> Note that you’re using “==” here in Lua which is exact string comparison 🙂

It is a simplified config. I actually run lua’s string.find first and  
test for a hit.  
I know ngx.re.match with the “i” modifier would be better but it does  
not work … maybe I need to update lua module version. I thought I’ll  
look into that later.  
Anyway, the exact implementation will cause the “if” block to be  
skipped.

> > The php file is downloaded. Obviously I don’t have the “libwww” when  
> > testing so I suppose the lua “if” block is skipped at which point the  
> > physical php file is found and sent to the user as is and the  
> > proxy\_pass directive is not run.
> 
> Which version of ngx\_lua are you using? Please show me your “nginx -V”  
> output? And which OS are you using? I’ve tested your example with  
> ngx\_lua git master HEAD on Slackware Linux x86\_64 and do not have any  
> issues 🙂

Centos 5.7 i386.  
lua module is 0.2.0

nginx: nginx version: nginx/1.0.6  
nginx: built by gcc 4.1.2 20080704 (Red Hat 4.1.2-50)  
nginx: TLS SNI support disabled  
nginx: configure arguments: --user=nginx --group=nginx  
–prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx  
–conf-path=/etc/nginx/nginx.conf  
–error-log-path=/var/log/nginx/error.log  
–http-log-path=/var/log/nginx/access.log  
–http-client-body-temp-path=/var/lib/nginx/tmp/client\_body  
–http-proxy-temp-path=/var/lib/nginx/tmp/proxy  
–http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi  
–pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx  
–with-http\_ssl\_module --with-http\_realip\_module  
–with-http\_stub\_status\_module --with-http\_perl\_module --with-mail  
–with-mail\_ssl\_module --with-cc-opt=‘-O2 -g -pipe -Wall  
-Wp,-D\_FORTIFY\_SOURCE=2 -fexceptions -fstack-protector  
–param=ssp-buffer-size=4 -m32 -march=i386 -mtune=generic  
-fasynchronous-unwind-tables’  
–add-module=/usr/src/redhat/BUILD/nginx-1.0.6/nginx-upstream-fair  
–add-module=/usr/src/redhat/BUILD/nginx-1.0.6/ngx\_cache\_purge-1.3  
–add-module=/usr/src/redhat/BUILD/nginx-1.0.6/ngx-headers-more  
–add-module=/usr/src/redhat/BUILD/nginx-1.0.6/ngx-devel-kit  
–add-module=/usr/src/redhat/BUILD/nginx-1.0.6/ngx-echo-module  
–add-module=/usr/src/redhat/BUILD/nginx-1.0.6/ngx-lua-module  
–add-module=/usr/src/redhat/BUILD/nginx-1.0.6/ngx\_auth\_request-module  
–without-http\_autoindex\_module --without-http\_empty\_gif\_module  
–without-http\_memcached\_module --without-http\_scgi\_module  
–without-http\_split\_clients\_module --without-http\_ssi\_module  
–without-http\_upstream\_ip\_hash\_module --without-http\_uwsgi\_module

> Also, enabling --with-debug in your nginx build and show me the  
> relevant sections of your error.log on the debug error log level will  
> be helpful too 🙂

I’ll get this later.

Thanks

---

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

</div>

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

> It is a simplified config. I actually run lua’s string.find first and  
> test for a hit.  
> I know ngx.re.match with the “i” modifier would be better but it does  
> not work

The ngx.re API was first introduced in the v0.2.1rc11 release. There’s  
no wonder that it does not work for you because you’re using v0.2.0 🙂

> … maybe I need to update lua module version. I thought I’ll  
> look into that later.

Surely you need. Please do that before other attempts 🙂

> > Which version of ngx\_lua are you using? Please show me your “nginx -V”  
> > output? And which OS are you using? I’ve tested your example with  
> > ngx\_lua git master HEAD on Slackware Linux x86\_64 and do not have any  
> > issues 🙂
> 
> Centos 5.7 i386.  
> lua module is 0.2.0

Sigh, your ngx\_lua module is way too old 🙂

There’s a bunch of important fixes regarding ngx.exec() and ngx.exit()  
since the ngx\_lua v0.2.0 release.

The latest version is v0.3.1rc9 and please try it out.

> > Also, enabling --with-debug in your nginx build and show me the  
> > relevant sections of your error.log on the debug error log level will  
> > be helpful too 🙂
> 
> I’ll get this later.

Please upgrade your ngx\_lua module to the latest version before doing  
this 🙂

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

</div>

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

> Sigh, your ngx\_lua module is way too old 🙂
> 
> There’s a bunch of important fixes regarding ngx.exec() and ngx.exit()  
> since the ngx\_lua v0.2.0 release.
> 
> The latest version is v0.3.1rc9 and please try it out.  
> OK thanks. I need to do a build for Nginx 1.0.8 and update my 3rd  
> party modules with new nginx builds.  
> I don’t install the rc versions of 3rd party modules though. I only  
> use full releases which means 0.3.0 in this case … expect if 0.3.1  
> is released in the interim of course.

Anyway, nice to know ngx\_lua is not “evil” (as long as it is 0.2.1+!)

Thanks

---

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

</div>

try open this link…i hope is usefull to you  
[www.anilcetin.com/convert-apache-htaccess-to-nginx/](http://www.anilcetin.com/convert-apache-htaccess-to-nginx/)

Posted at Nginx Forum:

> **[Re: So is "rewrite\_by\_lua" also evil?](https://forum.nginx.org/read.php?2,216525,216581#msg-216581)**
>
> I have the following simplified setup ... server { ... location @proxy { include /etc/nginx/firewall.default; proxy\_pass http://127.0.0.1:8080; ... } location ~ ^.+\\.php$ { content\_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 12, 2011, 12:32pm UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/7 "2011-10-12T12:32:09Z")

</div>

On 12 October 2011 13:23, MyName [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> try open this link..i hope is usefull to you  
> [www.anilcetin.com/convert-apache-htaccess-to-nginx/](http://www.anilcetin.com/convert-apache-htaccess-to-nginx/)  
> I don’t need to convert Apache htaccess to Nginx but thanks for the link  
> anyway.

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

</div>

On 12 October 2011 21:35, Nginx U. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> if method == nil then  
> See debug output: [http://pastebin.com/A7LNeCf0](http://pastebin.com/A7LNeCf0)  
> Some additional info.

I had thought the php files were being downloaded as text files but  
this is not strictly true. Files are downloaded whenever php is  
called but they are always empty with a “0” status code.

The associated access log output for the request in the debug log in  
the paste bin is: xx.xxx.xx.xx - - [12/Oct/2011:18:20:50 +0000] “GET  
/help/ HTTP/1.1” 0 0 “[http://testsite/index.php](http://testsite/index.php)” “Mozilla/5.0  
(Macintosh; Intel Mac OS X 10\_5\_8) AppleWebKit/534.50.2 (KHTML, like  
Gecko) Version/5.0.6 Safari/533.22.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 13, 2011, 3:35am UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/9 "2011-10-13T03:35:12Z")

</div>

On Thu, Oct 13, 2011 at 2:35 AM, Nginx U. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> if method == nil then
> method = ngx.re.match(ngx.var.request_method, "HEAD", "i")
> if method == nil then
> ngx.exit(ngx.HTTP_BAD_REQUEST)
> end
> end
> 
> ```
> 
> end  
> ';

BTW, you could have written the Lua code like this:

local m = ngx.re.match(ngx.var.request\_method, “GET|POST|HEAD”, “io”)  
if not m then  
ngx.exit(400)  
end

The ngx.re.match uses the same PCRE engine as ngx\_rewrite’s “if”  
anyway. Also, because the regex argument is a constant here, the “o”  
flag can be specified to cache the compiled regex object on the global  
level.

> See debug output: [http://pastebin.com/A7LNeCf0](http://pastebin.com/A7LNeCf0)

The debug log data you’ve provided contains a lot of references to the  
location @pretty\_urls which is missing from your original nginx.conf  
snippet. Could you please provide a minimized but complete nginx.conf  
that can reproduce this problem as well as the corresponding debug.log  
snippet?

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

</div>

On Thu, Oct 13, 2011 at 5:34 AM, Nginx U. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > See debug output: [http://pastebin.com/A7LNeCf0](http://pastebin.com/A7LNeCf0)
> 
> Some additional info.
> 
> I had thought the php files were being downloaded as text files but  
> this is not strictly true. Files are downloaded whenever php is  
> called but they are always empty with a “0” status code.

This information is invaluable 🙂 I’ve already reproduced this bug of  
ngx\_lua on my side and fixed it in my local tree.

Basically, calling ngx.exec to jump to a named location is buggy in  
that ngx\_http\_named\_location() in the nginx core does not clear module  
ctx structures as ngx\_http\_internal\_redirect() so ngx\_lua should have  
cleared its ctx itself.

I’ll tag another rc release of ngx\_lua for you to try when I finish  
running the whole test suite 🙂

Thank you very much for the bug report! Hopefully it’ll be no longer  
evil 😉

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 13, 2011, 5:58am UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/11 "2011-10-13T05:58:01Z")

</div>

On Thu, Oct 13, 2011 at 10:06 AM, agentzh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

Please try out the latest ngx\_lua v0.3.1rc10 release:

```
https://github.com/chaoslawful/lua-nginx-module/tags

```

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

</div>

Hi.

Updated Nginx to 1.0.8 and did my 3rd party modules as well and even  
went for rc versions

nginx: nginx version: nginx/1.0.8  
nginx: built by gcc 4.1.2 20080704 (Red Hat 4.1.2-51)  
nginx: configure arguments: --user=nginx --group=nginx  
–prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx  
–conf-path=/etc/nginx/nginx.conf  
–error-log-path=/var/log/nginx/error.log  
–http-log-path=/var/log/nginx/access.log  
–http-client-body-temp-path=/var/lib/nginx/tmp/client\_body  
–http-proxy-temp-path=/var/lib/nginx/tmp/proxy  
–http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi  
–pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx  
–with-http\_stub\_status\_module --with-http\_perl\_module --with-debug  
–without-http\_memcached\_module --without-http\_scgi\_module  
–without-http\_split\_clients\_module --without-http\_ssi\_module  
–without-http\_upstream\_ip\_hash\_module --without-http\_uwsgi\_module  
–add-module=/usr/src/redhat/BUILD/nginx-1.0.8/nginx-upstream-fair  
–add-module=/usr/src/redhat/BUILD/nginx-1.0.8/ngx\_cache\_purge-1.4  
–add-module=/usr/src/redhat/BUILD/nginx-1.0.8/ngx-headers-more-0.16rc3-0  
–add-module=/usr/src/redhat/BUILD/nginx-1.0.8/ngx-devel-kit-0.2.17-4  
–add-module=/usr/src/redhat/BUILD/nginx-1.0.8/ngx-echo-module-0.37rc5-0  
–add-module=/usr/src/redhat/BUILD/nginx-1.0.8/ngx-lua-module-0.31rc9-0  
–add-module=/usr/src/redhat/BUILD/nginx-1.0.8/ngx\_auth\_request-module  
–with-cc-opt=‘-O2 -g -pipe -Wall -Wp,-D\_FORTIFY\_SOURCE=2 -fexceptions  
-fstack-protector --param=ssp-buffer-size=4 -m32 -march=i386  
-mtune=generic -fasynchronous-unwind-tables’

Still seeing some evil type behaviour though.

This renders php as expected…

if ($request\_method !~\* (GET|HEAD|POST) ) {  
return 400;  
}

… while this sends php files as text downloads …

rewrite\_by\_lua ’  
method = ngx.re.match(ngx.var.request\_method, “GET”, “i”)  
if method == nil then  
method = ngx.re.match(ngx.var.request\_method, “POST”, “i”)  
if method == nil then  
method = ngx.re.match(ngx.var.request\_method, “HEAD”, “i”)  
if method == nil then  
ngx.exit(ngx.HTTP\_BAD\_REQUEST)  
end  
end  
end  
';

See debug output: [http://pastebin.com/A7LNeCf0](http://pastebin.com/A7LNeCf0)

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

</div>

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

> The debug log data you’ve provided contains a lot of references to the  
> location @pretty\_urls which is missing from your original nginx.conf  
> snippet. Could you please provide a minimized but complete nginx.conf  
> that can reproduce this problem as well as the corresponding debug.log  
> snippet?

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;  
}  
}

If request ends in “.php” it is sent to @proxy by php location. If not  
“.php”, Nginx tries to find and serve resource. If not found, sent to  
@pretty\_urls where it will either be rewritten to “.php” or a 404  
error returned.

---

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

</div>

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

> Basically, calling ngx.exec to jump to a named location is buggy in  
> that ngx\_http\_named\_location() in the nginx core does not clear module  
> ctx structures as ngx\_http\_internal\_redirect() so ngx\_lua should have  
> cleared its ctx itself.  
> I see. Don’t know much about this but does this mean the httpRewrite  
> module does clear this ctx? I.E. why doesn’t this affect the standard  
> rewrite module?
> 
> I’ll tag another rc release of ngx\_lua for you to try when I finish  
> running the whole test suite 🙂
> 
> Thank you very much for the bug report! Hopefully it’ll be no longer evil 😉  
> Thanks are to you.

---

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

</div>

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

> ```
> # some rewrite rules to php
> try_files $uri $uri/ @pretty_urls;
> 
> ```
> 
> }  
> }
> 
> If request ends in “.php” it is sent to @proxy by php location. If not  
> “.php”, Nginx tries to find and serve resource. If not found, sent to  
> @pretty\_urls where it will either be rewritten to “.php” or a 404  
> error returned.

PS: I had earlier thought the issue might be with the content\_by\_lua  
‘ngx.exec(“@proxy”);’; line but when I tried to use the following …

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

```

… which I thought should work and seems more appropriate, I got an  
error equivalent to issuing “return 444”.

---

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

</div>

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

> Please try out the latest ngx\_lua v0.3.1rc10 release:  
> I’ll have a bash later!

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 13, 2011, 6:56am UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/17 "2011-10-13T06:56:00Z")

</div>

On Thu, Oct 13, 2011 at 12:32 PM, Nginx U. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On 13 October 2011 05:06, agentzh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Basically, calling ngx.exec to jump to a named location is buggy in  
> > that ngx\_http\_named\_location() in the nginx core does not clear module  
> > ctx structures as ngx\_http\_internal\_redirect() so ngx\_lua should have  
> > cleared its ctx itself.  
> > I see. Don’t know much about this but does this mean the httpRewrite  
> > module does clear this ctx? I.E. why doesn’t this affect the standard  
> > rewrite module?

For ngx\_lua, for example, it does have a flag field named  
“entered\_content\_phase” in its context object (ctx) which marks  
whether the current request has entered the content phase. So when  
doing an internal redirection to a named location, this field must be  
cleared out or some bad things will happen in rewrite\_by\_lua because  
ngx\_lua now believes it’s in a content\_by\_lua not rewrite\_by\_lua.

Well, there’re more flags like this in ngx\_lua ctx. For ngx\_rewrite,  
it’s probably safe just because of its simplicity.

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.

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 13, 2011, 6:10pm UTC](https://www.ruby-forum.com/t/so-is-rewrite-by-lua-also-evil/212716/18 "2011-10-13T18:10:57Z")

</div>

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.

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

</div>

On Thu, Oct 13, 2011 at 12:52 PM, Nginx U. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 

This did not work due to exactly the same issue with ngx.exec + named  
locations. Please try the latest v0.3.1rc10 release mentioned earlier.  
It should also work now 🙂

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

</div>

Hi,

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.

Probably has to do with named locations as you mentioned earlier but I  
prefer to use these so I have rolled back to my back to my 1.0.6  
install so as to get back my ngx\_rewrite based setup which works fine  
with this.

Will sort out upgrading and further tests later.

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