# Lua pcre inside nginx

**URL:** <https://www.ruby-forum.com/t/lua-pcre-inside-nginx/206562>\
**Category:** NGINX\
**Created:** [May 4, 2011, 9:04pm UTC](https://www.ruby-forum.com/t/lua-pcre-inside-nginx/206562 "2011-05-04T21:04:20Z")\
**Posts on this page:** 6\
**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:** [May 4, 2011, 9:04pm UTC](https://www.ruby-forum.com/t/lua-pcre-inside-nginx/206562/1 "2011-05-04T21:04:20Z")

</div>

I wanted to use pcre inside a lua script using rex\_pcre. However, I  
always  
get the error:

failed to get memory (pattern offset: 15)

The snippet of code always works in a standalone script.

Ideas?

–  
Brian A.

---

<div class="post-metadata">

**Author:** ![AkinsS\_Brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d2c977/32.png) [@AkinsS\_Brian](https://www.ruby-forum.com/u/AkinsS_Brian)\
**Post date:** [May 4, 2011, 11:37pm UTC](https://www.ruby-forum.com/t/lua-pcre-inside-nginx/206562/2 "2011-05-04T23:37:41Z")

</div>

Hi,

On 04/05/2011 22:03, Akins, Brian wrote:

> I wanted to use pcre inside a lua script using rex\_pcre. However, I always  
> get the error:
> 
> failed to get memory (pattern offset: 15)
> 
> The snippet of code always works in a standalone script.
> 
> Ideas?  
> I just tried the supplied test suite for rex\_pcre on the latest (as of a  
> few days ago) trunk version of LuaJIT 2.0 on Nginx 1.0.0 and there were  
> no test failures. Maybe try JIT if you’re not using it already?

Cheers,

Marcus.

---

<div class="post-metadata">

**Author:** ![AkinsS\_Brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d2c977/32.png) [@AkinsS\_Brian](https://www.ruby-forum.com/u/AkinsS_Brian)\
**Post date:** [May 4, 2011, 11:40pm UTC](https://www.ruby-forum.com/t/lua-pcre-inside-nginx/206562/3 "2011-05-04T23:40:09Z")

</div>

On 5/4/11 5:37 PM, “Eugaia” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I just tried the supplied test suite for rex\_pcre on the latest (as of a  
> few days ago) trunk version of LuaJIT 2.0 on Nginx 1.0.0 and there were  
> no test failures. Maybe try JIT if you’re not using it already?

I already am using luajit. I was using rex\_pcre from luarocks.

–  
Brian A.

---

<div class="post-metadata">

**Author:** ![AkinsS\_Brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d2c977/32.png) [@AkinsS\_Brian](https://www.ruby-forum.com/u/AkinsS_Brian)\
**Post date:** [May 5, 2011, 12:11am UTC](https://www.ruby-forum.com/t/lua-pcre-inside-nginx/206562/4 "2011-05-05T00:11:36Z")

</div>

On 5/4/11 5:39 PM, “Brian A.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I already am using luajit. I was using rex\_pcre from luarocks.

I grabbed latest rex\_pcre, same issue. This is x86\_64 if that matters.

–  
Brian A.

---

<div class="post-metadata">

**Author:** ![AkinsS\_Brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d2c977/32.png) [@AkinsS\_Brian](https://www.ruby-forum.com/u/AkinsS_Brian)\
**Post date:** [May 6, 2011, 8:47pm UTC](https://www.ruby-forum.com/t/lua-pcre-inside-nginx/206562/5 "2011-05-06T20:47:14Z")

</div>

On 5/4/11 11:36 PM, “Chaos Wang” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ngx\_lua has fixed this problem, and can use pcre related extensions  
> normally after v0.1.6rc5.

Thanks. Seems to work now.

–  
Brian A.

---

<div class="post-metadata">

**Author:** ![AkinsS\_Brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d2c977/32.png) [@AkinsS\_Brian](https://www.ruby-forum.com/u/AkinsS_Brian)\
**Post date:** [May 5, 2011, 5:37am UTC](https://www.ruby-forum.com/t/lua-pcre-inside-nginx/206562/6 "2011-05-05T05:37:32Z")

</div>

NginX hooked pcre memory alloc/free functions pcre\_malloc/free to use  
its memory pool, in order to work around its memory leaking problem. As  
a side effect, it reset this pool to NULL after every usage in core  
without considering possible external references. If you don’t set pcre  
memory hooks to your own implementation, you will surely get the error.

ngx\_lua has fixed this problem, and can use pcre related extensions  
normally after v0.1.6rc5.

Ref:

- src/core/ngx\_regex.c in NginX sources
- ngx\_http\_lua\_patch.c in ngx\_lua sources
