热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Nginx+lua+redis搭建一个高并发的服务器

http://www.ttlsa.com/html/1283.htmlnginx+lua+redis构建高并发应用ngx_lua将lua嵌入到nginx,让nginx执行lua脚本,高并发,非阻塞的处理各种请求。url请求nginx服务器,然后lua查询redis,返回json数据。一.安装lua#apt-getins

http://www.ttlsa.com/html/1283.html

nginx+lua+redis构建高并发应用

ngx_lua将lua嵌入到nginx,让nginx执行lua脚本,高并发,非阻塞的处理各种请求。

url请求nginx服务器,然后lua查询redis,返回json数据。

一.安装lua

# apt-get install lua5.1

# apt-get install liblua5.1-dev

# apt-get install liblua5.1-socket2

 

二.安装nginx

# apt-get install git-core

# git clone https://github.com/simpl/ngx_devel_kit.git

# git clone https://github.com/chaoslawful/lua-nginx-module.git

# git clone https://github.com/agentzh/redis2-nginx-module.git

# git clone https://github.com/agentzh/set-misc-nginx-module.git

# git clone https://github.com/agentzh/echo-nginx-module.git

# git clone https://github.com/catap/ngx_http_upstream_keepalive.git

# apt-get install libpcre3 libpcre3-dev libltdl-dev libssl-dev libjpeg62 libjpeg62-dev libpng12-0 libpng12-dev libxml2-dev libcurl4-openssl-dev libmcrypt-dev autoconf libxslt1-dev libgd2-noxpm-dev libgeoip-dev libperl-dev -y

# wget http://nginx.org/download/nginx-1.0.8.tar.gz

# tar zxvf nginx-1.0.8.tar.gz

# cd nginx-1.0.8

# ./configure ?prefix=/usr/local/nginx ?with-debug ?with-http_addition_module \

?with-http_dav_module ?with-http_flv_module ?with-http_geoip_module \

?with-http_gzip_static_module ?with-http_image_filter_module ?with-http_perl_module \

?with-http_random_index_module ?with-http_realip_module ?with-http_secure_link_module \

?with-http_stub_status_module ?with-http_ssl_module ?with-http_sub_module \

?with-http_xslt_module ?with-ipv6 ?with-sha1=/usr/include/openssl \

?with-md5=/usr/include/openssl ?with-mail ?with-mail_ssl_module \

?add-module=../ngx_devel_kit \

?add-module=../echo-nginx-module \

?add-module=../lua-nginx-module \

?add-module=../redis2-nginx-module \

?add-module=../ngx_http_upstream_keepalive \

?add-module=../set-misc-nginx-module

# make

# make install

 

三.安装lua-redis-parser

# git clone https://github.com/agentzh/lua-redis-parser.git

# export LUA_INCLUDE_DIR=/usr/include/lua5.1

# make CC=gcc

# make install CC=gcc

 

四.安装json

# wget http://files.luaforge.net/releases/json/json/0.9.50/json4lua-0.9.50.zip

# unzip json4lua-0.9.50.zip

# cp json4lua-0.9.50/json/json.lua /usr/share/lua/5.1/

 

五.安装redis-lua

# git clone https://github.com/nrk/redis-lua.git

# cp redis-lua/src/redis.lua /usr/share/lua/5.1/

 

六.配置

user www-data;

worker_processes 8;

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

error_log logs/error.log notice;

pid logs/nginx.pid;

worker_rlimit_nofile 60000;

events {

worker_connections 1024;

use epoll;

}

http {

include mime.types;

default_type application/octet-stream;

access_log logs/access.log;

sendfile on;

tcp_nopush on;

tcp_nodelay on;

keepalive_timeout 60;

types_hash_max_size 2048;

server_tokens off;

lua_code_cache on;

upstream redis_pool {

server 192.168.1.39:6379;

keepalive 1024 single; //定义连接池大小,当连接数达到此数后,后续的连接为短连接

}

server {

listen 80;

server_name 192.168.1.211;

location /get_redis{

#internal;

set_unescape_uri $key $arg_key;

redis2_query hgetall $key;

redis2_pass redis_pool;

}

location /json {

content_by_lua_file conf/fuck.lua;

}

}

}

 

