Last active
March 28, 2017 05:58
-
-
Save dingmingxin/f20927417469532a4548adf09d9497ad to your computer and use it in GitHub Desktop.
openresty_luapath_multihosts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# virtual host2 conf | |
server { | |
listen 80; | |
server_name host1.xxx.com; | |
# Access log with buffer, or disable it completetely if unneeded | |
access_log host1/logs/access.log combined buffer=16k; | |
error_log host1/logs/error.log; | |
# lor runtime | |
location / { | |
set $test_test ""; | |
set_by_lua_file $test_test ./luapath/init.lua; | |
content_by_lua_file ./host1/app/main.lua; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# virtual host2 conf | |
server { | |
listen 80; | |
server_name host2.xxx.com; | |
# Access log with buffer, or disable it completetely if unneeded | |
access_log host2/logs/access.log combined buffer=16k; | |
error_log host2/logs/error.log; | |
# lor runtime | |
location / { | |
set $test_test ""; | |
set_by_lua_file $test_test ./luapath/init.lua; | |
content_by_lua_file ./host2/app/main.lua; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- File: luapath/path_system.lua -- generated by resty_lua_path.sh | |
return function() | |
return "path1/?.lua;path2/?.lua;", "path3/?.so;path4/?.so;" | |
end | |
-- File: luapath/path_framework.lua - edit by hand | |
-- include server common lua file paths | |
return function() | |
return "path1/?.lua;path2/?.lua;", "path3/?.so;path4/?.so;" | |
end | |
-- File: luapath/init.lua | |
local tconcat = table.concat | |
local spath = require "luapath.path_system" | |
local fpath = require "luapath.path_framework" | |
local sys_path, sys_cpath = spath() | |
local f_path, f_cpath = fpath() | |
package.path = tconcat({sys_path, f_path}, ";") | |
package.cpath = tconcat({sys_cpath, f_cpath}, ";") | |
return "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# main nginx conf file | |
pid tmp/nginx.pid; | |
worker_processes 4; | |
events { | |
worker_connections 4096; | |
} | |
http { | |
include ./mime.types; | |
client_max_body_size 4m; | |
client_body_buffer_size 4m; | |
sendfile on; | |
keepalive_timeout 65; | |
charset utf8; | |
lua_package_path "./?.lua;;"; | |
lua_package_cpath "./?.so;;"; | |
include ./sites-enabled/*.nginx.conf; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash - | |
path=$(pwd) | |
folder="${path}/luapath/" | |
mkdir -p $folder | |
package_path=$(resty -e 'ngx.say(package.path)') | |
package_cpath=$(resty -e 'ngx.say(package.cpath)') | |
cat << EOF > $folder/path_system.lua | |
return function() | |
return "$package_path", "$package_cpath" | |
end | |
EOF |
@dingmingxin 其实我感觉你应该先测试一下你的想法对不对。 👎
还有一个问题,package.path 和package.cpath不停增长,占用内存能超过 10M 吗?
@noname007 正是因为不太知道这种做法可行否,才放到这里讨论下。
关于你说的 package.path和package.cpath 不停增长的问题,你可以看下: openresty ›用ab -k 压resty,越来越慢
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
openresty 启动一个ngxin master和多个work进程。
配置为virtual host 分别设置package.path 和package.cpath .
这样做, 和在逻辑代码里设置package.path 相比, 不会导致package.path 和package.cpath不停增长。
但是可能有性能损失,因为是每个请求进来的时候都设置一下
具体这样会不会有问题-- 待测试