Home > Operation and Maintenance > Linux Operation and Maintenance > How to set up load balancing using nginx in Linux

How to set up load balancing using nginx in Linux

不言
Release: 2019-03-12 10:48:51
Original
2711 people have browsed it

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.

How to set up load balancing using nginx in Linux

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
Copy after login

Use Yum:

$ sudo yum install nginx
Copy after login

Use DNF:

$ sudo dnf install nginx
Copy after login

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;
   }
}
Copy after login

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;
}
Copy after login

IP Hash

upstream remote_servers {
   ip_hash;
   server   remote1.example.com;
   server   remote2.example.com;
   server   remote3.example.com  down;
 }
Copy after login

Step 4: Restart nginx service

After completing all changes, use the following command Restart the nginx service.

$ sudo systemctl restart nginx.service
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template