/etc/nginx/nginx.conf中的未知指令“ init_by_lua_block”

时间:2019-12-05 13:07:32

标签: nginx lua openresty

我正在使用AWS EC2 ubuntu 18.04实例来设置反向代理服务器,以实现Web应用程序白标的概念。

我安装了nginx openrestylua

以下是各个版本:

nginx version: nginx/1.14.0 (Ubuntu)
nginx version: openresty/1.15.8.2
Lua: 5.1.5 

我遇到在nginx中创建正确的配置文件的问题

这是我的nginx.conf文件代码:

user  ubuntu www;
events {
    worker_connections 1024;
}

http {
    lua_shared_dict auto_ssl 1m;
    lua_shared_dict auto_ssl_settings 64k;
    resolver 8.8.8.8 ipv6=off;

    init_by_lua_block {
        auto_ssl = (require "resty.auto-ssl").new()
        auto_ssl:set("allow_domain", function(domain)
           return true
        end)
        auto_ssl:init()
    }

    init_worker_by_lua_block {
        auto_ssl:init_worker()
    }

    server {
        listen 443 ssl;
        ssl_certificate_by_lua_block {
            auto_ssl:ssl_certificate()
        }
        ssl_certificate /etc/ssl/resty-auto-ssl-fallback.crt;
        ssl_certificate_key /etc/ssl/resty-auto-ssl-fallback.key;

        proxy_ssl_server_name on;
        location / {
            proxy_set_header Host app.mydomain.com;
            proxy_set_header Referer $host$uri;
            proxy_buffer_size          128k;
            proxy_buffers              4 256k;
            proxy_busy_buffers_size    256k;

            proxy_set_header User-Agent $http_user_agent;
            proxy_set_header X-Real-IP $remote_addr;

            proxy_set_header Accept-Encoding "";
            proxy_set_header Accept-Language $http_accept_language;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto  https;
            proxy_read_timeout 5m;

            proxy_pass https://app.mydomain.com;
        } 
    }

    server {
        listen 80;
        location /.well-known/acme-challenge/ {
            content_by_lua_block {
                auto_ssl:challenge_server()
            }
        }

        location / {
           return 301 https://$host$request_uri;
        }
    }

    server {
        listen 127.0.0.1:8999;
        client_body_buffer_size 128k;
        client_max_body_size 128k;

        location / {
            content_by_lua_block {
                auto_ssl:hook_server()
            }
        }
    }
}

重新启动nginx服务时,出现以下错误:

unknown directive "init_by_lua_block" in /etc/nginx/nginx.conf

2 个答案:

答案 0 :(得分:2)

最后,我了解到必须使用openresty而不是nginx来启动服务器。

所以我运行了以下命令,它起作用了:

sudo systemctl start openresty.service

答案 1 :(得分:0)

我遇到了同样的问题和同样的错误。在 ubuntu 18.04 上它有效:

  1. 通过 apt 安装 nginx lua 模块(这也会安装 libnginx-mod-http-ndk):
sudo apt install libnginx-mod-http-lua
  1. nginx.conf 文件的顶部添加用于加载模块的指令:
load_module modules/ndk_http_module.so;
load_module modules/ngx_http_lua_module.so;
  1. 添加完lua代码块后(例如官方Quick Start Guide中的说明),请确保以下行的路径正确:
lua_package_path "/path/to/nginx-lua-prometheus/?.lua;;";

目录 nginx-lua-prometheus 是您必须克隆的目录:https://github.com/knyar/nginx-lua-prometheus