OpenRestry实战三:idea openrestry搭建企业级开发环境

准备工作:

idea 2018

jdk 1.8

lua插件:idea-nginx-support-0.1.6.zip  Lua-1.0.119.zip openrestyLuaSupport-0.0.4.zip

openrestry web容器:openresty-1.15.8.2-win64.zip

idea安装lua插件:参考https://blog.csdn.net/u014079773/article/details/101062867

idea openrestry+nginx+lua搭建企业级开发环境:

 

工程创建好后在根目录下创建build.xml,在conf目录下创建nginx.conf,工程结构如下:

build.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!--工程名snms-->
<project name="snms" default="dist" basedir=".">
    <description>
        run snms
    </description>
    <!-- set global properties for this build -->
    <!--openresty-home的location为本地openresty安装路径-->
    <property name="openresty-home" location="E:\openresty-1.15.8.2-win64"/>
    <!--conf为当前工程存放配置文件的文件夹名-->
    <property name="conf" location="${basedir}/conf"/>
    <!--src为当前工程存放lua文件的文件夹名-->
    <property name="src" location="${basedir}/src"/>
    <property name="target-conf" location="${openresty-home}/conf"/>
    <property name="target-src" location="${openresty-home}/src"/>

    <echo>######开发版本的ant配置#####</echo>
    <target name="clean" depends="">
        <echo>清理openresty目录 ${dist}下的conf,logs,janus,januslib</echo>
        <delete dir="${target-conf}"/>
        <delete dir="${target-src}"/>
        <delete>
            <fileset dir="${openresty-home}/logs" includes="*.log"></fileset>
        </delete>
    </target>

    <target name="init" depends="clean">
        <echo>创建安装目录</echo>
        <mkdir dir="${target-conf}"/>
        <mkdir dir="${target-src}"/>
    </target>

    <target name="dist" depends="init" description="generate the distribution" >
        <echo>复制安装文件</echo>
        <copy todir="${target-conf}">
            <fileset dir="${conf}"></fileset>
        </copy>
        <copy todir="${target-src}">
            <fileset dir="${src}"></fileset>
        </copy>
    </target>

</project>

nginx.conf文件:

#user  root;#定义Nginx运行的用户和用户组
worker_processes  auto;#表示工作进程的数量,一般设置为cpu的核数
#worker_rlimit_nofile	65535;#指定一个nginx进程可以打开的最多文件描述符数目
#全局错误日志及PID文件,warn是错误日志级别
#错误日志定义等级,[ debug | info | notice | warn | error | crit ]
error_log  logs/error.log warn;
#pid       logs/nginx.pid;#log日志进程文件

#工作模式及连接数上限
events {
    worker_connections  51200;#表示每个工作进程的最大连接数
    multi_accept on;#尽可能多的接受请求
    #use epoll;#epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
}

