linuxserver / docker-swag

Nginx webserver and reverse proxy with php support and a built-in Certbot (Let's Encrypt) client. It also contains fail2ban for intrusion prevention.
https://docs.linuxserver.io/general/swag
GNU General Public License v3.0
2.66k stars 231 forks source link

[FEAT] install njs scripting language #449

Closed luisalrp closed 5 months ago

luisalrp commented 5 months ago

Is this a new feature request?

Wanted change

I would like Nginx njs scripting language installed to perform additional actions not available on SWAG.

Reason for change

nsj would enable new capabilities, like checking apikey validity on nginx before a request reaches an upstream server, manipulating response headers or writing flexible asynchronous content handlers and filters

Proposed code change

I don't know the best approach. Maybe a dockermod would be better option because some users would not like to have njs by default for security concerns.

Here is the documentation.

github-actions[bot] commented 5 months ago

Thanks for opening your first issue here! Be sure to follow the relevant issue templates, or risk having this issue marked as invalid.

Roxedus commented 5 months ago

Have you confirmed it is not present?

$ docker exec -it swag nginx -V 2>&1 | tr ' ' '\n' | grep njs
--add-dynamic-module=/home/buildozer/aports/main/nginx/src/njs-0.7.11/nginx
luisalrp commented 5 months ago

Hello Roxedus

Thanks for answering. When I run the docker exec instruction I got the same line, but not sure what can I do from it. I mean, if I try to go there, there is no folder or file and checking the njs conf examples neither the /etc/nginx/njs is present.

Here is an example of a conf file :

load_module modules/ngx_http_js_module.so;

events {  }

http {
    js_path "/etc/nginx/njs/";

    js_import utils.js;
    js_import main from http/hello.js;

    server {
        listen 80;

        location = /version {
            js_content utils.version;
        }

        location / {
            js_content main.hello;
        }

    }
}
Roxedus commented 5 months ago

You need the nginx-mod-http-js OS package, you can install it with the universal package mod.

You then have the needed directives present, like js_path.

Then you just have to configure nginx like normal, with the additional directives.

My quick test was something like adding

    js_path "/config/nginx/njs/";
    js_import main from hello.js;

to the existing http block, where I created the njs directory, and put the hello.js file from the example repo you linked. Then add the location, like you would with a proxy_pass, ie

    location = /hello.html {
        js_content main.hello;
    }

to a server block.

luisalrp commented 5 months ago

Thank you so much Roxedus!