Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the acf domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php on line 6121

Deprecated: Creation of dynamic property ACF::$fields is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/fields.php on line 138

Deprecated: Creation of dynamic property acf_loop::$loops is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/loop.php on line 28

Deprecated: Creation of dynamic property ACF::$loop is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/loop.php on line 269

Deprecated: Creation of dynamic property ACF::$revisions is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/revisions.php on line 397

Deprecated: Creation of dynamic property acf_validation::$errors is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/validation.php on line 28

Deprecated: Creation of dynamic property ACF::$validation is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/validation.php on line 214

Deprecated: Creation of dynamic property acf_form_customizer::$preview_values is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/forms/form-customizer.php on line 28

Deprecated: Creation of dynamic property acf_form_customizer::$preview_fields is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/forms/form-customizer.php on line 29

Deprecated: Creation of dynamic property acf_form_customizer::$preview_errors is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/forms/form-customizer.php on line 30

Deprecated: Creation of dynamic property ACF::$form_front is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/forms/form-front.php on line 598

Deprecated: Creation of dynamic property acf_form_widget::$preview_values is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/forms/form-widget.php on line 34

Deprecated: Creation of dynamic property acf_form_widget::$preview_reference is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/forms/form-widget.php on line 35

Deprecated: Creation of dynamic property acf_form_widget::$preview_errors is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/forms/form-widget.php on line 36

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the all-in-one-wp-migration domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php on line 6121

Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php:6121) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/wp_plugin/wp_plugin.php on line 23

Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/wp-super-cache/wp-cache-phase2.php on line 54

Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php:6121) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/wp-super-cache/wp-cache-phase2.php on line 1539

Deprecated: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/wp-super-cache/wp-cache-phase2.php on line 828

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the rocket domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php on line 6121

Deprecated: Creation of dynamic property acf_field_oembed::$width is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/fields/class-acf-field-oembed.php on line 31

Deprecated: Creation of dynamic property acf_field_oembed::$height is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/fields/class-acf-field-oembed.php on line 32

Deprecated: Creation of dynamic property acf_field_google_map::$default_values is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/fields/class-acf-field-google-map.php on line 33

Deprecated: Creation of dynamic property acf_field__group::$have_rows is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/includes/fields/class-acf-field-group.php on line 31

Deprecated: Creation of dynamic property acf_field_clone::$cloning is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/pro/fields/class-acf-field-clone.php on line 34

Deprecated: Creation of dynamic property acf_field_clone::$have_rows is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-pro/pro/fields/class-acf-field-clone.php on line 35