#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
    include      mime.types;#设定mime类型,类型由mime.type文件定义
    default_type  text/html;#请求默认返回格式
    #设定日志格式
    log_format  main  '$remote_addr\t$http_x_forwarded_for\t-\t$remote_user\t$time_iso8601\t$request_method\t"$document_uri"\t"$query_string"\t$server_protocol\t$status\t$body_bytes_sent\t$request_time\t"$http_referer"\t"$http_user_agent"\t$http_Cdn-Src-Ip\t$host\t$hostname\t$server_addr\t-\t-\t-\t-\t-\tV5';
    access_log  logs/access.log  main; #设定本虚拟主机的访问日志
    sendfile    on;#指定nginx是否调用sendfile函数(zero copy 方式)来输出文件,对于普通应用必须设为 on
    tcp_nopush on; #防止网络阻塞
    tcp_nodelay on; #提高数据的实时响应性
    #超时时间,客户端到服务器端的连接持续有效时间,单位是秒
    keepalive_timeout  30;

    gzip  on;#开启gzip压缩
    gzip_vary on;
    gzip_min_length  1k;
    gzip_buffers     4  8k;
    gzip_comp_level 1;#压缩级别大小,最大为9,值越小压缩后比例越小CPU处理更快,值越大消耗CPU比较高。
    gzip_types       text/plain application/x-javascript text/css text/htm application/xml application/javascript text/javascript;
    gzip_http_version 1.1;
    proxy_buffering off;

    #打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。
    open_file_cache max=51200 inactive=20s;
    #多长时间检查一次缓存的有效信息。
    open_file_cache_valid 30s;
    #open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive
    open_file_cache_min_uses 2;
    open_file_cache_errors on;
    lua_shared_dict logCache 100m;#缓存

    #lua模块路径,多个之间”;”分隔,其中”;;”表示默认搜索路径。windows下相对地址,linux下绝对路径
    lua_package_path 'src/?.lua;;src/app/?.lua';
    #lua_package_cpath '/usr/local/lib/lua/5.1/?.so;/usr/local/lib/?.so;;';#C语言依赖库
    #初始化脚本:windows下相对地址,linux下绝对路径
    init_by_lua_file  src/init.lua;

    #负载均衡配置,tdt_wugk是负载均衡名称
    #upstream tdt_wugk {
        #server   127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;
        #server   127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s;
    #}

    #虚拟主机配置
    server {
        listen 80;#监听端口,默认80端口号
        server_name  localhost;#监听域名
        proxy_set_header	X-Real-IP	$remote_addr;
        proxy_set_header	X-Forwarded-For	$proxy_add_x_forwarded_for;
        #设置nginx变量
        set $APP_PATH ''; #设置app请求路径

        #通用匹配,任何未匹配到其它location的请求都会匹配到,相当于switch中的default
        location / {
            root   html;#指定对应uri的资源查找路径,这里html为相对路径;
            index  index.html index.htm;#指定首页index文件的名称,可以配置多个以空格分开,如有多个按配置顺序查找。
        }

        #精确匹配
        location = /favicon.ico {
            log_not_found off;
        }

        #设定查看Nginx状态的地址
        #不带任何修饰符前缀匹配,但是在正则匹配之后
        location /Nginx-status {
             stub_status  on;
             access_log   off;
        }

        #不带任何修饰符前缀匹配,但是在正则匹配之后
        location /sumis-web/ {
            internal;#此处表示属于内部调用,外部无法直接访问
            #echo_after_body "$host";
            #proxy_buffering             off;
            #proxy_set_header            Host mois.suning.com;
            #proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
            #proxy_redirect              off;
            proxy_connect_timeout        1;
            #proxy_set_header            Connection "";
            #proxy_http_version          1.1;
            #proxy_send_timeout          30;
            #proxy_read_timeout          30;
            proxy_set_header             Accept-Encoding "";
            #proxy_pass_request_headers   off;
            proxy_pass http://sumfsdev.cnsuning.com;#动态代理域名
        }

        #^~ 开头对URL路径进行前缀匹配并且在正则之前
        location ^~ /api/ {
            charset UTF-8;
            default_type text/html;
            #设置nginx变量
            set $ROUTE_PATH 'route';#//route.lua的文件返回的函数名
            content_by_lua_file '${APP_PATH}/src/lua_star.lua';
        }

        #定义错误提示页面
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

test.lua:

module(..., package.seeall)

function test(ngxObj)
    ngx.say("------------ START ------------");
    ngx.say("This is my test lua");
    ngx.say("package path \n"..package.path);
    ngx.say("------------ END ------------");
end

Ant build添加build.xml

idea配置nginx server:

启动项目验证是否正常:

浏览器输入:http://localhost/api/test.json

工程下载地址:https://download.csdn.net/download/u014079773/11826092

备注:

1.所有app路径请求读取route.lua文件的url地址:

#^~ 开头对URL路径进行前缀匹配并且在正则之前
location ^~ /api/ {
   charset UTF-8;
   default_type text/html;
   #设置nginx变量
   set $ROUTE_PATH 'route';#//route.lua的文件返回的函数名
   content_by_lua_file '${APP_PATH}/src/lua_star.lua';
}

2.lua_star.lua读取route.lua文件所有url,若请求正常则正常返回,否则返回404

3.utils.lua:工具类

4.init.lua:初始化脚本

5.route.lua:存放所有app请求,若再写一个请求则在route里面追加即可:

--[[
 这里存储app所有请求url
 案例:["/api/test.json"]={"app.test", "test"}
 /api/test.json  表示请求url
 app.test  请求url的lua文件路径
 test  请求url的方法名
-- ]]

route = {
	["/api/test.json"]={"app.test", "test"},
    ["/api/category/queryCategory.json"]={"app.category", "queryCategory"},
    ["/api/category/queryCate.json"]={"app.category", "queryCate"}

}
return route

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值