search
HomeBackend DevelopmentPHP Tutorialkeepalived+nginx realizes the high availability of nginx

keepalived+nginx realizes the high availability of nginx

================================ ===

nginx high availability

nginx realizes load balancing of back-end realserver

================== =================


Experimental environment: OS: Centos 6.4( redhat 6.4)yum source:

1

2

3

4

5

6

7

8

9

10

11

[centos]

name=sohu-centos

baseurl=http://mirrors.sohu.com/centos/ $releasever /os/$basearch

gpgcheck=1

enable=0

gpgkey=http://mirrors.sohu.com /centos/RPM- GPG-KEY-CentOS-6

[epel]

name=sohu-epel

baseurl=http://mirrors.sohu.com/fedora-epel/$releasever/$basearch/

enable=1

gpgcheck=0


Topology map:

Planning of topology map:


IPAddress

Software

Master

172.16.22.1(VIP : 172.16.22.100)

keepalived+nginx

Backup

172.16.22.2 (VIP 172.16.22.100)

keepalived+ nginx

apache1

172.16.22.3

httpd

apache2

172. 16.22.4

httpd

Issues to consider with this architecture

1), Master is not down, then Master occupies VIP and nginx runs on Master

2), Master is down , the backup seizes the VIP and runs the nginx service on the backup

3). If the nginx service on the master server hangs, the VIP resources are transferred to the backup server

4), detection The health status of the backend server

Both Master and Backup have nginx services enabled. Regardless of Master or Backup, when one of the keepalived services stops, the VIP will drift to the node where the keepalived service is still there,

If you want the nginx service to hang up and the vip to drift to another node, you must use a script or use a shell command in the configuration file to control it.

First of all, it must be clear that keepalived cannot detect the health status of the backend server on this architecture. The health status detection of the backend server is judged by nginx, but the detection mechanism of nginx has certain flaws. , after a certain backend server goes down, nginx will still distribute requests to it. If the backend service cannot respond within a certain period of time, nginx will send it to another server. Then when the customer's request comes, nginx will wait for a while. The request will not be distributed to the down server, but after a period of time, nginx will still send the distribution request to the down server.

1. Install keepalived+nginx

Master:

1. Install keepalived and compile and install ngin x

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

[root@jie1 ~]# yum -y install keepalived

[root@jie1 ~]#tar xf nginx-1.4.2.tar.gz

[root@jie1 ~]#yum -y groupinstall "Development tools" "Server Platform Development"

[root@jie1 ~]#yum -y install pcre-devel

[root@jie1 ~]# cd nginx-1.4.2

[root@jie1 nginx-1.4.2]# groupadd nginx

[root@jie1 nginx-1.4.2]# useradd -r -g nginx nginx

[root@jie1 nginx-1.4.2]#./configure

--prefix=/usr

--sbin-path=/usr/sbin/nginx

--conf-path=/etc/nginx/nginx.conf

--error-log-path=/var/log/nginx/error.log

--http-log-path=/var/log/nginx/access.log

--pid-path=/var/run/nginx/nginx.pid 

--lock-path=/var/lock/nginx.lock

--user=nginx

--group=nginx

--with-http_ssl_module

--with-http_flv_module

--with-http_stub_status_module

--with-http_gzip_static_module

--http-client-body-temp-path=/var/tmp/nginx/client/

--http-proxy-temp-path=/var/tmp/nginx/proxy/

--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/

--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi

--http-scgi-temp-path=/var/tmp/nginx/scgi

--with-pcre

[root@jie1 nginx-1.4.2]# make && make install

2. Provide nginx system V service script file

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

6 3

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

[root@jie1 nginx-1.4.2]# vim /etc/rc.d/init.d/nginx

#!/bin/sh

#

# nginx - this script starts and stops the nginx daemon

#

# chkconfig: - 85 15

# description: Nginx is an HTTP(S) server, HTTP(S) reverse

