openresty安装及配置

本文详细介绍了如何在OpenResty中集成Lua脚本,包括资料准备、CentOS和Windows下的安装步骤、Lua脚本的使用、配置文件的引用方式、标准配置文件的设置,以及如何管理和调试Lua代码。

资料准备

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


}


<think>我们正在讨论如何重新安装OpenResty并编译配置文件。根据引用,用户可能已经尝试过安装,但需要重新安装配置。以下是步骤: 1. **下载和解压源代码**(参考引用[4]): ```bash wget https://openresty.org/download/openresty-1.25.3.2.tar.gz tar -zxvf openresty-1.25.3.2.tar.gz cd openresty-1.25.3.2 ``` 2. **配置编译选项**(参考引用[1][5]): 使用`./configure`命令,并指定所需的模块和安装路径。例如: ```bash ./configure --prefix=/usr/local/openresty \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-pcre \ --with-luajit ``` 注意:这里可以根据需要添加或删除模块。例如,如果不需要HTTP/2,可以去掉`--with-http_v2_module`。 3. **编译和安装**: ```bash make sudo make install ``` 4. **配置环境**(参考引用[2][3]): 安装完成后,OpenResty将被安装到`--prefix`指定的目录(例如`/usr/local/openresty`)。然后,需要设置环境变量,以便可以全局使用OpenResty命令: ```bash echo 'export PATH=/usr/local/openresty/nginx/sbin:$PATH' >> ~/.bashrc source ~/.bashrc ``` 5. **创建配置文件**(参考引用[3]): 默认情况下,配置文件位于`/usr/local/openresty/nginx/conf/nginx.conf`。你可以编辑这个文件来配置服务器。 如果需要自定义配置目录,可以在编译时通过`--conf-path`指定,但通常我们直接修改默认配置文件。 6. **启动OpenResty**(参考引用[3]): ```bash nginx -p /usr/local/openresty/nginx/ ``` 或者,如果你已经将nginx可执行文件路径加入环境变量,可以直接运行: ```bash nginx ``` 7. **验证安装**: 访问服务器IP或域名,或者使用`curl localhost`查看默认页面(如果没有修改默认配置,则监听80端口)。 8. **重载配置**(参考引用[3]): 修改配置文件后,使用以下命令重载配置(不重启服务): ```bash nginx -s reload ``` 注意:在重新安装之前,如果之前已经安装OpenResty,建议先停止旧服务: ```bash nginx -s stop ``` 或者如果使用systemd管理,则: ```bash sudo systemctl stop openresty ``` 如果之前是通过包管理器(如yum)安装的,建议先卸载: ```bash sudo yum remove openresty ``` 以上步骤是基于源代码编译安装OpenResty的标准流程。用户可以根据自己的需求调整编译选项。 注意:编译选项中的模块需要根据实际需求选择。例如,如果需要使用SSL,则必须包含`--with-http_ssl_module`;如果需要支持HTTP/2,则包含`--with-http_v2_module`。 另外,引用[5]中提到了更多的模块,如`--with-stream`(用于TCP/UDP代理)、`--with-http_flv_module`(用于流媒体)等,用户可以根据需要添加。 如果遇到依赖问题(如缺少PCRE、OpenSSL等开发包),请先安装这些依赖。在Ubuntu上可以使用`sudo apt-get install libpcre3-dev libssl-dev`,在CentOS上使用`sudo yum install pcre-devel openssl-devel`。 最后,生成相关问题:</think>以下是重新安装 OpenResty配置文件的完整步骤指南,结合了引用中的关键信息: ### 1. 下载和解压源码 ```bash # 下载最新版(替换版本号) wget https://openresty.org/download/openresty-1.25.3.2.tar.gz # 解压并进入目录 tar -zxvf openresty-1.25.3.2.tar.gz cd openresty-1.25.3.2 ``` ### 2. 配置编译参数(关键步骤) ```bash ./configure --prefix=/usr/local/openresty \ --with-luajit \ --with-http_ssl_module \ # SSL支持[^1][^5] --with-http_v2_module \ # HTTP/2支持 --with-http_realip_module \ # 真实客户端IP获取 --with-http_stub_status_module \ # 状态监控[^5] --with-http_gzip_static_module \ # Gzip压缩 --with-pcre \ # PCRE正则支持 --with-openssl-opt=enable-tls1_3 # 启用TLS1.3[^5] ``` > 📌 提示:可通过 `./configure --help` 查看所有可用模块 ### 3. 编译和安装 ```bash make -j$(nproc) # 使用所有CPU核心编译 sudo make install # 安装到指定目录 ``` ### 4. 配置环境变量 ```bash echo 'export PATH=/usr/local/openresty/nginx/sbin:$PATH' | sudo tee -a /etc/profile source /etc/profile ``` ### 5. 配置文件设置(核心) ```bash # 创建配置目录结构 sudo mkdir -p /usr/local/openresty/nginx/conf/{sites-available,sites-enabled} # 编辑主配置文件 sudo nano /usr/local/openresty/nginx/conf/nginx.conf ``` 基础配置模板: ```nginx worker_processes auto; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; # 包含子配置 include sites-enabled/*.conf; } ``` ### 6. 创建服务器配置 ```bash sudo nano /usr/local/openresty/nginx/conf/sites-available/default.conf ``` 示例配置: ```nginx server { listen 80; server_name localhost; location / { content_by_lua_block { ngx.say("OpenResty reinstalled successfully!") } } # 状态监控端点[^5] location /nginx_status { stub_status; } } ``` ### 7. 启用配置并启动服务 ```bash # 创建符号链接 sudo ln -s /usr/local/openresty/nginx/conf/sites-available/default.conf \ /usr/local/openresty/nginx/conf/sites-enabled/ # 测试配置语法 nginx -t # 启动服务 nginx ``` ### 8. 验证安装 ```bash curl http://localhost # 应返回:OpenResty reinstalled successfully! ``` ### 关键注意事项: 1. **模块选择**:根据需求添加编译模块,如流处理 `--with-stream` 或 Brotli 压缩 `--add-module=./ngx_brotli`[^5] 2. **配置重载**:修改配置后使用 `nginx -s reload` 平滑重载[^3] 3. **目录结构**: - 主配置:`/usr/local/openresty/nginx/conf/nginx.conf` - Lua脚本:`/usr/local/openresty/nginx/lua/` 4. **依赖安装**:编译前需安装 `libpcre3-dev, libssl-dev` 等依赖
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值