This is the second post for the series of NodeJS Nginx Manager - nginx-upstream,
please consider reading the first part.

At the first part, I tried to describe how to install nginx-upstream and manage backend servers. Today, I will go for the rest of the methods of NginxUpstream class and how to use them.

Sometimes, you may also need to access the backend hosts are defined on a specific virtual host. For this purpose, you can use the backendlist method.

1
2
// Method signature
backendList(callback: (err: any, servers: Array<Object>) => void);

This method returns the defined backend servers in the nginx configuration file as an array of;

1
2
3
4
{
"host": "host_name:port",
"enabled" : "true|false"
}

If backend server is toggled with toggleBackend method, than enabled property would be false, otherwise true.

With backendList, we have covered all the methods related to backend servers. Next part of the nginx-upstream is dealing with the virtual host configurations. First of all, let’s take a look at how to manage nginx to listen the fqdn.

1
setServer(fqdn: string, sitename: string, setUpstream: boolean, callback: (err: any) => void);

This method changes the server_name field of nginx configuration file and also updates the upstream block name with the given sitename. Take a look at the sample conf file for nginx below;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Upstream Servers
upstream nginxconf {
server localhost:81;
server localhost:82;
}
server {
# Ports
listen 80;
# Server (FQDN)
server_name example.com;
location / {
...
proxy_pass http://nginxconf;
...
}
...
...
}

Here we have one backend and our virtual host listens example.com fqdn from 80 port. When you want to change your fqdn, use the setServer method. In example;

1
2
3
4
5
6
7
var tmpTestFile = './test/test.conf';
var nu = new NginxUpstream(tmpTestFile);
nu.setServer('www.test.com', 'testing', true, function(err){
if(err){
// Handle error.
}
})

By calling setServer on previous nginx configuration file, result would be as below;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Upstream Servers
upstream testing {
server localhost:81;
}
server {
# Ports
listen 80;
# Server (FQDN)
server_name www.test.com;
location / {
...
proxy_pass http://testing;
...
}
...
...
}

Now, your nginx host will listen www.test.com on port 80 and again balance the requests to backend server localhost:81 and localhost:82.

Our next config changing method is setCompression. Nginx can take over the compression (gzip) from your backend application servers, which is a best practice to do so, since compression/decompression is a costly operation. This way you can utilize your backend servers better and use less CPU power.

Let’s take another example for describing GZIP compression on and off mechanism;

1
2
3
4
5
6
7
8
9
server {
# Ports
listen 80;
# Server (FQDN)
server_name example2.com;
...
...
...
}

You can use setCompression of NginxUpstream class to enable the compression;

1
2
// Method signature
setCompression(enable: boolean, types: Array<string>, callback: (err: any, compressionStatus: boolean) => void)

First parameter is for toggling compression and second parameter is the type of files that nginx would consider compression. This second parameter is optional and default types will be used for compression. Default value for types is;

1
["text/plain", "text/css", "application/json", "application/x-javascript", "text/xml", "application/xml", "application/xml+rss", "text/javascript"]

So we have covered some other interesting methods for nginx-upstream and on my next post, I will discuss about SSL configuration with nginx-upstream package. Thanks for reading this far.

Fork me on github to contribute github