This article introduces to you how to use nginx to set up load balancing in Linux. Let’s take a look at the specific content.
Prerequisites
Must have root access or sudo access. Connect to your server console using permissions access. Configure your site on the backend server.
Step 1: Install nginx server
First, log in to your server using ssh access, Windows users can use putty or ssh alternatives in the server. Now install nginx using Linux package manager. nginx packages are available under the default yum and apt repositories.
Use Apt-get:
$ sudo apt-get install nginx
Use Yum:
$ sudo yum install nginx
Use DNF:
$ sudo dnf install nginx
Step 2: Set up virtual host
Let’s create an nginx virtual host configuration file for the domain. Below is the minimal setup configuration file.
/etc/nginx/conf.d/www.example.com.conf
upstream remote_servers { server remote1.example.com; server remote2.example.com; server remote3.example.com; } server { listen 80; server_name example.com www.example.com; location / { proxy_pass http://remote_servers; } }
Step 3: Other useful commands
can also be used Some more useful settings to customize and optimize your load balancer with nginx. For example set, weight and ip hash (hash), as configured below.
Weight
upstream remote_servers { server remote1.example.com weight=1; server remote2.example.com weight=2; server remote3.example.com weight=4; }
IP Hash
upstream remote_servers { ip_hash; server remote1.example.com; server remote2.example.com; server remote3.example.com down; }
Step 4: Restart nginx service
After completing all changes, use the following command Restart the nginx service.
$ sudo systemctl restart nginx.service
This article has ended here. For more exciting content, you can pay attention to other related column tutorials on the php Chinese website! ! !
The above is the detailed content of How to set up load balancing using nginx in Linux. For more information, please follow other related articles on the PHP Chinese website!