安装 openresty

OpenResty 官网链接

tar -xzvf openresty-VERSION.tar.gz
cd openresty-VERSION/
make install

运行 openresty

# 默认安装路径为 /usr/local/openresty
# pwd 命令来查看"当前工作目录"的完整路径

/usr/local/openresty/nginx/sbin/nginx -p `pwd`/ -c /usr/local/openresty/nginx/conf/nginx.conf

# 启动

# 停止
/usr/local/openresty/nginx/sbin/nginx -s stop

# 重启
/usr/local/openresty/nginx/sbin/nginx -s reload

# 检验nginx配置是否正确
/usr/local/openresty/nginx/sbin/nginx -t


systemctl enable openresty

systemctl status openresty

systemctl restart openresty

nignx 配置

启动文件为nginx.conf ,要支持多文件 需要加上 include conf.d/*.conf;

··· 省略 ···

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;

    # 新建conf.d 新建不同的conf文件
    include conf.d/*.conf;

    server {
        listen       8099;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
··· 省略 ···



-- set_by_lua $res <lua-script-str> [$arg1 $arg2...]
-- nginx conf 代码
location /foo {
 set $diff ''; # we have to predefine the $diff variable here
 set_by_lua $sum '
     local a = 32
     local b = 56
     ngx.var.diff = a - b;  -- write to $diff directly
     return a + b;          -- return the $sum value normally
 echo "sum = $sum, diff = $diff";

让systemd 管理

  1. vim打开 /usr/lib/systemd/system/nginx.service 内容如下
    Description=The nginx process manager

[Service] Type=forking ExecStart=/usr/local/openresty/nginx/sbin/nginx ExecReload=/usr/local/openresty/nginx/sbin -s reload ExecStop=/usr/local/openresty/nginx/sbin -s stop


2. 重启system

systemctl daemon-reload


Centos7.X配置Nginx+Lua(OpenResty)以及让systemd 管理nginx服务 使用openresty通过lua修改请求/响应头