OpenResty® 是一款基于 NGINX 和 LuaJIT 的 Web 平台
openresty官网:http://openresty.org/cn/download.html
Luajit官网:https://luajit.org
环境准备
yum install pcre-devel openssl-devel gcc curl wget -y
下载资源
[root@i-0vjkl1ko ~]# mkdir -p /soft
[root@i-0vjkl1ko ~]# cd /soft
[root@i-0vjkl1ko soft]# wget https://openresty.org/download/openresty-1.13.6.2.tar.gz
[root@i-0vjkl1ko soft]# tar -zxvf openresty-1.13.6.2.tar.gz
[root@i-0vjkl1ko soft]# cd openresty-1.13.6.2
然后在进入openresty-1.13.6.2/
目录, 然后输入以下命令配置:
./configure
默认, --prefix=/usr/local/openresty
程序会被安装到/usr/local/openresty目录。
您可以指定各种选项,比如
./configure --prefix=/opt/openresty \
--with-luajit \
--without-http_redis2_module \
--with-http_iconv_module \
--with-http_postgres_module
试着使用 ./configure --help
查看更多的选项。
编译:
make
编译没问题后再安装:
make install
添加环境变量
PATH=/usr/local/openresty/nginx/sbin:$PATH
然后保存
source /etc/profile
通过输入env可查看所以环境变量设置
输入 nginx -v查看版本
[root@i-0vjkl1ko local]# nginx -v
nginx version: openresty/1.13.6.2
启动openresty
nginx
重启openresty
nginx -s reload
注意openresty中的nginx的文件信息如下:
日志和pid:/usr/local/openresty/nginx/logs
配置文件: /usr/local/openresty/nginx/conf/nginx.conf
测试例子1,返回hello world:
vim /usr/local/openresty/nginx/conf/nginx.conf 添加
server {
listen 8080;
location / {
default_type text/html;
content_by_lua '
ngx.say("<p>hello, world</p>")
';
}
}
重启生效 nginx -s reload
执行访问测试,正确返回hello world
[root@i-0vjkl1ko conf]# curl http://localhost:8080/
<p>hello, world</p>
例子2,调用lua脚本,获取uuid
mkdir /usr/local/openresty/nginx/lua
vim /usr/local/openresty/nginx/lua/uuid
function guid()
local seed = {
'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j',
'k','l','m','n','o','p','q','r','s','t',
'u','v','w','x','y','z'
}
local sid = ""
for i=1, 32 do
sid = sid .. seed[math.random(1,36)]
end
return string.format('%s-%s-%s-%s-%s',
string.sub(sid, 1, 8),
string.sub(sid, 9, 12),
string.sub(sid, 13, 16),
string.sub(sid, 17, 20),
string.sub(sid, 21, 32)
)
end
return guid()
vim /usr/local/openresty/nginx/conf/nginx.conf
在http中新增以下server
server {
listen 8082;
location / {
set_by_lua_file $res /usr/local/openresty/nginx/lua/uuid.lua;
echo $res;
}
}
nginx -s reload
执行测试 curl
[root@i-0vjkl1ko nginx]# curl http://localhost:8082
splr2ncq-fk41-zp6z-9cho-ksg6n7l5tk0u
ok,大公告成
下面是我对nginx中链路的理解参数
nginx.conf配置如下:
#user nobody;
worker_processes 1;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
log_format elk '[$time_iso8601] $msec $remote_addr "$server_addr:$server_port" $status $request_time $bytes_sent "$server_protocol" "$request_method" "$request_uri" "$http_referer" "$request_body" $upstream_addr $upstream_response_time "$http_user_agent" "$http_x_forwarded_for" "$uuid" "$http_parentuuid"';
access_log logs/access.log main;
access_log logs/access_elk.log elk;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8080;
location / {
default_type text/html;
content_by_lua '
ngx.say("<p>hello, world</p>")
';
}
}
server {
listen 8081;
location /adder {
set_by_lua_file $res /usr/local/openresty/nginx/lua/adder.lua $arg_n;
echo $res;
}
}
server {
listen 8082;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-for $remote_addr;
set_by_lua_file $uuid /usr/local/openresty/nginx/lua/uuid.lua;
proxy_set_header parentuuid $uuid;
proxy_pass http://127.0.0.1:80;
}
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://127.0.0.1:8080;
}
#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 html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
请求
[root@i-0vjkl1ko nginx]# curl http://localhost:8082
<p>hello, world</p>
time_iso8601 和 msec 记录的是写入日志的时间
请求从8082发起,中间80端口,最后才是8080,但写入日志的时间刚好相反,是因为最开始的请求是最后写入的要统计响应时间和流量大小等。
设置openresty开机自启动
echo '/usr/local/openresty/nginx/sbin/nginx' >> /etc/rc.local
chmod +x /etc/rc.local
ok,reboot测试一下吧