Search code examples
nginxluaopenresty

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


I'm using an AWS EC2 ubuntu 18.04 instance to set up a reverse proxy server to implement the concept of white labeling for a web app.

I installed nginx openresty and lua

Here are the respective versions:

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

I'm facing problems creating the correct config file in nginx

Here is my nginx.conf file code:

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()
            }
        }
    }
}

When I restart the nginx service, I'm getting the below error:

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

Solution

  • Finally, I learned that I had to start the server using openresty instead of nginx.

    So I ran the below command and it worked:

    sudo systemctl start openresty.service