liujit(5.1) http://luajit.org wget http://luajit.org/download/LuaJIT-2.0.0-beta9.tar.gz
lua http://www.lua.org
ngx_devel_kit
git clone https://github.com/simpl/ngx_devel_kit.git
echo-nginx-module git clone https://github.com/agentzh/echo-nginx-module.git
lua-nginx-module
git clone https://github.com/chaoslawful/lua-nginx-module.git
lua-resty-redis
git clone https://github.com/openresty/lua-resty-redis.git
lua-resty-mysql
git clone https://github.com/openresty/lua-resty-mysql.git
cjson
wget http://www.kyne.com.au/~mark/software/download/lua-cjson-2.1.0.tar.gz
--git 包 下載過來包含一下就好lua-resty-redis (可以在nginx.conf包含)
lua_package_path "/home/cq/nginx/lua/lua-resty-redis/lib/?.lua;;";
lua-resty-mysql (可以在lua項目裏包含)
package.path = "/home/cq/nginx/lua/lua-resty-mysql/lib/?.lua;"
--luajit
make
sudo make install PREFIX=/home/cq/nginx/luajit
export LUAJIT_LIB=/home/cq/nginx/luajit/lib
export LUAJIT_INC=/home/cq/nginx/luajit/include/luajit-2.0
--cjson 安裝MakeFile修改
LUA_INCLUDE_DIR = /home/cq/nginx/luajit/include/luajit-2.0
拷貝cjson.so到 /home/cq/nginx/luajit/lib/lua/5.1/
cp cjson.so /home/cq/nginx/luajit/lib/lua/5.1/
cjson (可以在nginx.conf包含)
lua_package_cpath "/home/cq/nginx/luajit/lib/lua/5.1/?.so;;";
--安裝nginx
./configure --prefix=/home/cq/nginx/nginx --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB" --add-module=/home/cq/share/tool/ngx_devel_kit --add-module=/home/cq/share/tool/echo-nginx-module --add-module=/home/cq/share/tool/lua-nginx-module --add-module=/home/cq/share/tool/redis2-nginx-module --with-http_ssl_module --with-http_stub_status_module
make
make install
--之後的配置nginx.conf 配置
http {
include mime.types;
default_type application/octet-stream;
lua_package_path "/home/cq/nginx/lua/lua-resty-redis/lib/?.lua;;";
# lua_package_path "/home/cq/nginx/lua/lua-resty-mysql/lib/?.lua;;";
lua_package_cpath "/home/cq/nginx/luajit/lib/lua/5.1/?.so;;";
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#rewrite ^ https://$http_host$request_uri? permanent;
rewrite ^(.*) https://$server_name$1 permanent;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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 on;
#ssl_certificate /home/cq/nginx/ssl/nginx.crt;
#ssl_certificate_key /home/cq/nginx/ssl/nginx.key;
ssl_certificate /home/cq/nginx/ssl/server.crt;
ssl_certificate_key /home/cq/nginx/ssl/server_nopwd.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
server_tokens off;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
location /lua {
default_type 'text/plain';
content_by_lua 'ngx.say("hello,lua")';
}
location /lua_resty_get{
default_type 'text/plain';
lua_code_cache off;
content_by_lua_file '/home/cq/share/sss/nginx_example/redis_get.lua';
}
location /lua_resty_set{
default_type 'text/plain';
lua_code_cache off;
content_by_lua_file '/home/cq/share/sss/nginx_example/redis_set.lua';
}
}
}
--啓動nginx
cd nginx
sudo ./sbin/nginx -t --檢查配置 都OK success
sudo ./sbin/nginx -c conf/nginx.conf --啓動nginx
sudo ./sbin/nginx -s reload --重新加載
sudo ./sbin/nginx -s stop --停止
tail -10f logs/error.log access.log --日誌
--ssl key
openssl genrsa -des3 -out server.key 1024 # 創建服務器私鑰,該命令會讓你輸入一個口令
openssl req -new -key server.key -out server.csr # 創建簽名請求的證書(CSR)
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key # 在加載SSL支持的Nginx並使用上述私鑰時除去必須的口令
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt # 最後標記證書使用上述私鑰和CSR
官方key
openssl rsa -in 214485401181231.key -out server_nopwd.key # 官方證書生成無鑰key
openssl req -new -x509 -key server_nopwd.key -out ca.crt -days 3650 # 生成ca的crt
--瀏覽器用的話,IE要導入證書以上是配置,接下去是lua代碼測試redis mysql的簡單應用
redis_get.lua
local key = ngx.var.arg_key;
--redis get https://192.168.0.248/lua_resty_get?key=foo3
ngx.header.content_type="text/plain";
local redis = require "resty.redis";
local red = redis:new();
red:set_timeout(1000);
local ok,err = red:connect("127.0.0.1",31026);
if not ok then
ngx.log(ngx.ERR, err);
ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE);
end
local res, err = red:get(key);
if not res then
ngx.log(ngx.ERR, err);
return;
end
ngx.say(res);
local ok, err = red:set_keepalive(10000,100);
if not ok then
ngx.say(ngx.say("failed to set keepalive: "),err);
red:close();
return;
end
--mysql connect query
package.path = "/home/cq/nginx/lua/lua-resty-mysql/lib/?.lua;"
local cjson = require "cjson"
local mysql = require "resty.mysql"
local function query_mysql()
local db = mysql:new()
db:connect{
host = "192.168.0.248",
port = 3306,
database = "sss_game",
user = "root",
password = "123456",
charset = "utf8"
}
local res, err, errno, sqlstate =
db:query("select * from conf where id = "..1)
db:set_keepalive(0, 100)
ngx.say("mysql done: ", cjson.encode(res))
end
ngx.thread.spawn(query_mysql)測試https://192.168.0.248/lua_resty_get?key=foo2
瀏覽器測試:
https://192.168.0.248/lua_resty_get?key=foo2
redis_set.lua
local key = ngx.var.arg_key;
local val = ngx.var.arg_value;
-- https://192.168.0.248/lua_resty_set?key=foo3&value=321321
ngx.header.content_type="text/plain";
local redis = require "resty.redis";
local red = redis:new();
red:set_timeout(1000);
local ok,err = red:connect("127.0.0.1",31026);
if not ok then
ngx.log(ngx.ERR, err);
ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE);
end
local res, err = red:set(key, val);
if not res then
ngx.log(ngx.ERR, err);
return;
end
ngx.say("set result: ", res);
-- sadd
res, err = red:sadd(key..key, val);
if not res then
ngx.log(ngx.ERR, err);
return;
end
ngx.say("sadd result: ", res);
local ok, err = red:set_keepalive(10000,100);
if not ok then
ngx.say(ngx.say("failed to set keepalive: "),err);
red:close();
return;
end
瀏覽器測試:
https://192.168.0.248/lua_resty_set?key=foo2&value=okokok