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

Introduction to the process and steps of Nginx configuration for load balancing

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

The following is an introduction to the Nginx configuration process and steps of load balancing, hoping to give you some help in practical application. There are many things involved in load balancing, not many theories, and there are many books on the Internet. Today, we will use the accumulated experience in the industry to do an answer.

192.168.1.150 VOIP virtual IP

192.168.1.151 CNSHANK01 load balancer 1

192.168.1.152 CNSHANK02 load balancer 2

System: Centos7.4

1. Nginx configuration (the two nginx configurations are exactly the same, but the home page settings are different):

Installation:

Yum-y install nginx configuration:

Vim / etc/nginx/nginx.conf

User nginx

Worker_processes auto

Error_log / var/log/nginx/error.log

Pid / run/nginx.pid

Include / usr/share/nginx/modules/*.conf

Events {

Worker_connections 12800

Use epoll

}

Http {

Include mime.types

Default_type application/octet-stream

Limit_conn_zone $binary_remote_addr zone=addr:10m; # Anti-IP***

Log_format main'$remote_addr-$remote_user [$time_local] "$request"'

'$status $body_bytes_sent "$http_referer"'

'"$http_user_agent"$http_x_forwarded_for"'

Access_log / var/log/nginx/access.log main

Sendfile on

Tcp_nopush on

Tcp_nodelay on

Keepalive_timeout 65

Types_hash_max_size 2048

Include / etc/nginx/mime.types; # assists some browsers (such as firefox) to identify the types of web page files

Default_type application/octet-stream

Include / etc/nginx/conf.d/*.conf

Client_body_buffer_size 1k; # specify the buffer size of the client request body

Client_header_buffer_size 1k; # specifies the headerbuffer size from the client request header

Large_client_header_buffers 2 1k; # maximum number and size of cache specified by larger message headers in client requests

Server_tokens off; # prohibits the display of nginx version numbers on error pages

Client_body_timeout 20; # set the client request body read timeout

Client_header_timeout 20; # set the client request header read timeout

Gzip on

Upstream dynamic {# load CVM name dynamic

Ip hash; # adopts ip_hash mode

Server 192.168.1.154:80

Server 192.168.1.155:80

}

Server {

Listen 80 default_server

Listen [::]: 80 default_server

Server_name blog.dollarphp.com

Root / usr/share/nginx/html

Include / etc/nginx/default.d/*.conf

Location / {

Proxy_pass http://dynamic; # load balancing Agent

Limit_conn addr 100; # specifies that a maximum of 100 connections are allowed per ip

Limit_rate 500k; # maximum bandwidth of each ip is 500k

Proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404

Index index.html index.htm; autoindex on; autoindex_localtime on;} location * ^. +. (js | css | htm | html | gif | jpg | jpeg | png | bmp | swf | ioc | rar | txt | flv | mid | doc | pdf | xls | mp3 | wma) ${root html;} location * ^. +. (?! [js | css | htm | html | jpg | jpeg | bmp | swf | ioc | rar | zip | rar}) ${$rar $rar} Location = / 40x.html {} error_page 500 502 503 504 / 50x.html; location = / 50x.html {}

}

}

Set up the configuration home page test file

Cd / usr/share/nginx/html

Cp. / index.html. / index.html.bak

Echo "CNSHANK01-192.168.1.151" >. / index.html # on host 192.1681.151

Echo "CNSHANK02-192.168.1.152" >. / index.html # on host 192.1681.152

Start

Systemctl start nginx

Systemctl enable nginx

2. Keepalived configuration:

I. Master server configuration (192.168.1.151):

Installation:

Yum-y install keepalived

Configuration:

Vim / etc/keepalived/keepalived.conf

! Configuration File for keepalived

Global_defs {

}

Vrrp_script chk_nginx {

Script "/ etc/keepalived/check.sh"

Interval 1

Weight-15

}

Vrrp_instance VI_1 {# instance, one vip, one instance

State MASTER # identifies the primary service

Interface ens33

Virtual_router_id 51 # can be consistent with the slave computer.

Priority 100 # weight, higher than slave machine

Advert_int 1

Authentication {# authentication authorization, set verification information, the two nodes must be consistent

Auth_type PASS

Auth_pass 123456

}

Virtual_ipaddress {# Virtual ip address

192.168.1.150

}

Track_script {# nginx Monitoring Service

Chk_nginx

}

Notify_master "/ etc/keepalived/notify.sh master"

Notify_backup "/ etc/keepalived/notify.sh backup"

Notify_fault "/ etc/keepalived/notify.sh fault"

}

Add a service check script (check.sh):

Vim / etc/keepalived/check.sh

#! / bin/bash

If ["pgrep nginx | wc-l" = = "0"]; then

Exit 1

Else

Exit 0

Fi

Notification script (notify.sh):

Vim / etc/keepalived/notify.sh

#! / bin/bash

If ["$1" = = "master"]; then

Echo "date'+% F% titled: switch to host mode" > > / var/log/keepalived/keepalived.log

Elif ["$1" = = "backup"]; then

/ usr/bin/systemctl start nginx

Echo "date'+% F% Tunable: switch to standby mode" > > / var/log/keepalived/keepalived.log

Else

/ usr/bin/systemctl start nginx

Echo "date'+% F% titled: downtime" > > / var/log/keepalived/keepalived.log

Fi

Add executable permissions to the script:

Chmod + x / etc/keepalived/check.sh

Chmod + x / etc/keepalived/notify.sh

Create a log directory:

Mkdir / var/log/keepalived

Start:

Systemctl start keepalived

II, standby server configuration (192.168.1.152):

Installation:

Yum-y install keepalived

Configuration:

Vim / etc/keepalived/keepalived.conf

! Configuration File for keepalived

Global_defs {

}

Vrrp_script chk_nginx {

Script "/ etc/keepalived/check.sh"

Interval 1

Weight-15

}

Vrrp_instance VI_1 {

State BACKUP # BACKUP mode

Interface ens33

Virtual_router_id 51 # is consistent with the host

Priority 99 # is smaller than the host

Advert_int 1

Authentication {# is consistent with the host

Auth_type PASS

Auth_pass 123456

}

Virtual_ipaddress {

192.168.1.150

}

Track_script {

Chk_nginx

}

Notify_master "/ etc/keepalived/notify.sh master"

Notify_backup "/ etc/keepalived/notify.sh backup"

Notify_fault "/ etc/keepalived/notify.sh fault"

}

Add a service check script (check.sh):

Vim / etc/keepalived/check.sh

#! / bin/bash

If ["pgrep nginx | wc-l" = = "0"]; then

Exit 1

Else

Exit 0

Fi Notification script (notify.sh):

Vim / etc/keepalived/notify.sh

#! / bin/bash

If ["$1" = = "master"]; then

Echo "date'+% F% titled: switch to host mode" > > / var/log/keepalived/keepalived.log

Elif ["$1" = = "backup"]; then

/ usr/bin/systemctl start nginx

Echo "date'+% F% Tunable: switch to standby mode" > > / var/log/keepalived/keepalived.log

Else

/ usr/bin/systemctl start nginx

Echo "date'+% F% titled: downtime" > > / var/log/keepalived/keepalived.log

Fi adds executable permissions to the script:

Chmod + x / etc/keepalived/check.sh

Chmod + x / etc/keepalived/notify.sh create log directory:

Mkdir / var/log/keepalived startup:

Systemctl start keepalived

3. Testing:

I. under normal condition

II, stop the master server: test after 192.168.1.151

Stop the slave server nginx service, and as a result, you can see that the access to the virtual IP will be cut from the master server to the slave server.

Systemctl stop nginx

View the log:

Tail / var/log/keepalived/keepalived.log

Master and slave logs all show that they are switched to standby mode.

Test with domain name

Modify the local hosts file C:\ Windows\ System32\ drivers\ etc\ hosts to join

192.168.1.150 www.bbs.com

After reading the above introduction about the Nginx configuration process and steps of load balancer, if there is anything else you need to know, you can find out what you are interested in in the industry information or find our professional and technical engineers for answers. Technical engineers have more than ten years of experience in the industry. Official website link www.yisu.com

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