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 use nginx+Apache load balancing and static and dynamic separation

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

Share

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

The following brings you how to use nginx+Apache load balancing and static separation, hoping to give you some help in practical application. Load balancing involves more things, there are 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.

Using nginx+Apache load balancing and static and dynamic separation

Introduction

LB load balancer clusters are divided into two categories: LVS (layer 4) and nginx or haproxy (layer 7)

The client accesses the website by visiting the VIP of the dispenser. The pages of the website in the seven layers are: .php. .html. PNG. Jpeg. Jsp, etc., dynamic pages and static pages. It needs to be distributed at the application layer based on different applications.

One: experimental topology diagram:

Second: experimental objectives

Actual combat: using Apache+nginx to achieve dynamic and static separation of load balancing cluster

Three: experimental environment

Host function classification

Hostnam

IP address

Install softwar

Nginx, proxy CVM

Xuegod63.cn

192.168.1.63

Nginx-1.8.0.tar.gz

Apache, static page processing

Xuegod62.cn

192.168.1.62

Http

Apache, static page processing

Xuegod64.cn

192.168.1.64

Http

Apache, dynamic page processing

Xuegod62.cn

192.168.1.62

Http

Apache, dynamic page processing

Xuegod64.cn

192.168.1.64

Http

Apache, image processing

Xuegod62.cn

192.168.1.62

Http

Apache, image processing

Xuegod64.cn

192.168.1.64

Http

Four: experimental code

1. Configure the distributor xuegod63 (proxy server)

1) when installing nginx, you must first install the corresponding compilation tools.

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

[root@xuegod63 ~] # yum-y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

Zlib: nginx provides gzip module, which needs zlib library support.

Openssl:nginx provides ssl function

Pcre: support address rewriting rewrite function

2) install nginx:

[root@xuegod63] # tar-zxvf nginx-1.8.0.tar.gz-C / usr/local/src/

[root@xuegod63 ~] # cd / usr/local/src/nginx-1.8.0/

[root@xuegod63 nginx-1.8.0] # / configure-- prefix=/server/nginx-1.8.0-- 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

Parameter explanation:

-- with-http_dav_module enable ngx_http_dav_module support (add PUT,DELETE,MKCOL: create collections, COPY and MOVE methods) turn it off by default and enable it if compiled

-- with-http_stub_status_module enables ngx_http_stub_status_module support (gets the working status of nginx since it was last started)

-- with-http_addition_module enables ngx_http_addition_module support (as an output filter that supports incomplete buffering and partial response to requests)

-- with-http_sub_module enables ngx_http_sub_module support (allows some text in the nginx response to be replaced with some other text)

-- with-http_flv_module enables ngx_http_flv_module support (provides time-based offset files for seeking memory usage)

-- with-http_mp4_module enables support for mp4 files (provides time-based offset files for seeking memory use)

3) compile and install:

[root@xuegod63 nginx-1.8.0] # make-j 4

[root@xuegod63 nginx-1.8.0] # make install

4) generate users running nginx:

[root@xuegod63 nginx-1.8.0] # useradd-u 8000-s / sbin/nologin nginx

5) start nginx:

[root@xuegod63 /] # / server/nginx-1.8.0/sbin/nginx

[root@xuegod63 nginx-1.8.0] # echo'/ server/nginx-1.8.0/sbin/nginx &'> / etc/rc.local

6) Test: http://192.168.1.63/

7) Daily operation of nginx service:

(1) Test whether the configuration file has the correct syntax:

[root@xuegod63 nginx-1.8.0] # / server/nginx-1.8.0/sbin/nginx-t

Nginx: the configuration file / server/nginx-1.8.0/conf/nginx.conf syntax is ok

Nginx: configuration file / server/nginx-1.8.0/conf/nginx.conf test is successful

(2) reload the configuration file

[root@xuegod63 nginx-1.8.0] # / server/nginx-1.8.0/sbin/nginx-s reload

(3) disable and enable nginx

[root@xuegod63 /] # / server/nginx-1.8.0/sbin/nginx-s stop

[root@xuegod63 /] # / server/nginx-1.8.0/sbin/nginx-s start # has no start parameter

Nginx: invalid option: "- s start"

7) configure nginx to become a distributor to achieve static and dynamic separation

[root@xuegod63 conf] # cd / server/nginx-1.8.0/conf # configuration file directory

[root@xuegod63 conf] # cp nginx.conf nginx.conf.back # back up the configuration file

[root@xuegod63 conf] # vim nginx.conf

[root@xuegod63 nginx-1.8.0] # vim / server/nginx-1.8.0/conf/nginx.conf # specifies to launch the nginx user

Change: # user nobody

Is: user nginx nginx

Change:

