nginx:負荷分散とNFSの演習

1. Nginx負荷分散

環境を整える

1つをエージェントとして、2つをWebサーバーとして、3つの仮想マシンを準備します。(ここでは、私のプロキシサーバーはコンパイルおよびインストールされたnginxを使用し、2つのWebはyumによってインストールされたnginxを使用しています。)
起動して、ファイアウォールとselinuxをオフにします。
ネットワークのブロックが解除され、Baiduにpingできることを確認します。
nginxを起動します。

  • 開始する絶対パスをコンパイルしてインストールします:/ usr / local / nginx / sbin / nginx。グローバル変数を追加し、/ etc / profile.d /の下にスクリプト(nginx.sh)を作成し、スクリプトにexport PATH = $ {PATH}:/ usr / local / nginx / sbin /を追加して、すぐに有効にするには、スクリプトを入手できます。
  • Yumのインストール:systemctl start nginx

Webサーバーを構成する

実際、Webサーバーには構成するものはありませんが、最終的に表示される結果をわかりやすくするために、nginxのデフォルトのファイル公開ディレクトリの内容をここで少し変更します。

  • nginx設定ファイルでウェブサイトのデフォルトの公開ディレクトリを表示します
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf`

ここに画像の説明を挿入

  • このように両方のWebサーバーを検索できます
  • サーバーが構成されている

プロキシサーバーの構成

構成ファイルを開く

[root@localhost ~]# vim /etc/nginx/nginx.conf

サーバー上にカスタム名でアップストリームを作成します(ここではweb1を開始しています)。
ここに画像の説明を挿入

場所を見つけ、最初の2行をコメント化して、次のコードを追加します

            proxy_pass http://web1;
            proxy_redirect default;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            proxy_connect_timeout 30;
            proxy_send_timeout 60;
            proxy_read_timeout 60;
location / {
    
    
            #root   html;
            #index  index.html index.htm;
            proxy_pass http://web1;
            proxy_redirect default;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            proxy_connect_timeout 30;
            proxy_send_timeout 60;
            proxy_read_timeout 60;
        }

追加されたアップストリームの名前を次の場所の
ここに画像の説明を挿入
出口に変更し、構成ファイルを再読み込みします

[root@localhost ~]# nginx -s reload

アクセスプロキシサーバー

[root@localhost ~]# curl 192.168.49.144

ここに画像の説明を挿入

2. nfsを追加する

ファイアウォールとselinuxをオフにする

サーバー構成NFS

  • NFSをインストールする
 [root@localhost ~]# yum -y install nfs-utils
  • NFSを起動する
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl enable nfs-server
  • ディレクトリを作成し、ディレクトリにファイルを作成し、コンテンツを書き込みます。
[root@localhost ~]# mkdir /data
[root@localhost ~]# vim /data/index.html
  • このディレクトリのファイルを共有する
[root@localhost ~]# vim /etc/exports
  • 以下を追加
/data/index.html *(ro,sync)
  • 共有を更新
[root@localhost ~]# exportfs -rv

クライアント構成(2つのWebサーバー)

  • NFSをインストールする
[root@localhost ~]# yum -y install nfs-utils
  • ストレージ共有を表示
 [root@localhost ~]# showmount -e 192.168.49.145
  • 2つのサーバーにディレクトリを作成し、共有ディレクトリにマウントします。
 [root@localhost ~]# mkdir -p /mnt/nfs1
 [root@localhost ~]# vim /etc/fstab 
 192.168.49.145:/data/ /mnt/nfs1 nfs defaults 0 0
 [root@localhost ~]# mount -a
  • マウントされたディレクトリを共有するように、WebサーバーのWebサイト公開ディレクトリを変更します

ここに画像の説明を挿入

  • 設定ファイルを再読み込み
[root@localhost ~]# nginx -s reload
  • 自分のIPにアクセスする
[root@localhost ~]# curl 192.168.49.143

ここに画像の説明を挿入

  • このとき、プロキシサーバーにアクセスすると、nfsクライアントのindex.htmlのコンテンツが取得されます
    ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/weixin_49844466/article/details/108287988