Nginx+lua+[memcached,redis]-Webサイトの階調公開を実現
6058 ワード
逸品ケース
1、Nginx + lua +[memcached,redis]
2、 / Leaf ID( UUID)
3、Redis , / MQ Select Poll Epoll Reactor
4、 Quartz docker+k8s
5、Zookeeper+Kafka
6、 Netty+WebSocket BIO NIO AIO Netty
7、 (HDFS|FastDFS)
8、OAuth2 -
9、 Redis SSO
10、 -Restful 10 DRF django restframe workd
Nginx+lua+[memcached,redis]-Webサイトの階調公開を実現
luaスクリプト
js,python,dart,go構文はShellに似ており,強い言語と弱い言語である.
print("hello lua")
Openrestyインストール、ump.jd.com
wget https://openresty.org/download/ngx_openresty-1.9.3.2.tar.gz
tar zxf ngx_openresty-1.9.3.2.tar.gz
cd ngx_openresty-1.9.3.2
./configure --prefix=/soft/openresty --with-luajit --with-http_stub_status_module --with-pcre --with-pcre-jit
make && make install
nginx構成
#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"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream java_prod {
server 192.168.23.164:8080;
}
upstream java_test {
server 192.168.23.164:9090;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /hello {
default_type text/html;
content_by_lua_block {
ngx.say("HelloWorld")
}
}
location /myip {
default_type 'text/plain';
content_by_lua '
clientIP = ngx.req.get_headers()["x_forwarded_for"]
ngx.say("Forwarded_IP:",clientIP)
if clientIP == nli then
clientIP = ngx.var.remote_addr
ngx.say("Remote_IP:",clientIP)
end
';
}
location / {
default_type 'text/plain';
content_by_lua_file /soft/dep.lua;
}
location @java_prod {
proxy_pass http://java_prod;
include proxy_params;
}
location @java_test {
proxy_pass http://java_test;
include proxy_params;
}
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.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;
# }
#}
proxy_params
proxy_redirect default;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 32k;
proxy_buffering on;
proxy_buffers 4 128k;
proxy_busy_buffers_size 256k;
proxy_max_temp_file_size 256k;
memcacheのインストール:
yum install memcached -y
systemctl start memcached
systemctl enable memcached
key-value
telnet 192.168.23.164 11211
add
set
get
delete
stats
flush_all
luaスクリプト
-- x-real-ip
clientIP = ngx.req.get_headers()["X-Real-IP"]
-- IP - x_forwarded_for
if clientIP == nil then
clientIP = ngx.req.get_headers()["x_forwarded_for"]
end
-- IP - remote_addr
if clientIP == nil then
clientIP = ngx.var.remote_addr
end
-- , memcached
local memcached = require "resty.memcached"
--
local memc, err = memcached:new()
--
if not memc then
ngx.say("failed to instantiate memc: ", err)
return
end
-- memcache
local ok, err = memc:connect("127.0.0.1", 11211)
--
if not ok then
ngx.say("failed to connect: ", err)
return
end
-- ip- res
local res,flags,err = memc:get(clientIP)
--
--ngx.say("value key: ",res,clientIP)
if err then
ngx.say("failed to get clientIP ", err)
return
end
-- 1 local-@java_test
if res == "1" then
-- ngx.say("yes")
ngx.exec("@java_test")
return
end
-- local-@java_prod
ngx.exec("@java_prod")
return
JAvaとtomcatのインストール
yum install java -y
tomcat準備