Installations
npm install nginx-conf
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
18.2.0
NPM Version
8.9.0
Score
89.5
Supply Chain
99.5
Quality
75.9
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (70.91%)
TypeScript (29.09%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
tmont
Download Statistics
Total Downloads
2,989,970
Last Day
2,170
Last Week
9,530
Last Month
43,981
Last Year
914,412
GitHub Statistics
394 Stars
114 Commits
42 Forks
12 Watching
1 Branches
5 Contributors
Bundle Size
9.64 kB
Minified
3.14 kB
Minified + Gzipped
Package Meta Information
Latest Version
2.1.0
Package Id
nginx-conf@2.1.0
Unpacked Size
52.06 kB
Size
13.16 kB
File Count
12
NPM Version
8.9.0
Node Version
18.2.0
Total Downloads
Cumulative downloads
Total Downloads
2,989,970
Last day
3.8%
2,170
Compared to previous day
Last week
-5.3%
9,530
Compared to previous week
Last month
8.6%
43,981
Compared to previous month
Last year
10.1%
914,412
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
4
nginx-conf
nginx-conf
is a node module for making changes to an nginx configuration
file programmatically.
Installation
npm install nginx-conf
This library has no dependencies.
Breaking Changes
Version 2.0.0
changed the way that single directives are accessed. In short, everything
is now array-indexed.
1// Pre 2.0.0: 2conf.nginx.foo.bar._value; 3 4// 2.0.0+ 5conf.nginx.foo[0].bar[0]._value;
Usage
Pretend you have an nginx config file like this one.
Note that all public methods are prefixed with _
so that they (hopefully) don't clash with
nginx's directives.
Note: *_content_by_lua_block
directives are supported in >=v1.3.0
.
1// vanilla JS: const NginxConfFile = require('nginx-conf').NginxConfFile; 2import {NginxConfFile} from '../../'; 3 4const filename = `${__dirname}/../files/readme.conf`; 5 6NginxConfFile.create(filename, function (err, conf) { 7 if (err || !conf) { 8 console.log(err); 9 return; 10 } 11 12 // reading values 13 console.log('user: ' + conf.nginx.user?.[0]._value); 14 console.log('http.server.listen: ' + conf.nginx.http?.[0].server?.[0].listen?.[0]._value); 15 console.log('http.server.location.root:' + conf.nginx.http?.[0].server?.[0].location?.[3].root?.[0]._value); 16 17 //writing values 18 //NginxConfFile.create() automatically sets up a sync, so that whenever 19 //a value is changed, or a node is removed/added, the file gets updated 20 //immediately 21 22 const onFlushed = () => { 23 console.log('finished writing to disk'); 24 }; 25 26 conf.on('flushed', onFlushed); 27 28 //listen to the flushed event to determine when the new file has been flushed to disk 29 if (conf.nginx.events?.[0].connections) { 30 conf.nginx.events[0].connections[0]._value = 1000; 31 32 //don't write to disk when something changes 33 conf.die(filename); 34 conf.nginx.events[0].connections[0]._value = 2000; //change remains local, not in /etc/nginx.conf 35 } 36 37 //write to a different file 38 conf.live(`${filename}.bak`); 39 40 //force the synchronization 41 conf.flush(); 42 43 //adding and removing directives 44 if (conf.nginx.http) { 45 conf.nginx.http[0]._add('add_header', 'Cache-Control max-age=315360000, public'); 46 console.log(conf.nginx.http[0].add_header?.[0]._value); //Cache-Control max-age=315360000, public 47 48 conf.nginx.http[0]._add('add_header', 'X-Load-Balancer lb-01'); 49 conf.nginx.http[0]._add('add_header', 'X-Secure true'); 50 51 console.log(conf.nginx.http[0].add_header?.[0]._value); //Cache-Control max-age=315360000, public 52 console.log(conf.nginx.http[0].add_header?.[1]._value); //X-Load-Balancer lb-01 53 console.log(conf.nginx.http[0].add_header?.[2]._value); //X-Secure true 54 55 conf.nginx.http[0]._remove('add_header'); //removes add_header[0] 56 conf.nginx.http[0]._remove('add_header', 1); //removes add_header[1] 57 } 58 59 //adding a new block 60 conf.nginx.http?.[0]._add('server'); 61 conf.nginx.http?.[0].server?.[0]._add('listen', '80'); 62 63 //that'll create something like this: 64 /* 65 server { 66 listen 80; 67 } 68 */ 69 70 //multiple blocks 71 conf.nginx.http?.[0]._add('server'); 72 conf.nginx.http?.[0].server?.[1]._add('listen', '443'); 73 74 /* 75 server { 76 listen 80; 77 } 78 server { 79 listen 443; 80 } 81 */ 82 83 // blocks with values: 84 conf.nginx.http?.[0].server?.[1]._add('location', '/'); 85 conf.nginx.http?.[0].server?.[1].location?.[0]._add('root', '/var/www/example.com'); 86 87 /* 88 server { 89 location / { 90 root /var/www/example.com; 91 } 92 } 93 */ 94 95 // you can also create empty blocks 96 conf.nginx.http?.[0]._add('events', '', []); // events { } 97 98 // lua blocks also work, but you can't put a mismatched "{" or "}" in a comment! 99 conf.nginx.http?.[0].server?.[0].location?.[0]._addVerbatimBlock('rewrite_by_lua_block', '\n\ 100 ngx.say("this is a lua block!")\n\ 101 res = ngx.location.capture("/memc",\n\ 102 { args = { cmd = "incr", key = ngx.var.uri } }\n\ 103 )' 104 ); 105 106 // remove old listener 107 conf.off('flushed', onFlushed); 108 109 // kill process when done writing to disk 110 conf.on('flushed', () => { 111 console.log('finished writing to disk, exiting'); 112 process.exit(); 113 }); 114 115 conf.flush(); 116});
Comments
Support for comments is supported-ish. Comments are attached to directives, and will always
be rendered above the directive when using toString()
(or _getString()
).
Comments can be added, removed and updated via the _comments
array on a node.
1console.log(conf.nginx.events[0].use[0]._comments.length); // 1 2console.log(conf.nginx.events[0].use[0]._comments[0]); // use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; 3 4//remove the comment 5conf.nginx.events[0].use[0]._comments.splice(0, 1); 6 7//add a new one 8conf.nginx.events[0].use[0]._comments.push('my new comment'); 9console.log(conf.nginx.events[0].use[0]._comments.length); // 1 10console.log(conf.nginx.events[0].use[0]._comments[0]); //my new comment 11 12//update a comment's text 13conf.nginx.events[0].use[0]._comments[0] = 'updated'; 14console.log(conf.nginx.events[0].use[0]._comments[0]); //updated
If the comment is in a weird place (like in the middle of a directive), it'll still be attached to the node. If it's after the directive (after the semicolon or closing brace), it will be attached to the next node, or ignored if it's at the end of the file.
Assuming this nginx configuration:
1foo #comment 2bar;
You will have this object structure:
1console.log(conf.nginx.foo[0]._value); //bar 2console.log(conf.nginx.foo[0]._comments[0]); //comment
But if the comment comes after:
1foo bar; 2#comment
1console.log(conf.nginx.foo[0]._value); //bar 2console.log(conf.nginx.foo[0]._comments.length); //0
Parser options
Support for go template syntax is provided via NginxParserOptions
. By default, templating syntax is not supported.
To enable templating syntax, pass the following NginxParserOptions
to the parser.parse
or parser.parseFile
function(s):
1{ 2 templateSyntax: true 3}
Development
1git clone git@github.com:tmont/nginx-conf.git 2cd nginx-conf 3npm install 4npm test
If you're making changes, you should run npm run watch
in a separate
terminal. tsc
will output the JavaScript in the dist/
directory.
The tests reference the JavaScript files in dist/
, not the TypeScript
files elsewhere.
Only the stuff in dist/
is included in the NPM package.
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
2 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/tmont/nginx-conf/test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/tmont/nginx-conf/test.yml/master?enable=pin
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 1 out of 1 npmCommand dependencies pinned
Reason
Found 2/24 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/test.yml:1
- Info: no jobLevel write permissions found
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 9 are checked with a SAST tool
Score
3.4
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to nginx-conf
@webantic/nginx-config-parser
Convert nginx conf strings to JSON and back!
nv-cli-nginx-conf
nv-cli-nginx-conf =============== - nv-cli-nginx-conf - cli tool, nginx conf for a special format
nginx-conf-parser
nginx config files parser based on jison
nginx-conf-builder
Nginx configuration helper