Deprecated: Creation of dynamic property jh_acf_field_table::$settings is deprecated in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-content/plugins/advanced-custom-fields-table-field/class-jh-acf-field-table.php on line 23

Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php:6121) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/rest-api/class-wp-rest-server.php on line 1896

Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php:6121) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/rest-api/class-wp-rest-server.php on line 1896

Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php:6121) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/rest-api/class-wp-rest-server.php on line 1896

Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php:6121) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/rest-api/class-wp-rest-server.php on line 1896

Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php:6121) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/rest-api/class-wp-rest-server.php on line 1896

Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php:6121) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/rest-api/class-wp-rest-server.php on line 1896

Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php:6121) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/rest-api/class-wp-rest-server.php on line 1896

Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/functions.php:6121) in /var/www/vhosts/studiogo.tech/httpdocs/upcloudold/wp-includes/rest-api/class-wp-rest-server.php on line 1896
{"id":24654,"date":"2015-02-19T13:33:06","date_gmt":"2015-02-19T11:33:06","guid":{"rendered":"https:\/\/upcloud.com\/community\/tutorials\/configure-load-balancing-nginx"},"modified":"2015-02-19T13:33:06","modified_gmt":"2015-02-19T11:33:06","slug":"configure-load-balancing-nginx","status":"publish","type":"tutorial","link":"https:\/\/studiogo.tech\/upcloudold\/tutorial\/configure-load-balancing-nginx\/","title":{"rendered":"How to configure load balancing using Nginx"},"content":{"rendered":"\n

Advantages of load balancing<\/h2>\n\n\n\n

Load balancing is an excellent way to scale out your application and increase its performance\u00a0and redundancy. Nginx, a popular web server software, can be configured as a simple yet powerful load balancer to improve your server’s\u00a0resource availability and efficiency.<\/p>\n\n\n\n

How does Nginx work? Nginx acts as a single entry point to a distributed web application working on multiple separate servers.<\/p>\n\n\n\n

This guide describes the advantages of load balancing. Learn how to set up load balancing with nginx for your cloud servers.<\/p>\n\n\n\n

As a prerequisite, you\u2019ll need to have at least two hosts with web server software installed and configured to see the benefit of the load balancer. If you already have one web host set up, duplicate\u00a0it by creating a custom image<\/a> and deploying it onto a new server at your UpCloud control panel<\/a>.<\/p>\n\n\n\n

\n
Try UpCloud for free!<\/a><\/div>\n<\/div>\n\n\n\n

Installing nginx<\/h2>\n\n\n\n

The first thing to do is to set up a new host that will serve as your load balancer. Deploy a new instance at your UpCloud Control Panel if you haven\u2019t already. Currently, nginx packages are available on the latest versions of CentOS, Debian and Ubuntu. So pick whichever of these you prefer.<\/p>\n\n\n\n

After you have set up the server the way you like, install the latest stable nginx. Use one of the following methods.<\/p>\n\n\n\n

# Debian and Ubuntu\nsudo apt-get update\n# Then install the Nginx Open Source edition\nsudo apt-get install nginx<\/pre>\n\n\n\n
# CentOS\n# Install the extra packages repository\nsudo yum install epel-release\n# Update the repositories and install Nginx\nsudo yum update\nsudo yum install nginx<\/pre>\n\n\n\n

Once installed change the directory into the nginx main configuration folder.<\/p>\n\n\n\n

cd \/etc\/nginx\/<\/pre>\n\n\n\n

Now depending on your OS, the web server configuration files will be in one of two places.<\/p>\n\n\n\n

Ubuntu and Debian follow a rule for storing virtual host files in \/etc\/nginx\/sites-available\/<\/tt>, which are enabled through symbolic links to \/etc\/nginx\/sites-enabled\/<\/tt>. You can use the command below to enable any new virtual host files.<\/p>\n\n\n\n

sudo ln -s \/etc\/nginx\/sites-available\/vhost<\/span> \/etc\/nginx\/sites-enabled\/vhost<\/span><\/pre>\n\n\n\n

CentOS users can find their host configuration files under \/etc\/nginx\/conf.d\/ in which any .conf<\/em> type virtual host file gets loaded.<\/p>\n\n\n\n

Check that you find at least the default configuration and then restart nginx.<\/p>\n\n\n\n

sudo systemctl restart nginx<\/pre>\n\n\n\n

Test that the server replies to HTTP requests. Open the load balancer server\u2019s public IP address in your web browser. When you see the default welcoming page for nginx the installation was successful.<\/p>\n\n\n\n

\"Nginx<\/figure>\n\n\n\n

If you have trouble loading the page, check that a firewall is not blocking your connection. For example on CentOS 7 the default firewall rules do not allow HTTP traffic, enable it with the commands below.<\/p>\n\n\n\n

sudo firewall-cmd --add-service=http --permanent\nsudo firewall-cmd --reload<\/pre>\n\n\n\n

Then try reloading your browser.<\/p>\n\n\n\n

Configuring nginx as a load balancer<\/h2>\n\n\n\n

When nginx is installed and tested, start to configure it for load balancing. In essence, all you need to do is set up nginx with instructions for which type of connections to listen to and where to redirect them. Create a new configuration file using whichever text editor you prefer. For example with nano<\/em>:<\/p>\n\n\n\n

sudo nano \/etc\/nginx\/conf.d\/load-balancer.conf<\/pre>\n\n\n\n

In the load-balancer.conf<\/em> you\u2019ll need to define the following two segments, upstream<\/em> and server<\/em>, see the examples below.<\/p>\n\n\n\n

# Define which servers to include in the load balancing scheme. \n# It's best to use the servers' private IPs for better performance and security.\n# You can find the private IPs at your UpCloud control panel<\/a> Network section.\nhttp {\n   upstream backend {\n      server 10.1.0.101; \n      server 10.1.0.102;\n      server 10.1.0.103;\n   }\n\n   # This server accepts all traffic to port 80 and passes it to the upstream. \n   # Notice that the upstream name and the proxy_pass need to match.\n\n   server {\n      listen 80; \n\n      location \/ {\n          proxy_pass http:\/\/backend;\n      }\n   }\n}<\/pre>\n\n\n\n

Then save the file and exit the editor.<\/p>\n\n\n\n

Next, disable the default server configuration you earlier tested was working after the installation. Again depending on your OS, this part differs slightly.<\/p>\n\n\n\n

On Debian and Ubuntu systems you\u2019ll need to remove the default<\/em> symbolic link from the sites-enabled<\/em> folder.<\/p>\n\n\n\n

sudo rm \/etc\/nginx\/sites-enabled\/default<\/pre>\n\n\n\n

CentOS hosts don\u2019t use the same linking. Instead, simply rename the default.conf<\/em> in the conf.d\/<\/em> directory to something that doesn\u2019t end with .conf<\/em>, for example:<\/p>\n\n\n\n

sudo mv \/etc\/nginx\/conf.d\/default.conf \/etc\/nginx\/conf.d\/default.conf.disabled<\/pre>\n\n\n\n

Then use the following to restart nginx.<\/p>\n\n\n\n

sudo systemctl restart nginx<\/pre>\n\n\n\n

Check that nginx starts successfully. If the restart fails, take a look at the  \/etc\/nginx\/conf.d\/load-balancer.conf<\/em> you just created to make sure there are no mistypes or missing semicolons.<\/p>\n\n\n\n

When you enter the load balancer\u2019s public IP address in your web browser, you should pass to one of your back-end servers.<\/p>\n\n\n\n

Load balancing methods<\/h2>\n\n\n\n

Load balancing with\u00a0nginx uses a round-robin algorithm by default if no other method is defined, like in the first example above.\u00a0With a round-robin scheme,\u00a0each server is selected\u00a0in turns according to the order you set them in the load-balancer.conf<\/em>\u00a0file. This balances the number of requests\u00a0equally for short operations.<\/p>\n\n\n\n

Least connections-based load balancing is another straightforward method. As the name suggests, this method directs the requests to the server with the least active connections at that time. It works more fairly than round-robin would\u00a0with applications where requests might sometimes take longer to complete.<\/p>\n\n\n\n

To enable the least connections balancing method,\u00a0add the parameter least_conn<\/em> to your upstream<\/em>\u00a0section as shown in the example below.<\/p>\n\n\n\n

upstream backend {\n   least_conn;\n   server 10.1.0.101; \n   server 10.1.0.102;\n   server 10.1.0.103;\n}<\/pre>\n\n\n\n

Round-robin and least connections balancing schemes are fair and have their uses. However, they cannot provide session persistence. If your web application requires that the users are subsequently directed to the same back-end server as during their previous connection, use the IP hashing method instead. IP hashing uses the visitor’s IP address as a key to determining which host should be selected to service the request. This allows the visitors to be each time directed to the same server, granted that the server is available and the visitor\u2019s IP address hasn\u2019t changed.<\/p>\n\n\n\n

To use this method, add the ip_hash<\/em> -parameter to your upstream<\/em> segment like in the example underneath.<\/p>\n\n\n\n

upstream backend {\n   ip_hash;\n   server 10.1.0.101; \n   server 10.1.0.102;\n   server 10.1.0.103;\n}<\/pre>\n\n\n\n

In a server setup where the available resources between different hosts are not equal, it might be desirable to favour some servers over others. Defining server weights allows you to further fine-tune load balancing with nginx. The server with the highest weight in the load balancer is selected the most often.<\/p>\n\n\n\n

upstream backend {\n   server 10.1.0.101 weight=4; \n   server 10.1.0.102 weight=2;\n   server 10.1.0.103;\n}<\/pre>\n\n\n\n

For example in the configuration shown above the first server is selected twice as often as the second, which again gets twice the requests compared to the third.<\/p>\n\n\n\n

Load balancing with HTTPS enabled<\/h2>\n\n\n\n

Enable HTTPS for your site, it is a great way to protect your visitors and their data. If you haven\u2019t yet implemented encryption on your web hosts, we highly recommend you take a look at our guide for how to install Let\u2019s Encrypt on nginx<\/a>.<\/p>\n\n\n\n

Using encryption with a load balancer is easier than you might think. All you need to do is to add another server section to your load balancer configuration file which listens to HTTPS traffic at port 443 with SSL. \u00a0Then set up a proxy_pass to your upstream segment like with the HTTP in the previous example above.<\/p>\n\n\n\n

Open your configuration file again for editing.<\/p>\n\n\n\n

sudo nano \/etc\/nginx\/conf.d\/load-balancer.conf<\/pre>\n\n\n\n

Then add the following server segment to the end of the file.<\/p>\n\n\n\n

server {\n   listen 443 ssl;\n   server_name domain_name<\/span>;\n   ssl_certificate \/etc\/letsencrypt\/live\/domain_name<\/span>\/cert.pem;\n   ssl_certificate_key \/etc\/letsencrypt\/live\/domain_name<\/span>\/privkey.pem;\n   ssl_protocols TLSv1 TLSv1.1 TLSv1.2;\n\n   location \/ {\n      proxy_pass http:\/\/backend;\n   }\n}<\/pre>\n\n\n\n

Then save the file, exit the editor and restart nginx again.<\/p>\n\n\n\n

sudo systemctl restart nginx<\/pre>\n\n\n\n

Setting up encryption at your load balancer when you are using private network connections to your back end has some great advantages.<\/p>\n\n\n\n