43 location / {

44 root html

45 index index.html index.htm; # in location / {. } add the following # to define the distribution policy

Index index.html index.htm

If ($request_uri ~ *\ .html $) {

Proxy_pass http://htmlservers; # matches to htm static type access and goes to the html service pool

}

If ($request_uri ~ *\ .php $) {

Proxy_pass http://phpservers; # matches to php dynamically typed files are parsed directly on the nginx server

}

}

As shown in the figure:

Comment out the following content, match it to the php dynamic type file and parse it directly on the nginx server, instead of parsing it to the back-end server:

As shown in the figure:

8) before the last line} of the configuration file nginx.conf, add the following:

Upstream htmlservers {# defines static file load balancing server group name

Server 192.168.1.62:80

Server 192.168.1.64:80

}

Upstream phpservers {# defines dynamic file load balancing server group name

Server 192.168.1.62:80

Server 192.168.1.64:80

}

Upstream picservers {# defines the name of the load balancing server group for picture files

Server 192.168.1.62:80

Server 192.168.1.64:80

}

# in the later work, configure it as the IP address of the specific business according to the needs of the work.

As shown in the figure:

9) reload the nginx server configuration file:

[root@xuegod63 conf] # / server/nginx-1.8.0/sbin/nginx-t

Nginx: the configuration file / server/nginx-1.8.0/conf/nginx.conf syntax is ok

Nginx: configuration file / server/nginx-1.8.0/conf/nginx.conf test is successful

[root@xuegod63 conf] # / server/nginx-1.8.0/sbin/nginx-s reload

2. Configure the backend server: xuegod62

(1) configure web server:

[root@xuegod62 html] # yum install httpd php-y

(2) generate static test files:

Root@xuegod62 html] # echo 192.168.1.62 > / var/www/html/index.html

(3) generate dynamic test files:

[root@xuegod62 html] # vim / var/www/html/test.php # is written as follows:

192.168.1.62-php

(4) generate image files: upload the following images to the "xuegod62 website / var/www/html/ directory:

(5) start the apache server:

[root@xuegod62 html] # service httpd restart

3. Configure the backend server: xuegod64

(1) configure web server:

[root@xuegod64 html] # yum install httpd php-y

(2) generate static test files:

[root@xuegod64 html] # echo 192.168.1.64 > / var/www/html/index.html

(3) generate dynamic test files:

[root@xuegod64 html] # vim / var/www/html/test.php # is written as follows:

192.168.1.64-php

(4) generate image file:-- upload the following images to "xuegod64 website / var/www/html/ directory:"

(5) restart the apache server

[root@xuegod64 html] # service httpd restart

4. Test

(1) Test load balancing and static separation-static pages:

=

(2) Test dynamic and static separation and load balancing-dynamic pages:

(3) Test the image load balance:

(4) Test automatically removes bad nodes:

[root@xuegod64 html] # service httpd stop

Http://192.168.1.63/pic.jpg

5. Test performance:

Extension: too many file openings

[root@xuegod63 html] # ab-n 1000-c 1000 http://192.168.1.62/index.html # is running normally

[root@xuegod63 html] # ab-n 2000-c 2000 http://192.168.1.62/index.html # reported an error

This is ApacheBench, Version 2.3

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.1.62 (be patient)

Socket: Too many open files (24) # when testing, too many socket files were opened at a time.

[root@xuegod63 ~] # ulimit-n

By default, one process is allowed to open up to 1024 files at the same time.

[root@xuegod63 ~] # ulimit-n 10240 # modification allows colleagues to open 10240 files by default

[root@xuegod63] # ab-n 2000-c 2000 http://192.168.1.62/index.html

This is ApacheBench, Version 2.3

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.1.62 (be patient)

Completed 200 requests

Completed 400 requests

.

Completed 1800 requests

Completed 2000 requests

Finished 2000 requests

Server Software: Apache/2.2.15

Server Hostname: 192.168.1.62

Server Port: 80

Document Path: / index.html

Document Length: 13 bytes

Concurrency Level: 2000

Time taken for tests: 1.119 seconds

Complete requests: 2000

Failed requests: 0

Write errors: 0

Total transferred: 560000 bytes

HTML transferred: 26000 bytes

Requests per second: 1787.69 [# / sec] (mean)

Time per request: 1118.765 [ms] (mean)

Time per request: 0.559 [ms] (mean, across all concurrent requests)

Transfer rate: 488.82 [Kbytes/sec] received

Connection Times (ms)

Min mean [+ /-sd] median max

Connect: 0 56 216.7 1 1062

Processing: 4 71 161.9 24 670

Waiting: 4 70 161.9 24 670

Total: 16 127 271.1 26 1087

Percentage of the requests served within a certain time (ms)

50% 26

66% 26

75% 27

80% 57

90% 717

95% 727

98% 1085

99% 1086

1087 (longest request)

After reading the above about how to use nginx+Apache load balancing and static separation, if you have 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.

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