tmont/nginx-conf

Proper way to add server?

Closed this issue · 2 comments

Hi I want to dynamically add new servers to a node

I'm trying to do this:

conf.nginx.tcp._add('server', '{ listen 25565; server_name minecraft.gameserv.com; proxy_pass localhost:23456; }');

which gives me this output:

tcp {
    server { listen 25565; server_name minecraft.gameserv.com; proxy_pass localhost:23456; };
}

everything is cool except that it adds a semicolon after the closing bracket }; which is invalid nginx syntax. I'm assuming thats because it expects me to be adding individual directives

Amusingly, I couldn't even remember how to do it :)

But it is possible. This is how:

conf.nginx.tcp._add('server');
conf.nginx.tcp.server._add('listen', '25565');
conf.nginx.tcp.server._add('server_name', 'minecraft.gameserv.com');
conf.nginx.tcp.server._add('proxy_pass', 'localhost:23456');

If you have more than one server block, you'll need to access them via array:

conf.nginx.tcp._add('server'); 
//if you already have a server {} block
conf.nginx.tcp.server[1]._add('listen', '25565');
//...

Thanks for opening this issue. I added a couple tests to cover this sort of thing, and updated the readme to contain these examples.

Thanks for the response and update very helpful library