nginx 中添加 lua 模块,支持lua脚本以及遇到的坑

本文详细介绍了在Nginx中安装Lua模块的过程,包括LuaJIT、ngx_devel_kit和lua-nginx-module的下载、配置及编译安装步骤。同时,针对安装过程中可能遇到的问题,如版本冲突、库文件缺失等提供了排错技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考文档:https://www.cnblogs.com/yulibostu/articles/10529989.html
工具版本说明

nginxluajitngx_devel_kitlua-nginx-module备注
V1.17.5V2.0.4v0.3.0v0.10.9rc7强烈建议,除了nginx版本之外,其他工具版本都使用跟我这个一样的,我是各种组合版本试错的过来人

一、安装带lua模块的nginx

nginx上面一共需要安装3个东西:luajit,ngx_devel_kit,lua-nginx-module

1.下载安装LuaJIT-2.0.4.tar.gz

这里有点小坑,从luajit.org中下载的luaJIT nginx启动会报warn,可以看后面的排坑技巧

cd /data/soft/nginx

wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz

tar xzvf LuaJIT-2.0.4.tar.gz

cd LuaJIT-2.0.4

make install PREFIX=/usr/local/luajit

 
#添加环境变量!

export LUAJIT_LIB=/usr/local/luajit/lib

export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0

2.下载解压ngx_devel_kit

cd /data/soft/nginx

wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz

tar -xzvf v0.3.0.tar.gz

3.下载解压lua-nginx-module

这里是重点啊!!!很多坑都是这个家伙带来的
一定要用v0.10.9rc7版本,我试了其他几个版本,比如说v0.10.8v0.10.15等,都是不行的,都会遇到下面说的那些坑,只有v0.10.9rc7不会。

cd /data/soft/nginx

wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz

tar -xzvf v0.10.9rc7.tar.gz

4.下载nginx,编译安装

cd /data/soft/nginx

wget http://nginx.org/download/nginx-1.17.5.tar.gz

tar -xzvf nginx-1.17.5.tar.gz

我用的就是1.17.5这个版本的,我还有其他机子用的1.15.3,最好不要用太低的版本吧。。。

然后接着。

cd /data/soft/nginx/nginx-1.17.5

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-pcre --add-module=/data/soft/nginx/lua-nginx-module-0.10.9rc7 --add-module=/data/soft/nginx/ngx_devel_kit-0.3.0 --with-stream

make

make install

如果是在原有nginx上面加lua模块的话,记得只执行make就够了哦,不要执行make install,不然会覆盖原有的东西。

好了好了 ,搞完了。你试试看会不会报错。。反正我是没问题了。。报错报的脑壳痛。。

算了再写一个验证lua模块的代码吧。。

5.验证lua模块

vim /usr/local/nginx/conf/nginx.conf

###在server模块里面,随便找个地方添加下面的代码
location /lua {
            default_type 'text/plain';

            content_by_lua 'ngx.say("hello, lua")';
        }
        

在这里插入图片描述
然后

/usr/local/nginx/sbin/nginx -t

/usr/local/nginx/sbin/nginx -s reload

最后验证

curl 127.0.0.1:5580/lua

在这里插入图片描述

二.安装过程中遇到的各种坑

1.让我不要用这个luajit版本,可以用openresty提供的luajit优化版本,或者干脆直接用openresty

[alert] 12687#0: detected a LuaJIT version which is not OpenResty's; many optimizations will be disabled and performance will be compromised (see https://github.com/openresty/luajit2 for OpenResty's LuaJIT or, even better, consider using the OpenResty releases from https://openresty.org/en/download.html)

在这里插入图片描述
这个倒问题不大,是个warn而已,需要卸载之前的luajit,到他说的网站去下载。
先卸载原有luagit

cd /data/soft/nginx/LuaJIT-2.0.4
make uninstall

然后从https://github.com/openresty/luajit2下载安装

git clone https://github.com/openresty/luajit2
cd luajit2
make && make install

剩下的步骤跟一开始装luajit是一样的。具体的看自己的目录写。

export LUAJIT_LIB=/usr/local/lib

export LUAJIT_INC=/usr/local/include/luajit-2.1

然后重新编译nginx,懒得写了。。。跟前面一样

2. 说resty.core模块找不到

明明用起来是没啥问题的,但是他就是启动的时候报错,我都想骂人了。

