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
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
[root@mufeng41 ~]# yum -y install gcc gcc-c++ autoconf automake [root@mufeng41 ~]# yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
[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/
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]#
./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
Installation
How to determine whether the execution is successful?[root@mufeng41 nginx-1.12.2]# make && make install
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]#
.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:*
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
[root@mufeng41 conf]# pwd /usr/local/nginx/conf [root@mufeng41 conf]# cp nginx.conf nginx.conf.bak [root@mufeng41 conf]#
配置如下图:
配置分发器
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; }
注释:
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配置文件最后一行}前添加一下内容:
代码如下:
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; }
配置文件是否有错误
[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
重启nginx
[root@mufeng41 conf]# /usr/local/nginx/sbin/nginx -s reload
接下来,需要在 mufeng42和mufeng43上操作
[root@mufeng42 ~]# yum install httpd php -y
生成静态测试文件
[root@mufeng42 ~]# echo 192.168.1.42 > /var/www/html/index.html
在创建一个php文件:
[root@itlaoxin162 ~]# vim /var/www/html/test.php
写入内容:
echo "我是42服务器";echo "我是沐风晓月"
启动apache
[root@mufeng42 ~]# systemctl restart httpd
安装http并生成静态文件
[root@mufeng43 ~]# yum install httpd php -y [root@mufeng43 ~]# echo 192.168.1.43 > /var/www/html/index.html
建立php文件
[root@mufeng43 ~]# cd /var/www/html/ [root@mufeng43 html]# vi mufeng.php [root@mufeng43 html]# cat mufeng.php echo "我是43服务器";
启动配置文件
[root@mufeng43 html]# systemctl restart httpd
到目前为止,nginx负载均衡就结束了,接下来就可以测试了:
测试静态页面
浏览器输入: http://192.168.1.41/ 进行测试
测试转发动态页面:
浏览器输入 http://192.168.1.41/test.php
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!