Skip to content

Commit b81c1f1

Browse files
authored
Merge pull request #20 from rkaiser0324/doc/debugging-lua
doc: debugging instructions and an installation check
2 parents 06080c2 + bd2cf6d commit b81c1f1

File tree

1 file changed

+73
-1
lines changed

1 file changed

+73
-1
lines changed

README.md

+73-1
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,20 @@ Physical memory usage of this plugin is insanely low, under 10 KB for each nginx
4444

4545
## Installation
4646

47-
1. Install nginx *with the [Lua module](https://github.com/openresty/lua-nginx-module)* `libnginx-mod-http-lua` and `luajit`.
47+
1. Install nginx with the [Lua module](https://github.com/openresty/lua-nginx-module) `libnginx-mod-http-lua` and the `luajit` package.
4848
1. Debian: `apt-get install nginx libnginx-mod-http-lua luajit`
4949
2. Fedora: `yum install nginx libnginx-mod-http-lua luajit`
50+
51+
1. Verify that the Lua module is properly installed by running:
52+
```bash
53+
nginx -V 2>&1 | tr ' ' '\n' | grep lua
54+
```
55+
It should display something similar to this:
56+
```bash
57+
--add-module=/lua-nginx-module-a318d250f547c854ea2b091d0e06372ac0c00abc
58+
--add-module=/lua-upstream-nginx-module-0.07
59+
--add-module=/stream-lua-nginx-module-9ce0848cff7c3c5eb0a7d5adfe2de22ea98e1abc
60+
```
5061
2. Build and install the plugin into an appropriate directory accessible by the nginx process, e.g.,
5162
```bash
5263
luajit -b htaccess.lua /etc/nginx/lua/htaccess.lbc
@@ -310,3 +321,64 @@ Variables not listed below are not supported.
310321
...
311322
}
312323
```
324+
325+
326+
## Debugging Lua inside a Docker container
327+
328+
Using IntelliJ IDEA, you can remotely debug Lua scripts like `htaccess.lua` running in an nginx Docker container, using [these steps](https://dev.to/omervk/debugging-lua-inside-openresty-inside-docker-with-intellij-idea-2h95). In particular, this has been tested on a Windows 10 host running IntelliJ IDEA 2022.2.4 (Community Edition), with the `fabiocicerchia/nginx-lua:1.23.2-almalinux8.7-20221201` Docker image from https://hub.docker.com/r/fabiocicerchia/nginx-lua.
329+
330+
It assumes you are mapping a host path of `C:\path\to\project\on\windows` to a path in the container volume of `/path/to/project`.
331+
332+
1. Install [IntelliJ IDEA](https://www.jetbrains.com/idea/download/#section=windows)
333+
1. The container needs to forward port 9966 to allow for Lua debugging. If you are using `docker-compose.yml` it will look something like this:
334+
```yml
335+
version: '2.4'
336+
services:
337+
nginx-lua:
338+
build:
339+
context: .
340+
container_name: nginx-lua
341+
ports:
342+
# For nginx requests over HTTP
343+
- 80:80
344+
# If you support nginx requests over HTTPS
345+
- 443:443
346+
# For Lua debugging
347+
- 9966:9966
348+
volumes:
349+
- ./relative/path/to/project/on/windows/:/path/to/project/
350+
```
351+
1. In the `Dockerfile` for the container, run the following command to build the EmmyLuaDebugger from source:
352+
```bash
353+
RUN dnf install -y cmake && \
354+
curl https://github.com/EmmyLua/EmmyLuaDebugger/archive/refs/tags/1.0.16.tar.gz \
355+
-L -o EmmyLuaDebugger-1.0.16.tar.gz && \
356+
tar -xzvf EmmyLuaDebugger-1.0.16.tar.gz && \
357+
cd EmmyLuaDebugger-1.0.16 && \
358+
mkdir -p build && \
359+
cd build && \
360+
cmake -DCMAKE_BUILD_TYPE=Release ../ && \
361+
make install && \
362+
mkdir -p /usr/local/emmy && \
363+
cp install/bin/emmy_core.so /usr/local/emmy/ && \
364+
cd .. && \
365+
cd .. && \
366+
rm -rf EmmyLuaDebugger-1.0.16 EmmyLuaDebugger-1.0.16.tar.gz
367+
```
368+
1. Start the container
369+
1. At the top of your Lua script to debug, add the following:
370+
```lua
371+
_G.emmy = {}
372+
_G.emmy.fixPath = function(path)
373+
return string.gsub(path, '/path/to/project/', 'C:/path/to/project/on/windows')
374+
end
375+
376+
package.cpath = package.cpath .. ';/usr/local/emmy/?.so'
377+
local dbg = require('emmy_core')
378+
dbg.tcpListen('localhost', 9966)
379+
dbg.waitIDE()
380+
dbg.breakHere()
381+
```
382+
1. In IDEA, create a Run/Debug configuration per the link and then start the debugger
383+
384+
When you request a URL that triggers the Lua script, it will pause on the `dbg.breakHere()` line so you can step through the code, watch variables, etc.

0 commit comments

Comments
 (0)