1.下载安装
1.1下载
wget https://openresty.org/package/centos/openresty.repo sudo mv openresty.repo /etc/yum.repos.d/ sudo yum check-update
1.2安装
sudo yum install -y openresty sudo yum install -y openresty-resty
2.启动
2.1创建目录
mkdir -p /opt/data/openresty/www/{logs,conf}
2.2创建配置文件
[root@k8s01 opt] vi /opt/data/openresty/www/conf/nginx.conf worker_processes 1; error_log logs/error.log; events { worker_connections 1024; } http { lua_code_cache off; charset 'utf-8'; add_header Access-Control-Allow-Origin *; server { listen 9000; location / { default_type text/html; content_by_lua 'ngx.say("<p>Hello, World!</p>")'; } } }
2.3启动
[root@k8s01 opt]# /usr/local/openresty/nginx/sbin/nginx -p /opt/data/openresty/www/ -c /opt/data/openresty/www/conf/nginx.conf nginx: [alert] lua_code_cache is off; this will hurt performance in /opt/data/openresty/www/conf/nginx.conf:7 # =============================================================================== 或者 /usr/local/openresty/nginx/sbin/nginx -p `pwd`/ -c conf/nginx.conf # ==================================================================================
3.验证
3.1查看配置
[root@k8s01 lua]# openresty -V nginx version: openresty/1.21.4.1 built by gcc 10.2.1 20200804 (Red Hat 10.2.1-2) (GCC) built with OpenSSL 1.1.1n 15 Mar 2022 TLS SNI support enabled configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt='-O2 -DNGX_LUA_ABORT_AT_PANIC -I/usr/local/openresty/zlib/include -I/usr/local/openresty/pcre/include -I/usr/local/openresty/openssl111/include' --add-module=../ngx_devel_kit-0.3.1 --add-module=../echo-nginx-module-0.62 --add-module=../xss-nginx-module-0.06 --add-module=../ngx_coolkit-0.2 --add-module=../set-misc-nginx-module-0.33 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.09 --add-module=../srcache-nginx-module-0.32 --add-module=../ngx_lua-0.10.21 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.33 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.19 --add-module=../redis2-nginx-module-0.15 --add-module=../redis-nginx-module-0.3.9 --add-module=../ngx_stream_lua-0.0.11 --with-ld-opt='-Wl,-rpath,/usr/local/openresty/luajit/lib -L/usr/local/openresty/zlib/lib -L/usr/local/openresty/pcre/lib -L/usr/local/openresty/openssl111/lib -Wl,-rpath,/usr/local/openresty/zlib/lib:/usr/local/openresty/pcre/lib:/usr/local/openresty/openssl111/lib' --with-cc='ccache gcc -fdiagnostics-color=always' --with-pcre-jit --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_v2_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_auth_request_module --with-http_secure_link_module --with-http_random_index_module --with-http_gzip_static_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-threads --with-compat --with-stream --with-http_ssl_module
3.2访问80端口
3.3访问9000端口
至此,Openresty 已成功搭建。
4.目录结构优化
我们把lua代码放在nginx配置中会随着lua的代码的增加导致配置文件太长不好维护,因此我们应该把lua代码移到外部文件中存储
4.1单独存放lua文件
mkdir -p /opt/data/openresty/lua/ vi /opt/data/openresty/lua/test.lua local date=os.date('%Y-%m-%d %H:%M:%S'); local html = [[ <html> <head></head> <body> <a href="http://www.runoob.com/">hello world by lua!中文测试</a> </body> </html> ]]; ngx.say(html , date ); tab1 = { key1 = "val1", key2 = "val2", key3 ="val3" }; for k, v in pairs(tab1) do ngx.say(k .. " - " .. v); end
4.2修改配置文件
vi /opt/data/openresty/www/conf/nginx.confworker_processes 1;error_log logs/error.log;events {worker_connections 1024;}http {lua_code_cache off;charset 'utf-8';add_header Access-Control-Allow-Origin *;server {listen 9000;location / {default_type text/html;content_by_lua_file /opt/data/openresty/lua/test.lua;}}}
4.3重新加载配置
[root@k8s01 opt]# /usr/local/openresty/nginx/sbin/nginx -p /opt/data/openresty/www/ -c /opt/data/openresty/www/conf/nginx.conf -s reload nginx: [alert] lua_code_cache is off; this will hurt performance in /opt/data/openresty/www/conf/nginx.conf:7
4.4访问9000端口