nginx-vhosts
Programmatically add or remove vhosts to a running Nginx instance.
To use this you should have your Nginx configuration file set up such that the http
section has a include
directive for all .conf
files in a certain folder on your machine, e.g.:
http {
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
The machine configured with the configuration about would use either /etc/nginx/conf.d/
or /etc/nginx/sites-enabled/
as the confDir
below. Note that the official Ubuntu PPA for Nginx has it configured this way by default.
See also:
api
var vhosts = require('nginx-vhosts')(opts, onStopStart)
onStopStart
is passed to and called from nginx-reload
var vhosts = opts { // isRunning is a boolean, true if nginx is running, false if it is not // this function will get called whenever nginx stops or starts // note: .reload() does not trigger this function, as nginx does not // actually stop during a configuration reload}
opts
defaults to:
confDir: '/usr/local/etc/nginx/conf.d/' pidLocation: '/var/run/nginx.pid'
vhosts.write(opts, cb)
vhosts
This writes a new configuration file to the configuration directory and then tells Nginx to reload its configuration. In the above example it would configure Nginx to proxy requests from test.local
to localhost:8080
You can also supply your own config file:
vhosts
Note: it may take Nginx a few seconds to finish reloading the configuration after the callback is called.
vhosts.remove(name, cb)
vhosts
This removes a configuration file and tells Nginx to reload its configuration.
Note: it may take Nginx a few seconds to finish reloading the configuration after the callback is called.
run the tests
There are integration tests available, provided you have the following things set up:
- your nginx is configured to store a
pid
file in '/var/run/nginx.pid' - your nginx is configured to
include
confs forhttp
from/usr/local/etc/nginx/conf.d/
- you have
test.local
in your/etc/hosts
as an entry forlocalhost
npm install
sudo npm test