Table of Contents
1. Experimental environment
1.1 System and services
./configure --prefix=/usr/local/nginx  --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module  --with-http_mp4_module
Copy after login
" >
./configure --prefix=/usr/local/nginx  --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module  --with-http_mp4_module
Copy after login
Use
3.5 Start nginx and test
If you don’t know where the nginx configuration file and startup script are, you can search for them. Use
四. 配置nginx成为分发器
4.1 先备份配置文件
4.2 把nginx设置成分发器,实现动静分离
五. 配置两台http服务器
5.1 配置mufeng42服务器 配置web服务器:
5.2 配置mufeng43服务器
5.3 测试
Home Operation and Maintenance Nginx How to use nginx to implement a load balancing cluster with dynamic and static separation

How to use nginx to implement a load balancing cluster with dynamic and static separation

May 22, 2023 pm 10:52 PM
nginx

1. Experimental environment

1.1 System and services

The operating system and services used this time:

This experiment requires a total of 3 servers, one nginx is used as a load balancing distributor and a distributor for dynamic and static separation, and two apache servers are used as back-end servers. nginx is used to realize load balancing and static and dynamic separation of the two apache servers.

Operating system: centos7.6
nginx version: 1.22 version
apache version: 2.4.6 that comes with the system by default
php version: 5.4.16## that comes with the system by default

# Both the apache and php versions can be upgraded to the latest version, which can be downloaded and installed from the official website.

1.2 Architecture diagram to be implemented this time

How to use nginx to implement a load balancing cluster with dynamic and static separation

#Generally, our servers are divided into many types, including file servers, picture servers, and database servers.

There are also various services:

  • Static file processing: You can use nginx or apache

  • Dynamic file processing : apache, tomcat

  • Image file processing: squid

In this article, we use nginx to implement a load balancing cluster with dynamic and static separation.

2. Detailed explanation of nginx load balancing

2.1 What is load balancing?

Server load balancing refers to the technology of allocating requests from clients to multiple servers to improve system performance, increase system reliability, and avoid single points of failure.

Through load balancing, multiple servers can jointly process client requests, thereby improving the overall performance and availability of the system.

In load balancing, multiple servers are usually formed into a server cluster. The client sends a request to the load balancer. The load balancer will allocate the request to one or more servers in the server cluster based on a certain algorithm. Processed on multiple servers. There are many load balancing algorithms, common ones include polling, random, minimum number of connections, etc.

Load balancing strategies can become more sophisticated, for example using advanced features such as session persistence, health checks and dynamic weight adjustments. By configuring and adjusting actual needs, the flexibility and efficiency of the load balancing system can be improved.

2.2 5 ways of load balancing

Nginx’s 5 ways of upstream load, the top 3 most commonly used methods are currently:

1) Polling (default)
Every Requests are assigned to different backend servers one by one in chronological order. If the backend server goes down, it can be automatically eliminated.
2) weight
Specifies the polling probability, weight is proportional to the access ratio, and is used when the performance of the back-end server is uneven.
3) ip_hash
Each request is allocated according to the hash result of the accessed IP, so that each visitor has fixed access to a back-end server, which can solve the session problem.
4) air (third party)
Requests are allocated according to the response time of the backend server, and those with short response times are allocated first.
5) url_hash (third party)
Distribute requests according to the hash result of the accessed URL, so that the same URL is directed to the same back-end server. It is more effective when the back-end server is cached

Three . Install nginx as a traffic distributor

3.1 Prepare to install dependent tools before installing nginx

[root@mufeng41 ~]#  yum -y install gcc gcc-c++ autoconf automake

[root@mufeng41 ~]# yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
Copy after login

When Mu Feng Xiaoyue did this step of installation, she forgot to mount the image, which wasted a lot. time, so you need to mount it in advance and configure the yum source.

Upload the nginx compressed package and decompress it

How to use nginx to implement a load balancing cluster with dynamic and static separation

[root@mufeng41 ~]# ll nginx-1.12.2.tar.gz 
-rw-r--r--. 1 root root 981687 8月  27 2019 nginx-1.12.2.tar.gz
[root@mufeng41 ~]# tar xf nginx-1.12.2.tar.gz  -C /usr/local/src/
Copy after login

Log in and view

root@mufeng41 ~]# cd !$
cd /usr/local/src/
[root@mufeng41 src]# ls
nginx-1.12.2
[root@mufeng41 src]# cd nginx-1.12.2/
[root@mufeng41 nginx-1.12.2]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@mufeng41 nginx-1.12.2]#
Copy after login

3.2 Start compiling

./configure --prefix=/usr/local/nginx  --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module  --with-http_mp4_module
Copy after login

Yes Explanation of parameters:

  • –with-http_dav_module enables ngx_http_dav_module support (adds PUT, DELETE, MKCOL: create collection, COPY and MOVE methods). It is closed by default and needs to be compiled and turned on

  • –with-http_stub_status_module Enable ngx_http_stub_status_module support (get the working status of nginx since the last startup)

  • –with-http_addition_module Enable ngx_http_addition_module support (acts as an output filter, supports incomplete buffering, responds to requests in parts)

  • ##–with-http_sub_module Enable ngx_http_sub_module support (allows replacing some of the nginx response with some other text Text)
  • –with-http_flv_module Enable ngx_http_flv_module support (provides time-based offset file seeking memory usage)
  • –with -http_mp4_module enables support for mp4 files (providing time-based offset files for seeking memory usage)
  • 3.3 Start compiling and installing nginx

Use

make && make install

Installation<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>[root@mufeng41 nginx-1.12.2]# make &amp;&amp; make install</pre><div class="contentsignin">Copy after login</div></div>How to determine whether the execution is successful?

