blog
blog copied to clipboard
Mac下openresty安装
- 下载openresty.
- 按照
# 安装目标目录
export OPENRESTY_HOME=$HOME/openresty
mkdir -p $OPENRESTY_HOME
# 安装openssl和pcre
brew install pcre openssl
# 设置pcre和openssl的安装目录
export PCRE_HOME=/usr/local/Cellar/pcre/8.42
export OPENSSL_HOME=/usr/local/Cellar/openssl/1.0.2q
# 编译安装
tar xzf openresty-1.11.2.5.tar.gz
cd openresty-1.11.2.5
./configure \
--prefix="$OPENRESTY_HOME" \
--with-cc-opt="-I$PCRE_HOME/include -I$OPENSSL_HOME/include" \
--with-ld-opt="-L$PCRE_HOME/lib -L$OPENSSL_HOME/lib" \
-j8\
--with-pcre-jit \
--with-http_realip_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_v2_module
make -j8
make install
brew install openresty/brew/openresty
Mac下Kong的安装与使用
原文点击
安装,由于使用homebrew各种报错,最后直接使用源码安装
安装openresty
Mac OS X (macOS) 用户安装过程中由于pcre和openssl可能出错,直接使用brew单独安装 http://openresty.org/cn/installation.html
brew update
brew install pcre openssl
安装成功后使用list查询安装路径
brew list pcre openssl
/usr/local/Cellar/pcre/8.41/bin/pcre-config
/usr/local/Cellar/pcre/8.41/bin/pcregrep
/usr/local/Cellar/pcre/8.41/bin/pcretest
/usr/local/Cellar/pcre/8.41/include/ (6 files)
/usr/local/Cellar/pcre/8.41/lib/libpcre.1.dylib
/usr/local/Cellar/pcre/8.41/lib/libpcre16.0.dylib
/usr/local/Cellar/pcre/8.41/lib/libpcre32.0.dylib
/usr/local/Cellar/pcre/8.41/lib/libpcrecpp.0.dylib
/usr/local/Cellar/pcre/8.41/lib/libpcreposix.0.dylib
/usr/local/Cellar/pcre/8.41/lib/pkgconfig/ (5 files)
/usr/local/Cellar/pcre/8.41/lib/ (10 other files)
/usr/local/Cellar/pcre/8.41/share/doc/ (64 files)
/usr/local/Cellar/pcre/8.41/share/man/ (103 files)
/usr/local/Cellar/openssl/1.0.2n/.bottle/etc/ (8 files)
/usr/local/Cellar/openssl/1.0.2n/bin/c_rehash
/usr/local/Cellar/openssl/1.0.2n/bin/openssl
/usr/local/Cellar/openssl/1.0.2n/include/openssl/ (75 files)
/usr/local/Cellar/openssl/1.0.2n/lib/libcrypto.1.0.0.dylib
/usr/local/Cellar/openssl/1.0.2n/lib/libssl.1.0.0.dylib
/usr/local/Cellar/openssl/1.0.2n/lib/engines/ (12 files)
/usr/local/Cellar/openssl/1.0.2n/lib/pkgconfig/ (3 files)
/usr/local/Cellar/openssl/1.0.2n/lib/ (4 other files)
/usr/local/Cellar/openssl/1.0.2n/share/man/ (1680 files)
安装 luarocks
brew install lua
brew link luarocks
git clone git://github.com/diegonehab/luasocket.git
cd luasocket
make macosx
sudo make install
安装openresty
wget https://openresty.org/download/openresty-1.11.2.5.tar.gz
tar xzf openresty-1.11.2.5.tar.gz
cd openresty-1.11.2.5
./configure \
--with-cc-opt="-I/usr/local/Cellar/openssl/1.0.2o_1/include/ -I/usr/local/Cellar/pcre/8.41/include/" \
--with-ld-opt="-L/usr/local/Cellar/openssl/1.0.2o_1/lib/ -L/usr/local/Cellar/pcre/8.41/lib/" \
-j8\
--with-pcre-jit \
--with-ipv6 \
--with-http_realip_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_v2_module
make -j8
make install
安装postgresql
在 Mac OS 安装
brew install postgresql
initdb /usr/local/var/postgres -E utf8
pg_ctl -D /usr/local/var/postgres -l logfile start
/usr/local/bin/psql -d postgres
#初始化kong的用户和数据库
CREATE USER kong; CREATE DATABASE kong OWNER kong;
在 Centos7 安装 postgresql 9.6 rpm
yum install -y https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm
yum install -y postgresql96-server postgresql96-contrib
# 初始化数据库
/usr/pgsql-9.6/bin/postgresql96-setup initdb
# 设置开机启动
systemctl enable postgresql-9.6.service
# 启动数据库
systemctl start postgresql-9.6.service
# 配置远程访问
vi /var/lib/pgsql/9.6/data/postgresql.conf
安装kong
wget https://codeload.github.com/Kong/kong/tar.gz/0.12.2 -O kong-0.12.2.tar.gz
tar xzf kong-0.12.2.tar.gz
sudo make
sudo make install
设置环境变量 我这里是直接改了zsh的环境变量,不用zsh的可以改~/.bash_profile
vi ~/.zsh_profile
export PATH="$PATH:/usr/local/openresty/bin"
export PATH="$PATH:/Users/LvYong/dev/kong-0.12.2/kong-0.12.2/bin"
启动kong
#初始化
kong migrations up
#启动
kong start
测试kong
⋊> ~ curl -i http://localhost:8001/
HTTP/1.1 200 OK
Date: Thu, 08 Mar 2018 08:49:08 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.12.2
问题1:
/usr/include/sys/resource.h:204:2: error: unknown type name 'uint64_t'
解决办法:
mv /usr/local/include /usr/local/include.old
brew install llvm
问题2:
Error: Failed installing dependency: https://luarocks.org/luasec-0.6-2.src.rock - Could not find header file for OPENSSL
No file openssl/ssl.h in /usr/local/include
No file openssl/ssl.h in /usr/include
解决办法:
cd /usr/local/include
ln -s ../opt/openssl/include/openssl
本机脚本:
➜ typhon4g git:(master) brew list pcre openssl
/usr/local/Cellar/pcre/8.43/bin/pcre-config
/usr/local/Cellar/pcre/8.43/bin/pcregrep
/usr/local/Cellar/pcre/8.43/bin/pcretest
/usr/local/Cellar/pcre/8.43/include/ (6 files)
/usr/local/Cellar/pcre/8.43/lib/libpcre.1.dylib
/usr/local/Cellar/pcre/8.43/lib/pkgconfig/ (5 files)
/usr/local/Cellar/pcre/8.43/lib/ (10 other files)
/usr/local/Cellar/pcre/8.43/share/doc/ (64 files)
/usr/local/Cellar/pcre/8.43/share/man/ (103 files)
/usr/local/Cellar/openssl/1.0.2s/.bottle/etc/ (8 files)
/usr/local/Cellar/openssl/1.0.2s/bin/openssl
/usr/local/Cellar/openssl/1.0.2s/include/openssl/ (75 files)
/usr/local/Cellar/openssl/1.0.2s/lib/libcrypto.1.0.0.dylib
./configure \
--prefix="/Users/bingoobjca/run/openresty-1.13.6.2" \
--with-cc-opt="-I/usr/local/Cellar/openssl/1.0.2s/include/ -I/usr/local/Cellar/pcre/8.43/include/" \
--with-ld-opt="-L/usr/local/Cellar/openssl/1.0.2s/lib/ -L/usr/local/Cellar/pcre/8.43/lib/" \
-j8 \
--with-pcre-jit \
--with-ipv6 \
--with-http_realip_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_v2_module
--prefix=path — 定义服务器文件的完整路径,该路径同时也是configure命令设置的 相对路径(除类库源文件外)以及nginx.conf文件定义的相对路径的基准路径。其默认 值是/usr/local/nginx。参考
kong 1.2.1 是基于lua5.1的,所以得先按照5.1,不能是5.2+等。
curl -R -O http://www.lua.org/ftp/lua-5.1.5.tar.gz
tar zxf lua-5.1.5.tar.gz
cd lua-5.1.5
make macosx test
sudo make install
$ brew tap kong/kong
$ brew install kong
$ /usr/local/opt/[email protected]/luarocks/bin/luarocks install cqueues 20171014.51-0 --tree=/usr/local/Cellar/kong/1.2.1 make CRYPTO_DIR=/usr/local/opt/[email protected]/openssl OPENSSL_DIR=/usr/local/opt/[email protected]/openssl
$ brew install kong
mac上安装openresty-1.13.6.2:
➜ openresty-1.13.6.2 ls /usr/local/Cellar/openssl
1.0.2s
➜ openresty-1.13.6.2 ls /usr/local/Cellar/pcre
8.43
➜ openresty-1.13.6.2 ./configure \
--with-cc-opt="-I/usr/local/Cellar/openssl/1.0.2s/include/ -I/usr/local/Cellar/pcre/8.43/include/" \
--with-ld-opt="-L/usr/local/Cellar/openssl/1.0.2s/lib/ -L/usr/local/Cellar/pcre/8.43/lib/" \
-j8\
--with-pcre-jit \
--with-ipv6 \
--with-http_realip_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_v2_module
➜ openresty-1.13.6.2 make -j8
➜ openresty-1.13.6.2 sudo make install
...
mkdir -p /usr/local/openresty/site/lualib /usr/local/openresty/site/pod /usr/local/openresty/site/manifest
ln -sf /usr/local/openresty/nginx/sbin/nginx /usr/local/openresty/bin/openresty
安装luarocks:
➜ Downloads curl -LO https://luarocks.github.io/luarocks/releases/luarocks-2.4.3.tar.gz
➜ Downloads tar zxf luarocks-2.4.3.tar.gz
➜ Downloads cd luarocks-2.4.3
➜ luarocks-2.4.3 ls /usr/local/openresty/luajit/include/
luajit-2.1
➜ luarocks-2.4.3 ./configure \
--lua-suffix=jit \
--with-lua=/usr/local/openresty/luajit \
--with-lua-include=/usr/local/openresty/luajit/include/luajit-2.1
➜ luarocks-2.4.3 make build
➜ luarocks-2.4.3 make install
➜ luarocks-2.4.3 luarocks
LuaRocks 2.4.3, the Lua package manager
安装kong
➜ Downloads curl -LO https://github.com/Kong/kong/archive/1.2.2.tar.gz
➜ Downloads tar zxf 1.2.2.tar.gz
➜ Downloads cd kong-1.2.2
➜ kong-1.2.2 export LDFLAGS="-L/usr/local/opt/openssl/lib"
➜ kong-1.2.2 export CPPFLAGS="-I/usr/local/opt/openssl/include"
➜ kong-1.2.2 sudo luarocks make OPENSSL_DIR=/usr/local/opt/openssl CRYPTO_DIR=/usr/local/opt/openssl
参考
package.path
和package.cpath
, from Lua Unofficial FAQ (uFAQ)
So what search path does require use? This is contained in the value of package.path: on a Unix system this would look like this (note that semicolons are used to separate items):
$> lua -e "print(package.path)"
./?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;/usr/local/lib/lua/5.1/?.lua;/usr/local/lib/lua/5.1/?/init.lua
On a Windows machine:
C:\>lua -e "print(package.path)"
.\?.lua;C:\Program Files\Lua\5.1\lua\?.lua;C:\Program Files\Lua\5.1\lua\?\init.
lua;C:\Program Files\Lua\5.1\?.lua;C:\Program Files\Lua\5.1\?\init.lua;C:\Progra
m Files\Lua\5.1\lua\?.luac
It's important to note that Lua itself does not know anything about directories; it will take the module name and substitute it as '?' in each pattern and see if that file exists; if so, that is then loaded.
A very instructive error message happens if you try to require a module that does not exist. This shows you explicitly how Lua is trying to find the module by matching the path entries:
$> lua -lalice
lua: module 'alice' not found:
no field package.preload['alice']
no file './alice.lua'
no file '/home/sdonovan/lua/lua/alice.lua'
no file '/home/sdonovan/lua/lua/alice/init.lua'
no file './alice.so'
no file '/home/sdonovan/lua/clibs/alice.so'
no file '/home/sdonovan/lua/clibs/loadall.so'
The second difference is the require keeps track of what modules have been loaded, so calling it again will not cause the module to be reloaded.
The third difference is that require will also load binary modules. In a similar fashion, package.cpath is used to find modules - they are shared libraries (or DLLs) which export an initialisation function.
$> lua -e "print(package.cpath)"
./?.so;/usr/local/lib/lua/5.1/?.so;/usr/local/lib/lua/5.1/loadall.so
C:\>lua -e "print(package.cpath)"
.\?.dll;.\?51.dll;C:\Program Files\Lua\5.1\?.dll;C:\Program Files\Lua\5.1\?51.dl
l;C:\Program Files\Lua\5.1\clibs\?.dll;C:\Program Files\Lua\5.1\clibs\?51.dll;C:
\Program Files\Lua\5.1\loadall.dll;C:\Program Files\Lua\5.1\clibs\loadall.dll
package.path
and package.cpath
can be modified, changing the behaviour of require. Lua will also use the environment variables LUA_PATH
and LUA_CPATH
if they are defined, to override the default paths.
What does './?.lua' match? This matches any Lua module in the current directory, which is useful for testing.
Why is there the pattern '/usr/local/share/lua/5.1/?/init.lua'? This is allows for self-contained packages of modules. So if there was a directory 'mylib' on the Lua path, then require 'mylib' would load 'mylib/init.lua'.
What if the module name contains dots, such as require 'socket.core? Lua replaces the period with the appropriate directory separator ('socket/core' or 'socket\core') and performs the search using this form. So on a Unix machine, it replaces '?' in '/usr/local/lib/lua/5.1/?.so' to give '/usr/local/lib/lua/5.1/socket/core.so' which matches.
1.20 How to explicitly load a binary module? A binary module is a shared library that must have one entry point with a special name. For instance, if I have a binary module fred then its initialisation function must be called luaopen_fred. This code will explicitly load fred given a full path:
local fn,err = package.loadlib('/home/sdonovan/lua/clibs/fred.so','luaopen_fred')
if not fn then print(err)
else
fn()
end
(For Windows, use an extension of .dll)
LUA_INIT
set ( LUA_PATH "LUA_PATH" CACHE STRING "Environment variable to use as package.path." ) set ( LUA_CPATH "LUA_CPATH" CACHE STRING "Environment variable to use as package.cpath." ) set ( LUA_INIT "LUA_INIT" CACHE STRING "Environment variable for initial script." )
lua解释器执行其参数前,会先查找一个名为LUA_INIT的环境变量:
如果找到这个变量,并且其内容为“@filename”,那么解释器会先执行这个文件; 如果LUA_INIT不是以@开头,则作为Lua代码执行。
如下面的示例,将LUA_INIT定义为"@a.lua",然后启动lua解释器,自动加载了a.lua文件。
# Lua init
export LUA_INIT="@$HOME/.lua/init.lua"
package.path = package.path .. ";" .. os.getenv("HOME") .. "/.lua/?.lua"
package.cpath = package.cpath .. ";" .. os.getenv("HOME") .. "/.lua/?.so"
参考
local resty_home = "/home/vagrant/openresty"
package.path = resty_home .. "/lualib/?.lua;" .. resty_home .. "/luajit/share/luajit-2.1.0-beta3/?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;".. resty_home .. "/luajit/share/lua/5.1/?.lua;" .. resty_home .. "/luajit/share/lua/5.1/?/init.lua;"
package.cpath = "./?.so;/usr/local/lib/lua/5.1/?.so;" .. resty_home .. "/luajit/lib/?.so;/usr/local/lib/lua/5.1/loadall.so"
春哥说了,OpenResty指定prefix编译后,就不能随便调整路径继续正常运行了
No, the installation tree of OpenResty is not relocatable by design, as you can see from the runtime error messages. The nginx executable of OpenResty relies on the location specified (by --prefix=PATH of ./configure) when building OpenResty to find dynamic libraries it depends on. Similarly, OpenResty relies on the same prefix to locate the default Lua libraries shipped by OpenResty.
Very few C/C++ projects in the *NIX world allows moving the installation trees to a different location than specified at build time.
编译环境:
# yum groupinstall "Development Tools"
yum -y install gcc gcc-c++ autoconf automake make
yum -y install zlib zlib-devel openssl pcre libxml2 libxml2-devel
yum -y install readline-devel pcre-devel openssl-devel
编译安装:
V=1.9.3.1 \
&& cd /usr/local/src \
&& ( [ -f "ngx_openresty-$V.tar.gz" ] || wget https://openresty.org/download/ngx_openresty-$V.tar.gz ) \
&& rm -rf ngx_openresty-$V \
&& tar zxvf ngx_openresty-$V.tar.gz \
&& cd ngx_openresty-$V \
&& ./configure \
--prefix=/usr/local/openresty \
--http-log-path=/var/log/openresty/access.log \
--error-log-path=/var/log/openresty/error.log \
--http-client-body-temp-path=/var/cache/openresty/client_temp \
--http-proxy-temp-path=/var/cache/openresty/proxy_temp \
--http-fastcgi-temp-path=/var/cache/openresty/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/openresty/uwsgi_temp \
--http-scgi-temp-path=/var/cache/openresty/scgi_temp \
--pid-path=/var/run/openresty.pid \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_ssl_module \
--with-cc-opt='-O2 -g' \
--with-luajit \
--without-lua_redis_parser \
--without-lua_rds_parser \
--without-lua_resty_dns \
--without-lua_resty_memcached \
--without-lua_resty_redis \
--without-lua_resty_mysql \
--without-lua_resty_upload \
--without-lua_resty_upstream_healthcheck \
--without-lua_resty_string \
--without-lua_resty_websocket \
--without-lua_resty_lock \
--without-lua_resty_lrucache \
--without-lua_resty_core \
--without-http_xss_module \
--without-http_coolkit_module \
--without-http_set_misc_module \
--without-http_form_input_module \
--without-http_encrypted_session_module \
--without-http_srcache_module \
--without-http_headers_more_module \
--without-http_array_var_module \
--without-http_memc_module \
--without-http_redis2_module \
--without-http_redis_module \
--without-http_rds_json_module \
--without-http_rds_csv_module \
&& make -j2 \
&& make install
基本配置:
[ -f /usr/sbin/nginx ] && mv /usr/sbin/nginx{,.backup}
ln -sfT /usr/local/openresty/nginx/sbin/nginx /usr/sbin/nginx
mkdir -p /usr/local/openresty/nginx/conf/{sites-available,sites-enabled}
mkdir -p /var/cache/openresty/
cat > /etc/logrotate.d/openresty <<EOF
/var/log/openresty/*.log {
daily
missingok
rotate 30
compress
notifempty
dateext
sharedscripts
postrotate
[ -f /var/run/openresty.pid ] && kill -USR1 \`cat /var/run/openresty.pid\`
endscript
}
EOF
cd /usr/local/openresty/nginx/conf/sites-available \
&& cat > default.conf <<EOF
#default site
server {
listen 80 default;
#server_name default;
root /usr/local/nginx/html;
index index.html index.htm;
#charset gb2312,utf-8,gbk;
#autoindex on;
#autoindex_exact_size off;
#autoindex_localtime on;
access_log /var/log/openresty/access.log main;
error_log /var/log/openresty/error.log warn;
location /nginx_status {
stub_status on;
access_log off;
#allow 127.0.0.1;
#allow 172.17.10.0/24;
#deny all;
}
} #end server
EOF
cd /usr/local/openresty/nginx/conf/sites-enabled \
&& ln -sfT ../sites-available/default.conf default.conf
/usr/local/openresty/nginx/conf/nginx.conf
#nginx.conf(openresty)
user nobody;
worker_processes 2;
error_log /var/log/openresty/error.log;
pid /var/run/openresty.pid;
worker_rlimit_nofile 10240;
events {
use epoll;
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
access_log /var/log/openresty/access.log;
log_format main
'$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio" "$connection" "$request_time"';
#server_names_hash_bucket_size 64
#proxy_read_timeout 200;
keepalive_timeout 65;
sendfile on;
#tcp_nopush on;
tcp_nodelay on;
server_tokens off;
gzip on;
gzip_min_length 1k;
gzip_proxied any;
gzip_buffers 4 32k;
gzip_types text/plain text/css text/xml application/xml application/atom+xml;
#gzip_types text/plain text/css text/xml;
gzip_disable "MSIE [1-6]\.";
ignore_invalid_headers on;
client_header_buffer_size 32k;
large_client_header_buffers 4 128k;
include sites-enabled/*.conf;
} #end http
/etc/init.d/openresty
#!/bin/sh
#
# openresty Startup script for openresty/nginx
#
# chkconfig: - 85 15
# processname: nginx
# config: /usr/local/openresty/nginx/conf/nginx.conf
# config: /etc/sysconfig/openresty
# pidfile: /var/run/openresty.pid
# description: nginx(openresty) is a HTTP and reverse proxy server
#
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop nginx/openresty
### END INIT INFO
# Source function library.
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/openresty ]; then
. /etc/sysconfig/openresty
fi
prog=nginx
nginx=${NGINX-/usr/local/openresty/nginx/sbin/nginx}
conffile=${CONFFILE-/usr/local/openresty/nginx/conf/nginx.conf}
lockfile=${LOCKFILE-/var/lock/subsys/openresty}
pidfile=${PIDFILE-/var/run/openresty.pid}
SLEEPMSEC=100000
RETVAL=0
start() {
echo -n $"Starting $prog: "
daemon --pidfile=${pidfile} ${nginx} -c ${conffile}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} ${prog}
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
echo -n $"Reloading $prog: "
killproc -p ${pidfile} ${prog} -HUP
RETVAL=$?
echo
}
upgrade() {
oldbinpidfile=${pidfile}.oldbin
configtest -q || return 6
echo -n $"Staring new master $prog: "
killproc -p ${pidfile} ${prog} -USR2
RETVAL=$?
echo
/bin/usleep $SLEEPMSEC
if [ -f ${oldbinpidfile} -a -f ${pidfile} ]; then
echo -n $"Graceful shutdown of old $prog: "
killproc -p ${oldbinpidfile} ${prog} -QUIT
RETVAL=$?
echo
else
echo $"Upgrade failed!"
return 1
fi
}
configtest() {
if [ "$#" -ne 0 ] ; then
case "$1" in
-q)
FLAG=$1
;;
*)
;;
esac
shift
fi
${nginx} -t -c ${conffile} $FLAG
RETVAL=$?
return $RETVAL
}
rh_status() {
status -p ${pidfile} ${nginx}
}
# See how we were called.
case "$1" in
start)
rh_status >/dev/null 2>&1 && exit 0
start
;;
stop)
stop
;;
status)
rh_status
RETVAL=$?
;;
restart)
configtest -q || exit $RETVAL
stop
start
;;
upgrade)
upgrade
;;
condrestart|try-restart)
if rh_status >/dev/null 2>&1; then
stop
start
fi
;;
force-reload|reload)
reload
;;
configtest)
configtest
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|upgrade|reload|status|help|configtest}"
RETVAL=2
esac
exit $RETVAL
rewrite_by_lua/access_by_lua/content_by_lua的区别:
-
nginx有11个处理阶段
nginx把一个请求分成了很多阶段,这样第三方模块就可以根据自己行为,挂载到不同阶段进行处理达到目的。
阶段 说明 set_by_lua* 流程分支处理判断变量初始化 rewrite_by_lua* 转发、重定向、缓存等功能(例如特定请求代理到外网) access_by_lua* IP准入、接口权限等情况集中处理(例如配合iptable完成简单防火墙) content_by_lua* 内容生成 header_filter_by_lua* 应答HTTP过滤处理(例如添加头部信息) body_filter_by_lua* 应答BODY过滤处理(例如完成应答内容统一成大写) log_by_lua* 回话完成后本地异步完成日志记录(日志可以记录在本地,还可以同步到其他机器) - 实际上我们只使用其中一个阶段content_by_lua*,也可以完成所有的处理。
- 但这样 会让我们的代码比较臃肿,越到后期越发难以维护。把我们的逻辑放在不同阶段,分工明确,代码独立,后期发力可以有很多有意思的玩法。
-
阶段 使用范围 init_by_lua* http set_by_lua* server, server if, location, location if rewrite_by_lua* http, server, location, location if access_by_lua* http, server, location, location if content_by_lua* location, location if header_filter_by_lua* http, server, location, location if body_filter_by_lua* http, server, location, location if log_by_lua* http, server, location, location if timer - - set_by_lua*/header_filter_by_lua*/body_filter_by_lua*/log_by_lua*阶段不能运行Output API、Control API、Subrequest API、Cosocket API
- output API(ngx.say和ngx.send_headers)
- control API(ngx.exit和ngx.exec)
- subrequest API(ngx.location.capture和ngx.location.capture_multi)
- cosocket API(ngx.socket.tcp和ngx.req.socket, lua-resty-http是基于cosocket实现的)
-
自从 v0.9.17 版本, 使用指令xxx_by_lua是 不优雅 的,应使用新的 xxx_by_lua_block 指令进行替代。from
-
在 rewrite_by_lua/access_by_lua 处理内部,当调用ngx.exit(ngx.OK)时,nginx 请求将继续下一阶段的内容处理。要在 rewrite_by_lua处理中终结当前请求,调用 ngx.exit,成功的请求设定 status >= 200 (ngx.HTTP_OK) 并 status < 300 (ngx.HTTP_SPECIAL_RESPONSE),失败的请求设定ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) (或其他相关的)。
-
Cosockets Not Available Everywhere Due to internal limitations in the Nginx core, the cosocket API is disabled in the following contexts: set_by_lua*, log_by_lua*, header_filter_by_lua*, and body_filter_by_lua.
The cosockets are currently also disabled in the init_by_lua* and init_worker_by_lua* directive contexts but we may add support for these contexts in the future because there is no limitation in the Nginx core (or the limitation might be worked around).
There exists a workaround, however, when the original context does not need to wait for the cosocket results. That is, creating a zero-delay timer via the ngx.timer.at API and do the cosocket results in the timer handler, which runs asynchronously as to the original context creating the timer.
参考 :
Why /usr/lib64/liblua-5.1.so
appears?
[footstone@tencent-beta18 ~]$ /home/footstone/openresty/nginx/sbin/nginx -p /home/footstone/openresty/nginx -s reload
[footstone@tencent-beta18 ~]$ ps -ef|grep nginx
footsto+ 7175 1 0 8月30 ? 00:00:00 nginx: master process /home/footstone/openresty/nginx/sbin/nginx -p /home/footstone/openresty/nginx
footsto+ 26354 7175 0 14:16 ? 00:00:00 nginx: worker process
footsto+ 26355 7175 0 14:16 ? 00:00:00 nginx: worker process
footsto+ 26356 7175 0 14:16 ? 00:00:00 nginx: worker process
footsto+ 26357 7175 0 14:16 ? 00:00:00 nginx: worker process
footsto+ 26366 26131 0 14:16 pts/2 00:00:00 grep --color=auto nginx
[footstone@tencent-beta18 ~]$ cat /proc/26354/maps|grep lua
7f8c585ac000-7f8c58623000 r-xp 00000000 fd:01 2786435 /home/footstone/openresty/luajit/lib/libluajit-5.1.so.2.1.0
7f8c58623000-7f8c58823000 ---p 00077000 fd:01 2786435 /home/footstone/openresty/luajit/lib/libluajit-5.1.so.2.1.0
7f8c58823000-7f8c58825000 r--p 00077000 fd:01 2786435 /home/footstone/openresty/luajit/lib/libluajit-5.1.so.2.1.0
7f8c58825000-7f8c58826000 rw-p 00079000 fd:01 2786435 /home/footstone/openresty/luajit/lib/libluajit-5.1.so.2.1.0
[footstone@tencent-beta18 ~]$ cat /proc/26354/maps|grep lua
7f8c585ac000-7f8c58623000 r-xp 00000000 fd:01 2786435 /home/footstone/openresty/luajit/lib/libluajit-5.1.so.2.1.0
7f8c58623000-7f8c58823000 ---p 00077000 fd:01 2786435 /home/footstone/openresty/luajit/lib/libluajit-5.1.so.2.1.0
7f8c58823000-7f8c58825000 r--p 00077000 fd:01 2786435 /home/footstone/openresty/luajit/lib/libluajit-5.1.so.2.1.0
7f8c58825000-7f8c58826000 rw-p 00079000 fd:01 2786435 /home/footstone/openresty/luajit/lib/libluajit-5.1.so.2.1.0
[footstone@tencent-beta18 ~]$ cat /proc/26354/maps|grep lua
7f8c585ac000-7f8c58623000 r-xp 00000000 fd:01 2786435 /home/footstone/openresty/luajit/lib/libluajit-5.1.so.2.1.0
7f8c58623000-7f8c58823000 ---p 00077000 fd:01 2786435 /home/footstone/openresty/luajit/lib/libluajit-5.1.so.2.1.0
7f8c58823000-7f8c58825000 r--p 00077000 fd:01 2786435 /home/footstone/openresty/luajit/lib/libluajit-5.1.so.2.1.0
7f8c58825000-7f8c58826000 rw-p 00079000 fd:01 2786435 /home/footstone/openresty/luajit/lib/libluajit-5.1.so.2.1.0
[footstone@tencent-beta18 ~]$ curl localhost:8081
{"status":404, "message":"服务请求不存在", "more info":"URI没找到,请检查API设置"}[footstone@tencent-beta18 ~]$
[footstone@tencent-beta18 ~]$ cat /proc/26354/maps|grep lua
7f8c51f00000-7f8c51f2c000 r-xp 00000000 fd:01 329142 /usr/lib64/liblua-5.1.so
7f8c51f2c000-7f8c5212b000 ---p 0002c000 fd:01 329142 /usr/lib64/liblua-5.1.so
7f8c5212b000-7f8c5212d000 r--p 0002b000 fd:01 329142 /usr/lib64/liblua-5.1.so
7f8c5212d000-7f8c5212e000 rw-p 0002d000 fd:01 329142 /usr/lib64/liblua-5.1.so
7f8c5212e000-7f8c52131000 r-xp 00000000 fd:01 2786787 /home/footstone/openresty/lualib/zlib.so
7f8c52131000-7f8c52331000 ---p 00003000 fd:01 2786787 /home/footstone/openresty/lualib/zlib.so
7f8c52331000-7f8c52332000 rw-p 00003000 fd:01 2786787 /home/footstone/openresty/lualib/zlib.so
7f8c52332000-7f8c52339000 r-xp 00000000 fd:01 2786465 /home/footstone/openresty/lualib/cjson.so
7f8c52339000-7f8c52539000 ---p 00007000 fd:01 2786465 /home/footstone/openresty/lualib/cjson.so
7f8c52539000-7f8c5253a000 r--p 00007000 fd:01 2786465 /home/footstone/openresty/lualib/cjson.so
7f8c5253a000-7f8c5253b000 rw-p 00008000 fd:01 2786465 /home/footstone/openresty/lualib/cjson.so
7f8c585ac000-7f8c58623000 r-xp 00000000 fd:01 2786435 /home/footstone/openresty/luajit/lib/libluajit-5.1.so.2.1.0
7f8c58623000-7f8c58823000 ---p 00077000 fd:01 2786435 /home/footstone/openresty/luajit/lib/libluajit-5.1.so.2.1.0
7f8c58823000-7f8c58825000 r--p 00077000 fd:01 2786435 /home/footstone/openresty/luajit/lib/libluajit-5.1.so.2.1.0
7f8c58825000-7f8c58826000 rw-p 00079000 fd:01 2786435 /home/footstone/openresty/luajit/lib/libluajit-5.1.so.2.1.0
mac:
➜ api-gateway-ng git:(feature/feature_refactor) ✗ vmmap 63328 |grep lua
__TEXT 0000000003806000-000000000380b000 [ 20K 20K 0K 0K] r-x/rwx SM=COW /Users/bingoobjca/run/openresty-1.13.6.2/lualib/cjson.so
__LINKEDIT 000000000380c000-000000000380f000 [ 12K 4K 0K 0K] r--/rwx SM=COW /Users/bingoobjca/run/openresty-1.13.6.2/lualib/cjson.so
__TEXT 00000001039d6000-0000000103a51000 [ 492K 472K 0K 0K] r-x/rwx SM=COW /Users/bingoobjca/run/openresty-1.13.6.2/luajit/lib/libluajit-5.1.2.1.0.dylib
__LINKEDIT 0000000103a53000-0000000103a7a000 [ 156K 8K 0K 0K] r--/rwx SM=COW /Users/bingoobjca/run/openresty-1.13.6.2/luajit/lib/libluajit-5.1.2.1.0.dylib
__DATA 000000000380b000-000000000380c000 [ 4K 4K 4K 0K] rw-/rwx SM=COW /Users/bingoobjca/run/openresty-1.13.6.2/lualib/cjson.so
__DATA 0000000103a51000-0000000103a53000 [ 8K 8K 8K 0K] rw-/rwx SM=COW /Users/bingoobjca/run/openresty-1.13.6.2/luajit/lib/libluajit-5.1.2.1.0.dylib