# vim fuck.lua

local json = require(“json”)

local parser = require(“redis.parser”)

local res = ngx.location.capture(“/get_redis”,{

args = { key = ngx.var.arg_key }

})

if res.status == 200 then

reply = parser.parse_reply(res.body)

value = json.encode(reply)

ngx.say(value)

a = json.decode(value)

ngx.say(a[2])

end

 

七.测试

# redis-cli -h 192.168.1.39

redis 192.168.1.39:6379> HMSET ttlsa www www.ttlsa.com mail mail.ttlsa.com

OK

# curl ‘http://192.168.1.211/json?key=ttlsa’

["www","www.ttlsa.com","mail","mail.ttlsa.com"]

www.ttlsa.com


推荐阅读
  • Nginx Buffer 机制引发的下载故障
    Nginx ... [详细]
  • Nginx使用AWStats日志分析的步骤及注意事项
    本文介绍了在Centos7操作系统上使用Nginx和AWStats进行日志分析的步骤和注意事项。通过AWStats可以统计网站的访问量、IP地址、操作系统、浏览器等信息,并提供精确到每月、每日、每小时的数据。在部署AWStats之前需要确认服务器上已经安装了Perl环境,并进行DNS解析。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • iOS超签签名服务器搭建及其优劣势
    本文介绍了搭建iOS超签签名服务器的原因和优势,包括不掉签、用户可以直接安装不需要信任、体验好等。同时也提到了超签的劣势,即一个证书只能安装100个,成本较高。文章还详细介绍了超签的实现原理,包括用户请求服务器安装mobileconfig文件、服务器调用苹果接口添加udid等步骤。最后,还提到了生成mobileconfig文件和导出AppleWorldwideDeveloperRelationsCertificationAuthority证书的方法。 ... [详细]
  • 云原生应用最佳开发实践之十二原则(12factor)
    目录简介一、基准代码二、依赖三、配置四、后端配置五、构建、发布、运行六、进程七、端口绑定八、并发九、易处理十、开发与线上环境等价十一、日志十二、进程管理当 ... [详细]
  • Linux下部署Symfoy2对app/cache和app/logs目录的权限设置,symfoy2logs
    php教程|php手册xml文件php教程-php手册Linux下部署Symfoy2对appcache和applogs目录的权限设置,symfoy2logs黑色记事本源码,vsco ... [详细]
  • 现在比较流行使用静态网站生成器来搭建网站,博客产品着陆页微信转发页面等。但每次都需要对服务器进行配置,也是一个重复但繁琐的工作。使用DockerWeb,只需5分钟就能搭建一个基于D ... [详细]
  • 有意向可以发简历到邮箱内推.简历直达组内Leader.能做同事的话,内推奖励全给你. ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 篇首语:本文由编程笔记#小编为大家整理,主要介绍了软件测试知识点之数据库压力测试方法小结相关的知识,希望对你有一定的参考价值。 ... [详细]
  • 本文整理了Java中java.lang.NoSuchMethodError.getMessage()方法的一些代码示例,展示了NoSuchMethodErr ... [详细]
  • Redis的默认端口、数据库使用和多端口配置
    本文介绍了Redis的默认端口、数据库使用和多端口配置的方法。通过选择不同的数据库和使用flushdb命令可以实现对不同数据库的访问和清除数据。同时,本文还介绍了在同一台机器上启用多个Redis实例的方法,并讨论了配置认证密码的步骤和注意事项。 ... [详细]
  • centos6.8 下nginx1.10 安装 ... [详细]
author-avatar
imba-Y_685
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有