# proxy and IMAP/POP3 proxy server

# processname: nginx

# config: /etc/nginx/nginx.conf

# config: /etc/sysconfig/nginx

# pidfile:     /var/run/nginx.pid

# Source function library.

/etc/rc.d/init.d/functions

# Source networking configuration.

/etc/sysconfig/network

# Check that networking is up.

"$NETWORKING""no"] && exit0

nginx="/usr/sbin/nginx"

prog=$(basename$nginx)

NGINX_C/code>"/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {

# make required directories

user=`nginx -V 2>&1 | grep"configure arguments:"sed's/[^*]*--user=([^ ]*).*/1/g'-`

opti -V 2>&1 | grep'configure arguments:'`

foropt in$options; do

if[ `echo$opt | grep'.*-temp-path'` ]; then

value=`echo$opt | cut-d "="-f 2`

if[ ! -d "$value"]; then

# echo "creating" $value

mkdir-p $value && chown-R $user $value

fi

fi

done

}

start() {

[ -x $nginx ] || exit5

[ -f $NGINX_CONF_FILE ] || exit6

make_dirs

echo-n $"Starting $prog: "

daemon $nginx -c $NGINX_CONF_FILE

retval=$?

echo

[ $retval -eq0 ] && touch$lockfile

return$retval

}

stop() {

echo-n $"Stopping $prog: "

killproc $prog -QUIT

retval=$?

echo

[ $retval -eq0 ] && rm-f $lockfile

return$retval

}

restart() {

configtest || return$?

stop

sleep1

start

}

reload() {

configtest || return$?

echo-n $"Reloading $prog: "

killproc $nginx -HUP

RETVAL=$?

echo

}

force_reload() {

restart

}

configtest() {

$nginx -t -c $NGINX_CONF_FILE

}

rh_status() {

status $prog

}

rh_status_q() {

rh_status >/dev/null2>&1

}

case"$1"in

start)

rh_status_q && exit0

$1

;;

stop)

rh_status_q || exit0

$1

;;

restart|configtest)

$1

;;

reload)

rh_status_q || exit7

$1

;;

force-reload)

force_reload

;;

status)

rh_status

;;

condrestart|try-restart)

rh_status_q || exit0

;;

*)

echo$"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"

exit2

esac

[root@jie1 nginx-1.4.2]# chmod +x /etc/rc.d/init.d/nginx

[root@jie1 nginx-1.4.2]# service nginx start

Starting nginx:                                                                                                                       nginx-1.4.2]

# scp -p /etc/rc.d/init.d/nginx 172.16.22.2:/etc/rc.d/init.d #Copy the nginx service script to the backup, -p means to maintain the original permissions

3. Modify configuration file

12

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

[root@jie1 ~]

# cd /etc/keepalived/

[root@jie1 keepalived]

# vim keepalived.conf

global_defs {

​​

notification_email {

root@localhost

Statement
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
What are some common problems that can cause PHP sessions to fail?What are some common problems that can cause PHP sessions to fail?Apr 25, 2025 am 12:16 AM

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

How do you debug session-related issues in PHP?How do you debug session-related issues in PHP?Apr 25, 2025 am 12:12 AM

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

What happens if session_start() is called multiple times?What happens if session_start() is called multiple times?Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

How do you configure the session lifetime in PHP?How do you configure the session lifetime in PHP?Apr 25, 2025 am 12:05 AM

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

What are the advantages of using a database to store sessions?What are the advantages of using a database to store sessions?Apr 24, 2025 am 12:16 AM

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

How do you implement custom session handling in PHP?How do you implement custom session handling in PHP?Apr 24, 2025 am 12:16 AM

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

What is a session ID?What is a session ID?Apr 24, 2025 am 12:13 AM

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

How do you handle sessions in a stateless environment (e.g., API)?How do you handle sessions in a stateless environment (e.g., API)?Apr 24, 2025 am 12:12 AM

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.

See all articles

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

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.