Answer:

echo $?

3.4 Generate a running nginx user

[root@mufeng41 nginx-1.12.2]# useradd -u 8000 -s /sbin/nologin  nginx
[root@mufeng41 nginx-1.12.2]# id nginx
uid=8000(nginx) gid=8000(nginx) 组=8000(nginx)
[root@mufeng41 nginx-1.12.2]#
Copy after login

3.5 Start nginx and test

If you don’t know where the nginx configuration file and startup script are, you can search for them. Use

find / -name nginx.conf

.Start the service

[root@itlaoxin163 ~]# find / -name nginx.conf
/usr/local/nginx/conf/nginx.conf

# 启动
[root@mufeng41 nginx-1.12.2]# /usr/local/nginx/sbin/nginx 

[root@mufeng41 nginx-1.12.2]# netstat -antup |grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      25286/nginx: master 
udp        0      0 0.0.0.0:58076           0.0.0.0:*
Copy after login

View the effect

[root@mufeng41 nginx-1.12.2]# systemctl stop firewalld.service
[root@mufeng41 nginx-1.12.2]# curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Fri, 24 Mar 2023 11:06:29 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 24 Mar 2023 11:01:53 GMT
Connection: keep-alive
ETag: "641d8321-264"
Accept-Ranges: byte
Copy after login

四. 配置nginx成为分发器

4.1 先备份配置文件

[root@mufeng41 conf]# pwd
/usr/local/nginx/conf
[root@mufeng41 conf]# cp nginx.conf nginx.conf.bak
[root@mufeng41 conf]#
Copy after login

4.2 把nginx设置成分发器,实现动静分离

配置如下图:

配置分发器

How to use nginx to implement a load balancing cluster with dynamic and static separation

location / {
            root   html;
            index  index.html index.htm;


        if ($request_uri ~* \.html$){
                   proxy_pass http://htmlservers;
           }
        if ($request_uri ~* \.php$){
                   proxy_pass http://phpservers;
           }
                   proxy_pass http://picservers;

        }
Copy after login

注释:
location 的作用是根据请求的 URI,将请求转发到不同的后端服务器上进行处理。具体解释如下:

  • location /:表示所有请求(URI)都会被这个 location 块所匹配。

  • root html:表示当访问的 URI对应的文件不存在时,会在 nginx 安装目录下的 html 目录中查找对应的文件。

  • index index.html

  • index.htm:表示当访问的 URI 对应的目录中没有指定的默认文件时,会尝试访问 index.html 或 index.htm 文件。

  • if ($request_uri ~* .html$):表示如果请求的 URI 包含 .html,则执行下面的语句。

  • proxy_pass http://htmlservers:表示将请求转发到名为 htmlservers 的后端服务器处理。

  • if ($request_uri ~* .php$):表示如果请求的 URI 包含 .php,则执行下面的语句。

  • proxy_pass http://phpservers:表示将请求转发到名为 phpservers 的后端服务器处理。

  • proxy_pass http://picservers:表示将请求转发到名为 picservers 的后端服务器处理,这个语句没有条件限制,如果以上两个if 语句都不匹配,则会执行这个语句。

接下来设置负载均衡对应的IP

定义负载均衡设备的IP

在nginx配置文件最后一行}前添加一下内容:

How to use nginx to implement a load balancing cluster with dynamic and static separation

代码如下:

 upstream  htmlservers {  
         server 192.168.1.42:80;   
         server 192.168.1.43:80;
 }
 upstream  phpservers{
         server 192.168.1.42:80;
         server 192.168.1.43:80;
 }
 upstream  picservers {
         server 192.168.1.42:80;
         server 192.168.1.43:80;
 }
Copy after login

配置文件是否有错误

[root@mufeng41 conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Copy after login

重启nginx

[root@mufeng41 conf]# /usr/local/nginx/sbin/nginx -s reload
Copy after login

五. 配置两台http服务器

接下来,需要在 mufeng42和mufeng43上操作

5.1 配置mufeng42服务器 配置web服务器:

[root@mufeng42 ~]# yum install httpd  php -y
Copy after login

生成静态测试文件

[root@mufeng42 ~]# echo 192.168.1.42  > /var/www/html/index.html
Copy after login

在创建一个php文件:

[root@itlaoxin162 ~]# vim /var/www/html/test.php
Copy after login

写入内容:

echo "我是42服务器";echo "我是沐风晓月"<?phpphpinfo();?>
Copy after login

启动apache

[root@mufeng42 ~]# systemctl  restart httpd
Copy after login

5.2 配置mufeng43服务器

安装http并生成静态文件

[root@mufeng43 ~]# yum install httpd php -y
[root@mufeng43 ~]# echo 192.168.1.43 > /var/www/html/index.html
Copy after login

建立php文件

[root@mufeng43 ~]# cd /var/www/html/

[root@mufeng43 html]# vi mufeng.php
[root@mufeng43 html]# cat mufeng.php 
echo "我是43服务器";
<?php
phpinfo();
?>
Copy after login

启动配置文件

[root@mufeng43 html]# systemctl restart httpd
Copy after login

5.3 测试

到目前为止,nginx负载均衡就结束了,接下来就可以测试了:

测试静态页面

浏览器输入: http://192.168.1.41/ 进行测试

How to use nginx to implement a load balancing cluster with dynamic and static separation

How to use nginx to implement a load balancing cluster with dynamic and static separation

测试转发动态页面:

浏览器输入 http://192.168.1.41/test.php

How to use nginx to implement a load balancing cluster with dynamic and static separation

The above is the detailed content of How to use nginx to implement a load balancing cluster with dynamic and static separation. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

See all articles