openresty安装及配置,


资料准备

http://openresty.org/cn/download.html

centos 版本需要

参考资料:

https://moonbingbing.gitbooks.io/openresty-best-practices/content/ngx/if_is_evil.html

windows安装

下载windows版本的openresty:
解压启动nginx.exe:

双击nginx.exe运行

查看运行状态

tasklist /fi “imagename eq nginx.exe”

网页打开,默认的80端口
http://localhost

lua脚本的使用

在nginx.conf中配置lua脚本:
直接在nginx.conf中编写,正式环境一般是直接加载lua脚本所在的目录,这样分开存放脚本信息方便管理
在nginx.conf中引用lua脚本

location /lua { 
default_type 'text/html'; 
content_by_lua 'ngx.say("hello world")'; 
}

location /hello {
    content_by_lua_file lua/hello.lua;
}

hello.lua脚本

ngx.say(“hello,world!!!”)

nginx引用配置文件

修改nginx.conf,在server之前加入如下行

include  D:/Downloads/openresty-1.13.6.2-win64/lua/*.conf;

会自动引入lua目录下的所有conf配置文件到nginx中
配置规则:服务名.conf

这里在lua下创建lua.conf


server { 
	listen 81; 
 	server_name  localhost;


	location /lua { 
	default_type 'text/html'; 
	content_by_lua 'ngx.say("hello world")'; 
	}


	location /hello {
	    content_by_lua_file lua/hello.lua;
	}


}

重启nginx
/usr/servers/nginx/sbin/nginx -s reload

http://localhost:81/hello

## lua代码文件和缓存
1、lua_code_cache
默认情况下lua_code_cache 是开启的,即缓存lua代码,即每次lua代码变更必须reload nginx才生效,
如果在开发阶段可以通过lua_code_cache off;关闭缓存,这样调试时每次修改lua代码不需要reload nginx;但是正式环境一定记得开启缓存。

可以测试下,修改hello.lua中的输出内容,刷新网页但是并没有展现新的内容,这就是因为缓存默认开启了的原因

2、lua代码文件
我们把lua代码放在nginx配置中会随着lua的代码的增加导致配置文件太长不好维护,因此我们应该把lua代码移到外部文件中存储。

mkdir lua
cd lua
vim hello.lua 
#添加如下内容 
ngx.say("hello world by lua!");

然后lua.conf修改为:

location /lello { 
default_type 'text/html';
lua_code_cache off;
content_by_lua_file  lua/hello.lua;
}

3、重启nginx
/nginx -s reload
关闭缓存后会看到如下报警(忽略不管)
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/servers/nginx/conf/lua.conf:7

4、再次访问如http://10.0.2.81/lello(自己的机器根据实际情况换ip),可以看到如下内容:
hello world by lua!

可以多次修改内容后刷新,发现会立刻更新内容,不会缓存

5、错误日志
如果运行过程中出现错误,请不要忘记查看错误日志。
tail -f /usr/servers/nginx/logs/error.log

到此nginx+lua基本环境搭建完毕。

## 标准配置文件方式

编辑nginx.conf配置文件
vim /usr/servers/nginx/conf/nginx.conf
在http部分添加如下配置,根据需要添加你所需要的部分

http {
include mime.types; 
default_type text/html;

lua_package_path "/usr/servers/lualib/?.lua;;"; #依赖lua 模块 
lua_package_cpath "/usr/servers/lualib/?.so;;"; #依赖c模块 
include /mnt/lua/*.conf; #单独lua配置,*表示所有的相关nginx配置都会加载进去
} 

#lua模块路径,多个之间”;”分隔,其中”;;”表示默认搜索路径,默认到/usr/servers/nginx下找

nginx配置检查和重启

测试配置信息是否正常
root@user:/usr/servers/nginx/conf# /usr/servers/nginx/sbin/nginx -t
如果显示如下内容说明配置成功
nginx: the configuration file /usr/servers/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/servers/nginx/conf/nginx.conf test is successful
如果运行过程中出现错误,请不要忘记查看错误日志。
tail -f /usr/servers/nginx/logs/error.log

重启nginx
/usr/servers/nginx/sbin/nginx -s reload

一般工作目录如下
/mnt/lua/conf/.conf lua项目相关的nginx配置信息
/mnt/lua/server/
.lua lua提供服务的源码脚本地址

### 配置信息
nginx.conf


#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;


    include  D:/Downloads/openresty-1.13.6.2-win64/lua/*.conf;


    server {
        listen       80;
        server_name  localhost;

        #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_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;
    #    }
    #}

}

lua.conf


server { 
	listen 81; 
 	server_name  localhost;


	location /lua { 
	default_type 'text/html'; 
	content_by_lua 'ngx.say("hello world")'; 
	}


	location /hello {
	default_type 'text/html'; 
	lua_code_cache off;
	content_by_lua_file lua/hello.lua;
	}


}


相关内容

    暂无相关文章