Switch is a language agnostic CLI that allows you to write apache or nginx conf as JSON and switch between the two seamlessly.
Install the SpiderMonkey javascript shell:
Linux:
apt-get install -y libmozjs-24-bin
Mac with homebrew:
brew install spidermonkey
Download Switch:
curl -o- https://raw.githubusercontent.com/luke-j/switch/master/install.sh | bash
Create your .switchrc
config file (see the example here).
Once you've created your config file, you can create conf and switch server software via the CLI:
Switch
Note: currently supported <app> options are either nginx or apache
Usage:
switch help Show this message
switch to <app> Build conf file for the <app> specified and start the <app> service, catching any <app> errors
--config <file> Config file to be used to generate conf file. Default: <current directory>/.switchrc
--out <file> Output file. Default: /etc/nginx/sites-enabled/switch.conf, /etc/apache2/sites-enabled/switch.conf
--source <file> The Switch source directory, for current directory, use a period. Default: ~/.switch
--print Display the generated conf without writing to the output file
switch current Display <app> currently being used
Example:
switch to nginx Generate nginx.conf and start the nginx service
switch to apache --print Display the generated apache.conf, without writing to file or starting apache
.switchrc
files are just json. Config options are given under a property with the same name as the server.
For example, if we wanted to create a virtual host for example.com
, our .switchrc
file would read:
{
"example.com": {
}
}
In addition to these options, you can also include "locations" - which are any properties starting with a forward slash. Locations allow you to nest options for particular URL paths.
For example, if we wanted to compress javascript assets under the url example.com/static/js
, we could do:
{
"example.com": {
"/static/js": {
"compress": {
"enable": true,
"types": [
"application/javascript"
]
}
}
}
}
Type: Number
, Default: 80
, Required
Type: Array
, Default: []
Type: String
, Default: null
Type: String
, Default: null
Type: String
, Default: null
Type: Array
, Default: null
Type: Boolean
, Default: false
Type: Boolean
, Default: true
Type: Boolean
, Default: false
Type: Object
, Default: Object
Type: Boolean
, Default: false
Type: String
, Default: null
Type: String
, Default: null
Type: Object
, Default: Object
Type: Boolean
, Default: true
Type: Array
, Default: ['text/plain']
Type: Object
, Default: Object
Type: Boolean
, Default: false
Type: Array
, Default: ['text/plain']
Type: Object
, Default: Object
Type: Number
, Default: 0
Type: Number
, Default: 0
Type: Number
, Default: 0
Type: Number
, Default: 0
Type: Number
, Default: 0
Type: Object
, Default: Object
Type: Boolean
, Default: false
Type: String
, Default: 'Restricted'
Type: String
, Default: null
Type: Object
, Default: Object
Type: Object
, Default: null
Type: Array
, Default: []
Type: Object
, Default: Object
Type: Boolean
, Default: false
Type: String
, Default: null
Type: Object
, Default: Object
Type: Boolean
, Default: false
Type: String
, Default: null
Type: Boolean
, Default: true
This example creates an SSL virtual host for example.com
with a reverse proxy on port 3000, it caches and compresses static assets, and performs a 301 redirect on http://
and www
subdomain traffic to https://example.com
.
{
"example.com": {
"port": 443,
"ssl": {
"enable": true,
"cert": "/etc/ssl/cert.crt",
"key": "/etc/ssl/cert.key"
},
"aliases": ["sub.example.com"],
"accessLog": "/var/log/access.log",
"errorLog": "/var/log/error.log",
"root": "/var/www",
"index": [
"index.js"
],
"serverSignature": false,
"listDirectories": false,
"/": {
"proxy": {
"enable": true,
"to": "http://localhost:3000"
}
},
"/static": {
"compress": {
"enable": true,
"types": [
"text/plain",
"application/javascript",
"text/javascript",
"text/css"
]
},
"caching": {
"enable": true,
"types": [
"js", "css", "html", "svg"
],
"expires": {
"years": 1
}
},
"headers": {
"set": {
"header-1": "1",
"header-2": "2",
"header-3": "3"
},
"unset": [
"bad-header-1",
"bad-header-2"
]
}
}
},
"www.example.com": {
"port": 80,
"aliases": ["example.com"],
"redirect": {
"enable": true,
"to": "https://example.com",
"permanent": true
}
}
}
If you have any problems, post an issue to this repository's issue page.
Fork and clone the repository and run the following:
npm install && npm run watch
Once you've made your changes, run the build process:
npm run build
Finally, if your tests pass, submit a pull request!
You can also run vagrant up
for a simple development box with node, nginx and apache installed.
Copyright 2016- Luke Jones (https://github.com/luke-j)
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.