一、反向代理
1 環境說明

nginx 使用 YUM 方式安裝,版本是 1.18
2 配置步驟
2.1 后端 web 服務器 h2上操作
啟動 Nginx 服務
systemctl start nginx && systemctl enable nginx
修改 web 服務器頁面內容
首先我們配置 h2 ,讓他作為 web 服務器,提供一個頁面內容的展示
查看并確認 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; # 頁面檔案名稱
}
修改 /usr/share/nginx/html/index.html 檔案的內容如下:
真實 web 服務器 h2
測驗是否可以訪問 web 服務
首先在 h2 上執行如下命令訪問網站
[root@h2 ~]# curl h2
真實 web 服務器 h2
之后在 h1 上訪問網站
[root@h1 ~]# curl h2
真實 web 服務器 h2
[root@h1 ~]#
2.2 反向代理服務器 h1上操作
啟動 Nginx 服務
systemctl start nginx && systemctl enable nginx
配置代理
把 /etc/nginx/conf.d/default.conf 檔案的內容修改成為如下內容
[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;
}
}
重新加載 Nginx 的組態檔
[root@h1 ~]# nginx -s reload
測驗
在客戶端機器 h3 上訪問代理服務器 h1
[root@h3 ~]# curl h1
真實 web 服務器 h2
[root@h3 ~]#
二、負載均衡
1 環境說明

在物理宿主機上操作
添加 2臺主機到 docer-compose.yml 檔案中
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
啟動他們
[xiguatian@development project]$ docker-compose up -d h4 h5
2 配置步驟
2.1 分別在 h4 和 h5 服務器上部署 Nginx 并設定開啟自啟
創建 /etc/yum.repos.d/nginx.repo 檔案并寫入如下內容:
[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
接著執行入下命令安裝 Nginx
yum -y install nginx
最后執行如下命令啟動 Nginx 服務器,并設定為開機自啟
systemctl start nginx && systemctl enable nginx
2.2 配置 web 服務器 h4 和 h5 的頁面內容
修改 h4 的默認頁面內容
修改 /usr/share/nginx/html/index.html 檔案的內容如下:
[root@h4 ~]# echo "真實 web 服務器 h4" > /usr/share/nginx/html/index.html
修改 h5 的默認頁面內容
修改 /usr/share/nginx/html/index.html 檔案的內容如下:
[root@h5 ~]# echo "真實 web 服務器 h5" > /usr/share/nginx/html/index.html
測驗在代理服務器行是否可以訪問 web 服務
首先在 h1 上執行如下命令訪問 h4 和 h5 的網站
[root@h1 ~]# curl h4
真實 web 服務器 h4
[root@h1 ~]# curl h5
真實 web 服務器 h5
[root@h1 ~]#
2.3 配置代理服務器 h1
在代理服務器 h1 上操作
修改 h1 的 Nginx 組態檔 /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;
}
}
重新加載 Nginx 組態檔
[root@h1 ~]# nginx -s reload
2.4 測驗代理服務器是否有效
在客戶端 h3 上操作
在 h3 上連續訪問 h1 4 次
[root@h3 ~]# curl h1
真實 web 服務器 h2
[root@h3 ~]# curl h1
真實 web 服務器 h4
[root@h3 ~]# curl h1
真實 web 服務器 h5
[root@h3 ~]# curl h1
真實 web 服務器 h2
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/189552.html
標籤:其他
