Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the load balance of nginx in http

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

The following brings you how to achieve nginx in http Load Balancer, I hope to be able to give you some help in practical application, Load Balancer involves more things, theory is not much, there are many books on the Internet, today we will use the accumulated experience in the industry to do an answer.

First review LB Cluster Load Balancer Cluster

Level 4:

LVS

Nginx(stream)

Haproxy(mode_tcp)

seventh level

Http protocol

Nginx(http,upstream)

Haproxy(mode http)

Httpd/ats/perlbal/pound/…

Next how to implement nginx in http Load Balancer

The ngx_stream_proxy_module module can schedule http services, where the stream module has

Specialized server subcommands, unlike other servers, which are used to define virtual hosts

The server in the stream module is used to define a Cloud Virtual Machine in the group. Server can be reused many times.

Define multiple servers, so you can achieve Load Balancer for servers.

#################################################################

1. Prepare the experimental environment. Prepare at least three hosts, one of which is used as an nginx scheduling server and equipped with two network cards.

Configure nginx, httpd and httpd on three hosts respectively, and test that you can successfully access the page

[root@localhost nginx]# curl http://172.18.10.10:80/test1.html

Test Page 1 on UpStream Server 1 (172.18.10.10)

[root@localhost nginx]# curl http://172.18.10.11:80/test1.html

Test Page 1 on UpStream Server 2 (172.18.10.11)

Make 172.18.10.10 and 172.18.10.11 dynamic sites (install httpd+php, i.e. ap, listen 80)

172.18.10.10 and 172.18.10.11 as static sites (where 10.11 installs nginx and listens to 8080, 10.10 configures virtual hosts and listens to 80 and 8080)

2. Purpose of the experiment. Implement nginx Load Balancer for Static and Dynamic Content

3. Start configuration operation

Edit php page under 172.18.10.10

[root@localhost ~]# vim /var/www/html/index.php

HTTPD listend on 80 Server1

Send the experiment page to the page file storage path of 172.18.10.11

[root@localhost ~]# scp /var/www/html/index.php 172.18.10.11:/var/www/html/

Changing Server1 to Server2

HTTPD listend on 80 Server2

4. Common sense Use Google Chrome to request two addresses to see if the test page can be displayed properly--------

5. Configure nginx for static sites

scp the prepared nginx installation package to two hosts respectively

[root@localhost ~]# scp nginx-1.6.2-1.el6.ngx.x86_64.rpm 172.18.10.10:/root/

6, Install nginx

[root@localhost ~]# yum install nginx-1.6.2-1.el6.ngx.x86_64.rpm

7. Configure virtual services for static sites

172.18.10.10 Top:

Comment Root Path

#DocumentRoot "/var/www/html"

Add a new listening port

#Listen 12.34.56.78:80

Listen 80

Listen 8080

Add virtual hosts, listening on ports 80 and 8080 respectively

DocumentRoot /var/www/shope

ServerName www.magedu.com

DocumentRoot /var/www/html

ServerName imgs.magedu.com

save and exit

create a directory

[root@localhost ~]# mkdir /var/www/shope

Move index.php to this directory

[root@localhost ~]# mv /var/www/html/index.php /var/www/shope/

check the syntax

[root@localhost ~]# httpd -t

Restart httpd service

[root@localhost ~]# service httpd restart

Stopping httpd: [ OK ]

Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName

[ OK ]

Access test ports 80 and 8080 on the browser side, respectively

results are normal

172.18.10.11 Top:

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf

Change the virtual host listening port to 8080

listen 80-------------》 listen 8080;

Change root path

root /usr/share/nginx/html;-----》root /data/html;

Create Virtual Host Directory Path

[root@localhost ~]# mkdir /data/html -pv

mkdir: created directory `/data'

mkdir: created directory `/data/html'

The requested URL/data/was not found on this server.

[root@localhost ~]# mv /var/www/html/test* /data/html/

Start the nginx service and check if the port is listening.

[root@localhost ~]# nginx

[root@localhost ~]# ss -tnl

State Recv-Q Send-Q Local Address:Port Peer Address:Port

LISTEN 0 128 *:8080 *:*

LISTEN 0 128 :::80 :::*

LISTEN 0 128 :::22 :::*

LISTEN 0 128 *:22 *:*

LISTEN 0 100 ::1:25 :::*

LISTEN 0 100 127.0.0.1:25

Visit the page. See if you can access it properly.

8. Configure the nginx service on the nginx scheduling end on 172.18.200.100

[root@localhost ~]# vim /etc/nginx/nginx.conf

#Use the default weighted polling algorithm for binding

upstream websrvs {

server 172.18.10.10:80 weight=2 max_fails=2 fail_timeout=2;

server 172.18.10.11:80 weight=3;

}

upstream staticsrvs {

server 172.18.10.10:8080 weight=1;

server 172.18.10.11:8080 weight=1;

}

9. Method of editing scheduling

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf

index.php index.html; ####Global definition, order

location / {

proxy_pass http://websrvs; ####Dynamic resource loading path definition

root /usr/share/nginx/html;

index index.php index.html index.htm;

}

location ~* \. (jpg|jpeg|png|gif|html)$ {

proxy_pass http://staticsrvs; ####static resource load path definition

index index.php;

}

10. Load new tests

[root@localhost ~]# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@localhost ~]# nginx -s reload

Open Google Chrome, type http://172.18.200.100/Refresh the page and you will see the following page content switching back and forth

HTTPD listend on 80 Server2

HTTPD listend on 80 Server1

Request http://172.18.200.100/index.php, and find that the following page content switches back and forth

HTTPD listend on 80 Server2

HTTPD listend on 80 Server1

Use curl to test from other clients

[root@localhost ~]# for ((i=1;i

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report