nginx+lua(openresty) 安装及使用(一)

前言
OpenResty —— 通过 Lua 扩展 NGINX 实现的可伸缩的 Web 平台。
OpenResty(也称为 ngx_openresty)是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。
OpenResty 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发,),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。

1:准备
下载地址: http://openresty.org/cn/download.html ( 这里下载是win64版最新版)
在这里插入图片描述
2:配置
openresty-1.21.4.1-win64\conf\nginx.conf 拷贝个 改名 nginx_debug.conf 这样方便测试

worker_processes  1;
error_log logs/error.log;
events {
    
    
    worker_connections 1024;
}
http {
    
    
	lua_package_path "j:/temp/nginx_lua/openresty-1.21.4.1-win64/src_lua/?.lua;j:/temp/nginx_lua/openresty-1.21.4.1-win64/lualib/?.lua;;"; #lua模块
	lua_package_cpath "j:/temp/nginx_lua/openresty-1.21.4.1-win64/lualib/?.so;;"; #c模块
    server {
    
    
        #listen 127.0.0.1:9002;
		listen 0.0.0.0:9002;
		lua_code_cache off; ###lua_code_cache on;#调试模式(即关闭lua脚本缓存)  //生产环境 on  #修改lua文件不需要重新,立即生效
		
		keepalive_timeout 60s;  # 配置段: http, server, location 指定每个 TCP 连接最多可以保持多长时间。Nginx 的默认值是 75 秒,
		#有些浏览器最多只保持 60 秒,所以可以设定为 60 秒。若将它设置为 0,就禁止了 keepalive 连接。
        client_body_timeout 20s;# 配置段: http, server, location 指定客户端与服务端建立连接后发送 request body 的超时时间。
		#如果客户端在指定时间内没有发送任何内容,Nginx 返回 HTTP 408(Request Timed Out)
		client_header_timeout 10s;# 配置段: http, server, location  客户端向服务端发送一个完整的 request header 的超时时间。
		#如果客户端在指定时间内没有发送一个完整的 request header,Nginx 返回 HTTP 408(Request Timed Out)
		  
		location /lua {
    
    
		  #根目录j:/temp/nginx_lua/openresty-1.21.4.1-win64
           content_by_lua_file src_lua/HelloLua.lua; #相对 根目录的 路径
        }
		
		location /testlua {
    
    
		proxy_set_header            X-real-ip $remote_addr;                                           # 可直接获取客户端IP
		proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;    # 通过代理服务器获取客户端IP
			#lua_code_cache off; ###lua_code_cache on;#调试模式(即关闭lua脚本缓存)  //生产环境 on
           content_by_lua_file src_lua/http.lua;
        }
		
		
		location /testluapost {
    
    
			#lua_code_cache off; ###lua_code_cache on;#调试模式(即关闭lua脚本缓存)  //生产环境 on
           content_by_lua_file src_lua/http_post.lua;
        }
		
		location /test {
    
    
           # default_type text/html;
            content_by_lua '
                ngx.say("<p>Hello, World!</p>")
            ';
        }
    }
}

在这里插入图片描述
3:运行
http.lua

local function getrealip()
	local headers=ngx.req.get_headers()
   local ip=headers["X-REAL-IP"] or headers["X_FORWARDED_FOR"] or ngx.var.remote_addr or "0.0.0.0"
    return ip
end
local function main()
	local realip =  getrealip()
	ngx.say("realip:",realip)
	ngx.say("http.lua_4")
    
   
   local arg = ngx.req.get_uri_args()

            for k, v in pairs(arg) do

                 ngx.say(k .. "-" ..v)

    end
end
main()

nginx -p ./ -c conf/nginx_debug.conf #启动
//重启Nginx进程,发送reload信号
nginx -p ./ -c nginx-debug.conf -s reload
//停止nginx进程,发送stop信号
nginx -p ./ -c nginx-debug.conf -s stop
在这里插入图片描述
浏览器执行
http://127.0.0.1:9002/testlua?name=aaa&age=12&test=‘test’

在这里插入图片描述

4:后续
以此为基础,改造下php的登录

如果觉得有用,麻烦点个赞,加个收藏

猜你喜欢

转载自blog.csdn.net/yunteng521/article/details/131074894