tyltr技术窝

1.环境说明#

2.安装#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
tar xzvf  ngx_openresty-1.7.10.1.tar.gz
cd ngx_openresty-1.7.10.1
# 使用brew安装依赖,
# 在本版本的mac上,把依赖安装到了/usr/local/opt/下了,在编译时指定的依赖路径就是依赖安装的目录
brew install pcre openssl

# 编译 --with-cc-opt --with-ld-opt 依赖的路径
# --prefix 指定openresty的安装目录
# -j4 表示使用4核cup去执行编译工作
./configure \
--with-cc-opt="-I/usr/local/opt/openssl/include/ -I/usr/local/opt/pcre/include/" \
--with-ld-opt="-L/usr/local/opt/openssl/lib/ -L/usr/local/opt/pcre/lib/" \
--prefix=/opt/openresty \
-j4

make -j4
sudo make install

进入openresty的安装目录

1
2
3
4
5
6
7
8
cd /opt/openresty

ls
# 查看目录结构,四个目录
# bin luajit lualib nginx

# 启动openresty,即启动其NGINX
sudo /opt/openresty/nginx/sbin/nginx -c /opt/openresty/nginx/conf/nginx.conf -p /opt/openresty/nginx

可访问80端口进行验证,或者使用 ps -ef | grep nginx 进行验证

3. 简单的lua文件 hello world#

在编写lua文件时,建议使用 sublime 进行编写。

打开目录 /opt/openresty/nginx

默认NGINX的配置,本机配置文件 /opt/openresty/nginx/conf/nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#user  nobody;

# 只启动一个worker进程
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;

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

}

新建一个location,指定lua脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

#user nobody;

# 只启动一个worker进程
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;

server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
}

# 新增 hello 路径,通过content_by_lua_file,指定一个lua脚本
# 一旦用户请求/hello, nginx就会运行 lua/hello.lua 脚本
location /hello {
content_by_lua_file lua/hello.lua;
}



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

}

nginx目录下,新建 lua目录,并且在 lua目录中创建 hello.lua

nginx

  • conf
    • fastcgi.conf
    • fastcgi.conf.default
    • fastcgi_params
    • fastcgi_params.default
    • koi-utf
    • koi-win
    • mime.types
    • mime.types.default
    • nginx.conf
    • nginx.conf.default
    • scgi_params
    • scgi_params.default
    • uwsgi_params
    • uwsgi_params.default
    • win-utf
  • logs
    • access.log
    • error.log
    • nginx.pid
  • lua
    • hello.lua
      ……..

配置文件修改了,需要重新加载。

1
sudo  /opt/openresty/nginx/sbin/nginx -c  /opt/openresty/nginx/conf/nginx.conf -p /opt/openresty/nginx   -s  reload

执行 curl localhost:80/hello 进行验证lua脚本

4.注意#

4.1 热加载lua脚本#

在上面的操作的中,可以看到 每次更改文件都需要重新reload。太麻烦了!

在开发调试时,希望有一种自动加载的机制,每次修改后都自动加载。可以通过配置解决这个问题

需要 /opt/openresty/nginx/conf/nginx.conf 文件中server添加配置 lua_code_cache off; 即可实现

1
2
3
4
5
6
7
8
9
server {
listen 80;
server_name localhost;

lua_code_cache off;

#charset koi8-r;

......

但是只能在开发环境中,这样(*lua_code_cache off *)配置。在线上环境中,一定要开启,否则会影响性能。

重新加载配置文件,会提示性能警告:

1
nginx: [alert] lua_code_cache is off; this will hurt performance in /opt/openresty/nginx/conf/nginx.conf:38