sailorproject / sailor

A Lua MVC Web Framework.
MIT License
921 stars 125 forks source link

Create two applications for two different domains. #136

Closed aleksmelnikov closed 7 years ago

aleksmelnikov commented 7 years ago

Hello. Could you help with my issue. I would like to configure the system with nginx + sailor: http://example1.com --> run application 'example1' (located in /usr/local/www/example1) http://example2.com --> run application 'example2' (located in /usr/local/www/example2) How to do this?

I tried so:

Created 2 application in /usr/local/www/example1, /usr/local/www/example2 Nginx run with -p /usr/local/www Nginx.conf: ... http { lua_package_path ' ;/usr/local/share/lua/5.1/?.lua; ;/usr/local/www/example1/?.lua; ;/usr/local/www/example1/controllers/?.lua; ;/usr/local/www/example1/themes/default/?.lp; ;/usr/local/www/example2/?.lua; ;/usr/local/www/example2/controllers/?.lua; ;/usr/local/www/example2/themes/default/?.lp; '; ... server { server_name example1.com; ... location / { default_type text/plain; root /usr/local/www/example1; lua_need_request_body on; lua_code_cache on; content_by_lua_file example1/index.lua; index index.lua index.lp; }

    location ~ ^/[^\.]+$ {
        root /usr/local/www/example1;
        lua_need_request_body on;
        lua_code_cache        off;
        content_by_lua_file   example1/index.lua;
        index                 index.lp index.lua;
        rewrite_by_lua_block  {
            local conf = require "conf.conf"
            if conf.sailor.friendly_urls then
                local args = ngx.req.get_uri_args()
                args[conf.sailor.route_parameter] = ngx.var.uri:sub(2)
                ngx.req.set_uri_args(args)
                ngx.req.set_uri("/index.lua")
            end
        }
    }
    location ~ \.(css|eot|js|json|gif|jpg|png|svg|ttf|woff)$ {
    }

... server { server_name example2.com; ... location / { lua_need_request_body on; lua_code_cache on; content_by_lua_file example2/index.lua; index index.lua index.lp; } location ~ ^/[^.]+$ { lua_need_request_body on; lua_code_cache off; content_by_lua_file example2/index.lua; index index.lp index.lua; rewrite_by_lua_block { local conf = require "conf.conf" if conf.sailor.friendly_urls then local args = ngx.req.get_uri_args() args[conf.sailor.route_parameter] = ngx.var.uri:sub(2) ngx.req.set_uri_args(args) ngx.req.set_uri("/index.lua") end } } location ~ .(css|eot|js|json|gif|jpg|png|svg|ttf|woff)$ { }

Request http://example1.com takes variables from /usr/local/www/example1/conf/conf.lua Request http://example2.com lakes variables from /usr/local/www/example1/conf/conf.lua not from /usr/local/www/example2/conf/conf.lua

Etiene commented 7 years ago

The issue here is that you are adding the package paths at the top level, so the files for one app are overwriting each other. As you added example 1 in the path first and example 2 after that, lua will try to find files in those folders in that exact order. So as you wrote it, if a file is not found at lua's shared libraries, lua will look for it in example 1, then if it's not there then in example 2 and so on. They should be scoped on each server configuration for each app instead of at the top, but Nginx does not allow that, so the solution would not be to adjust the package path in the nginx.conf but rather at the top of index.lua at the root of each app using lua itself, like this:

package.path =  '/usr/local/www/example1/?.lua;'..package.path
local sailor = require "sailor"
sailor.launch()

However, once you do this, you will still run into problems because when I wrote sailor I did not write it having it mind that an app would be initialised from outside its root dir! So some relative paths are bugged specially when loading the templates. I hope this answer is satisfactory for the moment, I'll work on fixing these paths soon :)

aleksmelnikov commented 7 years ago

Yes, thank you. :) It is good that you know current problems with intersection files of different applications. I hope you can fix the issue.

aleksmelnikov commented 7 years ago

May be like this will work fine:

---> index.lua: local base_dir = ((debug.getinfo(1).source):match('^@?(.-)index.lua$') or '') package.path = base_dir..'/?.lua;'..package.path local sailor = require "sailor" sailor.set_application_path(base_dir) sailor.launch()

---> sailor.lua: function sailor.set_application_path(r) sailor.path = r end function sailor.init(r) local page = require "sailor.page" --sailor.set_application_path(r) ...

Etiene commented 7 years ago

Ok! I've made a new release of remy to fix the paths on nginx:

Besides getting the newest version of remy luarocks install remy

you have to add this to the top of index.lua

package.path =  (debug.getinfo(1).source):match('^@?(.-)index.lua$')..'/?.lua;'..package.path

and concerning the nginx config you have to update some points to find the respective subdirs. I wasnt trying two apps on different server names but to apps on different ports of localhost. nevertheless I got them to work! I'll post my whole conf here so you can see by example:

worker_processes  1;
error_log stderr notice;

events {
    worker_connections 16384;
}
http {
    server {
        listen 8080;
        include       app_1/conf/mime.types;
        default_type  application/octet-stream;
        root .;
        sendfile        on;
        access_log off;
        location / {
            lua_need_request_body on;
            lua_code_cache off;
            content_by_lua_file app_1/index.lua;
            index  index.lua index.lp;
        }

        location ~ ^/[^\.]+$ {
            lua_need_request_body on;
            lua_code_cache        off;
            content_by_lua_file   app_1/index.lua;
            index                 index.lp index.lua;
            rewrite_by_lua_block  {
                local conf = require "app_1.conf.conf"
                if conf.sailor.friendly_urls then
                    local args = ngx.req.get_uri_args()
                    args[conf.sailor.route_parameter] = ngx.var.uri:sub(2)

                    ngx.req.set_uri_args(args)
                    ngx.req.set_uri("/index.lua")
                end
            }
        }

        location ~ \.(css|eot|js|json|gif|jpg|png|svg|ttf|woff|map)$ {
            root app_1;
        }
    }

    server {
        listen 8089;

        include       app_2/conf/mime.types;
        default_type  application/octet-stream;
        root .;
        sendfile        on;
        access_log off;
        location / {
            lua_need_request_body on;
            lua_code_cache on;
            content_by_lua_file app_2/index.lua;
            index  index.lua index.lp;
        }

        location ~ ^/[^\.]+$ {
            lua_need_request_body on;
            lua_code_cache        off;
            content_by_lua_file   app_2/index.lua;
            index                 index.lp index.lua;
            rewrite_by_lua_block  {
                local conf = require "app_2.conf.conf"
                if conf.sailor.friendly_urls then
                    local args = ngx.req.get_uri_args()
                    args[conf.sailor.route_parameter] = ngx.var.uri:sub(2)

                    ngx.req.set_uri_args(args)
                    ngx.req.set_uri("/index.lua")
                end
            }
        }

        location ~ \.(css|eot|js|json|gif|jpg|png|svg|ttf|woff|map)$ {
            root app_2;
        }
    }
}
aleksmelnikov commented 7 years ago

Excuse me, why are there 1x"lua_code_cache on" and 3x"lua_code_cache off" in your nginx config? Will it work with 4x"lua_code_cache on"?

Etiene commented 7 years ago

Oops... yes, it will work with 4 lua_code_cache on! I was reloading the pages all the time to test, so I turned them off temporarily then forgot to put it to the default before pasting here!

aleksmelnikov commented 7 years ago

Hello

with location / { lua_need_request_body on; lua_code_cache on; content_by_lua_file app_1/index.lua; index index.lua index.lp; } and location / { lua_need_request_body on; lua_code_cache on; content_by_lua_file app_2/index.lua; index index.lua index.lp; }

the second running application takes the first running application config

aleksmelnikov commented 7 years ago

I think nginx is caching application index.lua with package.path inside it.

aleksmelnikov commented 7 years ago

hello.

to understand nginx lua cache work I did so: I changed file ngx_http_lua_cache.c (lua-nginx-module) from

ifndef DDEBUG

define DDEBUG 0

endif

to

ifndef DDEBUG

define DDEBUG 1

endif

then recompiled nginx and now I can see only lua cache messages in the nginx info log: error_log /var/log/nginx-info.log info;

messages like these:

lua * ngx_http_lua_cache_loadfile: CACHE file key already pre-calculated at /usr/ports/www/nginx/work/lua-nginx-module-0.10.6/src/ngx_http_lua_cache.c line 227. lua * ngx_http_lua_cache_loadfile: XXX cache key for file: [nhlf_d7b4e00e0a1db075c2ac662449d3430c] at /usr/ports/www/nginx/work/lua-nginx-module-0.10.6/src/ngx_http_lua_cache.c line 230. lua * ngx_http_lua_cache_load_code: Code cache table to load: 0x96f8 at /usr/ports/www/nginx/work/lua-nginx-module-0.10.6/src/ngx_http_lua_cache.c line 45. lua * ngx_http_lua_cache_load_code: Value associated with given key in code cache table is not code chunk: stack top=2, top value type=no value at /usr/ports/www/nginx/work/lua-nginx-module-0.10.6/src/ngx_http_lua_cache.c line 80. lua * ngx_http_lua_cache_loadfile: Code cache missed! cache key='nhlf_d7b4e00e0a1db075c2ac662449d3430c', stack top=0, file path='/usr/local/www/application1/index.lua' at /usr/ports/www/nginx/work/lua-nginx-module-0.10.6/src/ngx_http_lua_cache.c line 247. lua * ngx_http_lua_cache_loadfile: loadfile returns 0 (6) at /usr/ports/www/nginx/work/lua-nginx-module-0.10.6/src/ngx_http_lua_cache.c line 252. lua *\ ngx_http_lua_cache_store_code: Code cache table to store: 0x96f8 at /usr/ports/www/nginx/work/lua-nginx-module-0.10.6/src/ngx_http_lua_cache.c line 111.

I hope it will be useful to you. Thanks.

Etiene commented 7 years ago

I'll take a look in this later, but I feel that maybe someone more experienced with openresty specifically would be more helpful. Have you tried reaching to the openresty mail list as well?

aleksmelnikov commented 7 years ago

I created a question in the lua_nginx_module home and got an answer. https://github.com/openresty/lua-nginx-module/issues/894 With 'lua_code_cache on' all requests work on one LUA VM. So, they have one package.path

aleksmelnikov commented 7 years ago

But now I quite suitable your latest code for nginx without cache. Because I need to have many applications on one nginx instance. :) Thank you again.

Etiene commented 7 years ago

ok! So I guess the solution is turning the cache off then! I'll consider this closed then! :)

aleksmelnikov commented 7 years ago

Etiene, I think this temporary solution for current level only. Because ''cache off" run own lua vm for every user request. It's a big memory usage for big web project with thousands users. So, you have to find a better solution in the future. Then big portals can use the framework. Anyone understand that is complicated code update and can take much time. But it more important then other improvements. It will be best options for the framework! :) So, I vote to open back the issue.

Etiene commented 7 years ago

It is however an nginx issue and not sailor's, which is why I closed it. I don't think there's anything I can do about this unless nginx changes how their vm works...