One, reverse proxy
1 Environmental description
nginx uses YUM method to install, version is 1.18
2 configuration steps
2.1 Operations on the back-end web server h2
Start Nginx service
systemctl start nginx && systemctl enable nginx
Modify web server page content
First, we configure h2 to serve as a web server to provide a display of page content
Check and confirm the page root directory of Nginx
[root@h2 ~]# grep -C 3 'location /' /etc/nginx/conf.d/default.conf
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html; # 页面目录
index index.html index.htm; # 页面文件名称
}
Modify the /usr/share/nginx/html/index.html
contents of the file are as follows:
真实 web 服务器 h2
Test whether you can access the web service
First execute the following command on h2 to visit the website
[root@h2 ~]# curl h2
真实 web 服务器 h2
Then visit the website on h1
[root@h1 ~]# curl h2
真实 web 服务器 h2
[root@h1 ~]#
2.2 Operation on the reverse proxy server h1
Start Nginx service
systemctl start nginx && systemctl enable nginx
Configure proxy
The /etc/nginx/conf.d/default.conf
contents of the file to be modified following
[root@h1 ~]# egrep -v '^[ \t]*#|^$' /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
location / {
# 这里就是配置代理的地方,使用关键字 proxy_pass
# 后面跟的是真实服务器IP或者主机名加端口号,这里是 h2
proxy_pass http://h2:80;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Reload the Nginx configuration file
[root@h1 ~]# nginx -s reload
test
Access the proxy server h1 on the client machine h3
[root@h3 ~]# curl h1
真实 web 服务器 h2
[root@h3 ~]#
Two, load balancing
1 Environmental description
Operate on the physical host
Add 2 hosts to the docer-compose.yml file
h4:
image: centos7-sshd
container_name: h4
hostname: h4.sharkyun.com
privileged: true
command: /usr/sbin/init
networks:
xiuyun_net:
ipv4_address: 192.16.2.40
h5:
image: centos7-sshd
container_name: h5
hostname: h5.sharkyun.com
privileged: true
command: /usr/sbin/init
networks:
xiuyun_net:
ipv4_address: 192.16.2.50
Start them
[xiguatian@development project]$ docker-compose up -d h4 h5
2 configuration steps
2.1 Deploy Nginx on h4 and h5 servers and set to enable self-start
Create a /etc/yum.repos.d/nginx.repo
file and write the following:
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
Then execute the following command to install Nginx
yum -y install nginx
Finally, execute the following command to start the Nginx server and set it to self-start after booting
systemctl start nginx && systemctl enable nginx
2.2 Configure the page content of the web server h4 and h5
Modify the default page content of h4
Modify the /usr/share/nginx/html/index.html
contents of the file are as follows:
[root@h4 ~]# echo "真实 web 服务器 h4" > /usr/share/nginx/html/index.html
Modify the default page content of h5
Modify the /usr/share/nginx/html/index.html
contents of the file are as follows:
[root@h5 ~]# echo "真实 web 服务器 h5" > /usr/share/nginx/html/index.html
Test whether the web service can be accessed in the proxy server line
First, execute the following commands on h1 to visit the websites of h4 and h5
[root@h1 ~]# curl h4
真实 web 服务器 h4
[root@h1 ~]# curl h5
真实 web 服务器 h5
[root@h1 ~]#
2.3 Configure the proxy server h1
Operate on the proxy server h1
Modify the Nginx configuration file of h1 /etc/nginx/conf.d/default.conf
upstream testapp {
server h2:80;
server h4:80;
server h5:80;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://testapp;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Reload Nginx configuration file
[root@h1 ~]# nginx -s reload
2.4 Test whether the proxy server is valid
Operate on client h3
Visit h1 4 times consecutively on h3
[root@h3 ~]# curl h1
真实 web 服务器 h2
[root@h3 ~]# curl h1
真实 web 服务器 h4
[root@h3 ~]# curl h1
真实 web 服务器 h5
[root@h3 ~]# curl h1
真实 web 服务器 h2