Node.JS Nginx Manager - Part II
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.
This method returns the defined backend servers in the nginx configuration file as an array of;
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.
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;
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;
By calling setServer on previous nginx configuration file, result would be as below;
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;
You can use setCompression of NginxUpstream class to enable the compression;
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;
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.