correct way to include the module
Opened this issue · 3 comments
I'm facing this problem again and again, unsure how to solve it the correct way:
#!/usr/bin/env lua5.1
local session = require "resty.session".start{
name = "filter",
storage = "redis",
strategy = "regenerate",
redis = {
prefix = 'waf',
host = 'redis-service',
},
cookie = {
lifetime = 1800,
--renew = 1800,
},
secret = "somesecret..." --required to set a fixed session secret, to prevent session lost on nginx restart / reload
}
local base64 = require 'base64'
--user is not authorized
if not session.data.authorized then
...
end
Openresty log:
2020/07/26 11:50:27 [error] 7#7: *1 lua entry thread aborted: runtime error: /usr/local/openresty/nginx/html/entry.lua:21: attempt to index local 'session' (a nil value)
From my point of view the session
module isn't loaded somehow correctly, it's installed using OPM.
root@324eaa7d19cd:/tmp/lua-gd-2_0_33r2# opm list
bungle/lua-resty-session 3.6
openresty/lua-resty-string 0.11
Is there any way how I can dig deeper into this problem?
It's for sure somehow caused by redis, @bungle any idea how to debug deeper?
Using the hello world example works fine, seems like the module is loaded correctly there.
Hi,
I'm also atempting to use Redis as a session store and getting the same result.
I'm using this as a configuration.
Have you managed to figure out a solution?
Very little documentation about this and hard to debug ( or I don't know how !? ) .
server {
listen 80;
server_name localhost;
default_type text/html;
# redis session backen
set $session_storage redis;
set $session_redis_host 127.0.0.1;
local session, err = require "resty.session".start{...}
print(err)
Thank you @bungle . I managed to discover this the other day and forgot to post a comment.
In my case I was receiving an error.
I am running openresty docker container (via compose) and I had to use set $session_redis_host <docker-compose-service-name>
; In my case redis
. I
For others who run into this, my config files bellow.
@bungle: They can be edited and added as examples .
version: '3.9'
services:
redis:
image: redis
ports:
- 6379:6379
volumes:
- redis_data:/data
resty:
build: .
ports:
- 8080:80
volumes:
# Configurations
- ./nginx/conf.d:/etc/nginx/conf.d
- ./nginx/lua:/etc/nginx/lua
# temporary file storage
- resty_temp:/var/run/openresty/
volumes:
resty_temp:
redis_data:
nginx/conf/default.conf
lua_package_path "/etc/nginx/lua/?.lua;;";
# Settign lua cahche off will reload lua files every time - avoid nginx restarts
lua_code_cache off; #development only
server {
listen 80;
server_name localhost;
default_type text/html;
# https://github.com/bungle/lua-resty-session#none-cipher
set $session_cipher none;
# https://github.com/bungle/lua-resty-session#pluggable-serializers
set $session_serializer json;
# https://github.com/bungle/lua-resty-session#redis-storage-adapter
set $session_storage redis;
set $session_redis_host 'redis';
set $session_secret 623q4hR325t36VsCD3g567922IC0073T;
# https://github.com/openresty/lua-resty-redis/issues/159
resolver local=on ipv6=off;
resolver_timeout 5s;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/openresty/nginx/html;
}
location /lua_content {
# MIME type determined by default_type:
default_type 'text/plain';
content_by_lua_block {
ngx.say('Hello,world!')
}
}
location /greet {
default_type text/plain;
content_by_lua_block {
local hello = require "hello"
hello.greet("a Lua module")
}
}
location / {
content_by_lua '
ngx.say("<html><body><a href=/start>Start the test</a>!</body></html>")
';
}
location /start {
content_by_lua '
local session, err = require "resty.session".start()
if not session then
ngx.log(ngx.ERR,"session present" .. err)
end
session.data.name = "Hello nurse"
session.data.uid = "123"
session:save()
ngx.say("<html><body>Session started. ",
"<a href=/test>Check if it is working</a>!</body></html>")
';
}
location /test {
content_by_lua '
local session = require "resty.session".open()
if session.present then
ngx.log(ngx.ERR, "session uid" .. session.data.uid)
end
if not session.started then
ngx.print(session.data.uid)
session:start()
end
ngx.say("<html><body>Session was started by <strong>",
session.data.name or "Anonymous",
"</strong>! <a href=/modify>Modify the session</a>.</body></html>")
';
}
location /modify {
content_by_lua '
local session = require "resty.session".start()
session.data.name = "Lua Fan"
session:save()
ngx.say("<html><body>Session modified. ",
"<a href=/modified>Check if it is modified</a>!</body></html>")
';
}
location /modified {
content_by_lua '
local session = require "resty.session".open()
ngx.say("<html><body>Session was modified by <strong>",
session.data.name or "Anonymous",
"</strong>! <a href=/destroy>Destroy the session</a>.</body></html>")
';
}
location /destroy {
content_by_lua '
require "resty.session".destroy()
ngx.say("<html><body>Session was destroyed. ",
"<a href=/check>Is it really so</a>?</body></html>")
';
}
location /check {
content_by_lua '
local session = require "resty.session".open()
ngx.say("<html><body>Session was really destroyed, you are known as ",
"<strong>",
session.data.name or "Anonymous",
"</strong>! <a href=/>Start again</a>.</body></html>")
';
}
}
lua/hello.lua
local _M = {}
function _M.greet(name)
ngx.log(ngx.ERR, "hello world here")
ngx.say("Greetings from", name)
end
function _M.debug(session)
local cjson = require "cjson"
ngx.log(ngx.ERR, "session >>", cjson.encode(session), "<<")
end
return _M