[error] 12687#0: lua_load_resty_core failed to load the resty.core module from https://github.com/openresty/lua-resty-core; ensure you are using an OpenResty release from https://openresty.org/en/download.html (rc: 2, reason: module 'resty.core' not found:
	no field package.preload['resty.core']
	no file '/data/soft/soft_package/nginx/sockproc/lua-resty-shell/lib/resty/core.lua'
	no file './resty/core.lua'
	no file '/usr/local/LuaJIT/share/luajit-2.1.0-beta3/resty/core.lua'
	no file '/usr/local/share/lua/5.1/resty/core.lua'
	no file '/usr/local/share/lua/5.1/resty/core/init.lua'
	no file '/usr/local/LuaJIT/share/lua/5.1/resty/core.lua'
	no file '/usr/local/LuaJIT/share/lua/5.1/resty/core/init.lua'
	no file './resty/core.so'
	no file '/usr/local/lib/lua/5.1/resty/core.so'
	no file '/usr/local/LuaJIT/lib/lua/5.1/resty/core.so'
	no file '/usr/local/lib/lua/5.1/loadall.so'
	no file './resty.so'
	no file '/usr/local/lib/lua/5.1/resty.so'
	no file '/usr/local/LuaJIT/lib/lua/5.1/resty.so'
	no file '/usr/local/lib/lua/5.1/loadall.so')

在这里插入图片描述
这个问题到git上面看了 ,差不多是这个地方吧:https://github.com/openresty/lua-nginx-module/issues/1509
就是在nginx.conf 中的 http{}模块中加入下面这行代码:

lua_load_resty_core off;

之后nginx reload就不会报那个烦人的错了。

3. error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory

error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory

这个错误好说,网上说了很多,有两种方法:
方法1:
执行下面的代码(注意前面的路径是你自己安装的luajit路径,正常安装跟我这个路径不一样哦)

ln -s /usr/local/luajit/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2

方法2:
执行下面的代码(同样还是注意前面的路径)

echo "/usr/local/luajit/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
<think>好的,用户想知道如何在Android Java项目中使用Retrofit构建网络请求模块。首先,我得回忆一下Retrofit的基本用法和相关依赖。记得Retrofit是基于OkHttp的,所以需要添加这两个库的依赖,可能还有Gson转换器。 用户的问题集中在搭建步骤,所以应该分步骤说明。首先应该是添加依赖,然后创建数据模型类,接着定义API接口,再配置Retrofit实例,最后发起网络请求。还要注意示例代码的正确性,确保用户能看懂。 需要检查是否有遗漏的步骤,比如网络权限或者ProGuard配置,但可能用户是新手,暂时不需要太复杂的配置。另外,引用中的代码片段提到Hilt和RxJava,但用户的问题可能不需要这些高级功能,所以保持基础示例即可。 还要考虑用户可能的错误,比如忘记添加转换器导致解析失败,或者接口定义不正确。在回答中提醒注意事项,比如注解的使用和baseUrl的正确格式。 最后,生成相关问题时要围绕Retrofit的扩展功能、异常处理、Hilt整合以及与其他库的对比,帮助用户深入学习和解决问题。</think>在Android Java项目中使用Retrofit构建网络请求模块的步骤如下: ### 一、添加依赖 在app模块的build.gradle中添加: ```groovy dependencies { implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' // Gson转换器 implementation 'com.squareup.okhttp3:logging-interceptor:4.9.3' // 日志拦截器 } ``` ### 二、创建数据模型 ```java // 响应数据结构 public class ApiResponse<T> { private int errorCode; private T data; // getters & setters } // 具体业务数据模型 public class User { private String username; private String email; // getters & setters } ``` ### 三、定义API接口 ```java public interface ApiService { @POST("/user/login") @FormUrlEncoded Call<ApiResponse<User>> login( @Field("username") String username, @Field("password") String password ); } ``` ### 四、配置Retrofit实例 ```java public class RetrofitClient { private static Retrofit retrofit; public static Retrofit getInstance() { if (retrofit == null) { OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)) .build(); retrofit = new Retrofit.Builder() .baseUrl("https://api.example.com/") .client(client) .addConverterFactory(GsonConverterFactory.create()) .build(); } return retrofit; } } ``` ### 五、发起网络请求 ```java ApiService service = RetrofitClient.getInstance().create(ApiService.class); Call<ApiResponse<User>> call = service.login("admin", "123456"); call.enqueue(new Callback<ApiResponse<User>>() { @Override public void onResponse(Call<ApiResponse<User>> call, Response<ApiResponse<User>> response) { if (response.isSuccessful()) { User user = response.body().getData(); // 处理成功逻辑 } } @Override public void onFailure(Call<ApiResponse<User>> call, Throwable t) { // 处理失败逻辑 } }); ``` ### 注意事项 1. 接口方法必须使用注解(@GET/@POST等)声明请求方式 2. Base URL必须以`/`结尾,接口路径不应以`/`开头[^2] 3. 需要添加网络权限: ```xml <uses-permission android:name="android.permission.INTERNET"/> ``` ### 扩展功能 通过添加Converter Factory可支持XML、Protobuf等格式[^3],结合RxJava可实现响应式编程: ```java .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) ```
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值