nginx負載均衡實例

来源:https://www.cnblogs.com/walk1314/archive/2018/08/20/9480590.html
-Advertisement-
Play Games

實例整體框架: 使用VMware搭建 5台Centos7虛擬機(包括客戶端),系統版本:CentOS Linux release 7.2.1511。實例所安裝的nginx版本:1.12.2,mariadb-server版本:5.5.56,php-fpm版本:5.4.16,PHPMyAdmin版本:4 ...


實例整體框架:

 

 使用VMware搭建 5台Centos7虛擬機(包括客戶端),系統版本:CentOS Linux release 7.2.1511。實例所安裝的nginx版本:1.12.2,mariadb-server版本:5.5.56,php-fpm版本:5.4.16,PHPMyAdmin版本:4.0.10.20。此實例所有虛擬機均已關閉防火牆並設置selinux為Permissive(systemctl stop firewalld.service,setenforce 0)。

搭建web server:

1、安裝php-fpm和mariadb-server並創建web資源存放目錄:

[root@webserver Desktop]# yum install -y php php-fpm php-mbstring mariadb-server php-mysql
[root@webserver Desktop]# mkdir /data/html

2、配置php-fpm:

#配置php-fpm
[root@webserver Desktop]# vim /etc/php-fpm.d/www.conf
    listen = 0.0.0.0:9000
    listen.allowed_clients = 10.10.0.11,10.10.0.12
    pm.status_path = /status
    ping.path = /ping
    ping.response = pong
    php_value[session.save_handler] = files
    php_value[session.save_path] = /var/lib/php/session
#設置會話session文件屬主屬組
[root@webserver Desktop]# chown apache:apache /var/lib/php/session
[root@webserver Desktop]# ll -d /var/lib/php/session
    drwxrwx---. 2 apache apache 4096 Aug 20 15:50 /var/lib/php/session/
[root@webserver Desktop]# systemctl start php-fpm.service
[root@webserver Desktop]# ss -tan
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128          *:9000                     *:*                  
LISTEN     0      5      192.168.122.1:53                       *:*                  
LISTEN     0      128          *:22                       *:*                  
LISTEN     0      128    127.0.0.1:631                      *:*                  
LISTEN     0      100    127.0.0.1:25                       *:*                  
LISTEN     0      128         :::22                      :::*                  
LISTEN     0      128        ::1:631                     :::*                  
LISTEN     0      100        ::1:25                      :::*

3、創建index.php頁面並並下載PHPMyAdmin和WordPress:

[root@webserver Desktop]# cd /data/html
[root@webserver html]# vim index.php
    <h1> 10.10.0.13 server</h1>
    <?php
        phpinfo();
    ?>
[root@webserver html]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
[root@webserver html]# wget https://files.phpmyadmin.net/phpMyAdmin/4.0.10.20/phpMyAdmin-4.0.10.20-all-languages.tar.gz
[root@webserver html]# tar xf wordpress-4.9.4-zh_CN.tar.gz
[root@webserver html]# tar xf phpMyAdmin-4.0.10.20-all-languages.tar.gz
[root@webserver html]# ln -sv phpMyAdmin-4.0.10.20-all-languages phpmyadmin
#配置wordpress所用資料庫
[root@webserver html]# cp /data/html/wordpress/wp-config-sample.php /data/html/wordpress/wp-config.php
[root@webserver html]# vim /data/html/wordpress/wp-config.php
    define('DB_NAME', 'wordpress');
    define('DB_USER', 'wpuser');
    define('DB_PASSWORD', '12345678');
    define('DB_HOST', 'localhost');
    define('DB_CHARSET', 'utf8');

4、配置mariadb:

[root@webserver html]# vim /etc/my.cnf
    [mysqld]
    skip_name_resolve=ON
    innodb_file_per_table=ON
root@webserver html]# systemctl start mariadb.service
#設置mariadb的安全許可權
root@webserver html]# mysql_secure_installation
...
#創建wordpress資料庫並授權wpuser操作許可權,跟wordpress配置文件保持一致
root@webserver html]# mysql -uroot -p
Enter password: 
MariaDB [(none)]> create database wordpress;
MariaDB [(none)]> grant all on wordpress to 'wpuser'@'%' identified by '12345678';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> exit;

搭建nginx1:

1、安裝Nginx並創建web資源存放目錄

[root@nginx1 Desktop]# yum install -y nginx
[root@nginx1 Desktop]# mkdir -pv /data/html

2、創建index.html預設頁面並下載PHPMyAdmin和WordPress

[root@nginx1 Desktop]# cd /data/html
[root@nginx1 html]# vim index.html
    <h1>this is 10.10.0.11 nginx</h1>
[root@nginx1 html]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
[root@nginx1 html]# wget https://files.phpmyadmin.net/phpMyAdmin/4.0.10.20/phpMyAdmin-4.0.10.20-all-languages.tar.gz
[root@nginx1 html]# tar xf wordpress-4.9.4-zh_CN.tar.gz
[root@nginx1 html]# tar xf phpMyAdmin-4.0.10.20-all-languages.tar.gz
[root@nginx1 html]# ln -sv phpMyAdmin-4.0.10.20-all-languages phpmyadmin
#配置wordpress所用資料庫
[root@nginx1 html]# cp /data/html/wordpress/wp-config-sample.php /data/html/wordpress/wp-config.php
[root@nginx1 html]# vim /data/html/wordpress/wp-config.php
    define('DB_NAME', 'wordpress');
    define('DB_USER', 'wpuser');
    define('DB_PASSWORD', '12345678');
    define('DB_HOST', 'localhost');
    define('DB_CHARSET', 'utf8');

3、配置虛擬主機並啟動nginx:

[root@nginx1 html]# vim /etc/nginx/nginx.conf
#註釋nginx預設的主機配置
    ...
    server {
#        listen       80 default_server;
#        listen       [::]:80 default_server;
    ...
[root@nginx1 html]# vim /etc/nginx/conf.d/vhost.conf   #配置虛擬主機,頁面動靜分離
  server {
    listen 80;
    server_name www.test.org;
    index index.html;
    location / {
         root /data/html;
    }
    location ~* \.php$ {
      fastcgi_pass 10.10.0.13:9000;
      fastcgi_index index.php;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME /data/html/$fastcgi_script_name;
    }
    location ~* ^/(status|ping)$ {
      fastcgi_pass 10.10.0.13:9000;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
    }
    }
[root@nginx1 html]# systemctl start nginx.service

nginx2配置過程同nginx1.

搭建Nginx SLB:

安裝nginx併進行負載均衡配置:

[root@SLB Desktop]# yum -y install nginx
[root@SLB Desktop]# vim /etc/nginx/nginx
#在http欄位進行以下配置
    http {
        ...
        #定義集群
        upstream webservers {
            server 10.10.0.11:80 max_fails=3;
            server 10.10.0.12:80 max_fails=3;
            server 127.0.0.1:80 backup;
        }
        server {
        ...
        location / {
                proxy_pass http://webservers;   #反代給集群伺服器
                proxy_set_header host $http_host;   #設置代理請求報文的host欄位為$http_host
                proxy_set_header X-Forward-For $remote_addr;       #為代理請求報文添加X-Forward-For欄位以傳遞真實ip地址$remote_addr
            }
            ...
        }
[root@SLB Desktop]# systemctl start nginx.service

客戶端進行訪問:

1、修改hosts:

[root@client Desktop]# vim /etc/hosts
    ...
    172.16.0.11 www.test.org

2、訪問:

      

3、沒配置緩存時進行壓力測試:

[root@client Desktop]# ab -c 100 -n 100000 http://www.test.org/wordpress
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.test.org (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests


Server Software:        nginx/1.12.2
Server Hostname:        www.test.org
Server Port:            80

Document Path:          /wordpress
Document Length:        185 bytes

Concurrency Level:      100
Time taken for tests:   58.734 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Non-2xx responses:      100000
Total transferred:      41700001 bytes
HTML transferred:       18500000 bytes
Requests per second:    1702.59 [#/sec] (mean)
Time per request:       58.734 [ms] (mean)
Time per request:       0.587 [ms] (mean, across all concurrent requests)
Transfer rate:          693.34 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2   8.4      0     295
Processing:     2   57 124.9     31    2962
Waiting:        2   56 124.8     31    2962
Total:          7   58 125.3     33    2962

Percentage of the requests served within a certain time (ms)
  50%     33
  66%     51
  75%     66
  80%     77
  90%    111
  95%    157
  98%    273
  99%    375
 100%   2962 (longest request)

4、在SLB伺服器進行緩存配置:

#創建緩存存放目錄
[root@SLB Desktop]# mkdir -p /data/nginx/cache
[root@SLB Desktop]# vim /etc/nginx/nginx.conf
#在http欄位進行配置
    http {
        ...
        proxy_cache_path /data/nginx/cache levels=1:1 keys_zone=nginxcache:50m max_size=1g;
        ...
        
        server {
            ...
            proxy_cache nginxcache;
            proxy_cache_key $request_uri;
            proxy_cache_valid 200 301 302 1h;
            proxy_cache_methods GET HEAD;
            proxy_cache_valid any 1m;
            add_header X-cache '$upstream_cache_status from $host';
            proxy_cache_use_stale http_502;
            ...
        }
[root@SLB Desktop]# systemctl restart nginx.service

5、再次進行壓力測試:

[root@client Desktop]# ab -c 100 -n 100000 http://www.test.org/wordpress
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.test.org (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests


Server Software:        nginx/1.12.2
Server Hostname:        www.test.org
Server Port:            80

Document Path:          /wordpress
Document Length:        185 bytes

Concurrency Level:      100
Time taken for tests:   14.391 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Non-2xx responses:      100000
Total transferred:      41700000 bytes
HTML transferred:       18500000 bytes
Requests per second:    6948.98 [#/sec] (mean)
Time per request:       14.391 [ms] (mean)
Time per request:       0.144 [ms] (mean, across all concurrent requests)
Transfer rate:          2829.81 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   1.9      0      48
Processing:     2   14   3.9     13      58
Waiting:        1   13   3.8     13      58
Total:          8   14   3.9     13      67

Percentage of the requests served within a certain time (ms)
  50%     13
  66%     14
  75%     14
  80%     14
  90%     16
  95%     24
  98%     27
  99%     29
 100%     67 (longest request)

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • CLR線程池並不會在CLR初始化時立即建立線程,而是在應用程式要創建線程來運行任務時,線程池才初始化一個線程。線程池初始化時是沒有線程的,線程池裡的線程的初始化與其他線程一樣,但是在完成任務以後,該線程不會自行銷毀,而是以掛起的狀態返回到線程池。直到應用程式再次向線程池發出請求時,線程池裡掛起的線程 ...
  • 概述 Windows Community Toolkit 4.0 於 2018 月 8 月初發佈:Windows Community Toolkit 4.0 Release Note. 4.0 版本相較於 3.0,增加了 DataGrid 等控制項,Sample App 支持了 Fluent Desi ...
  • 之前研究過c#的async和await關鍵字,幕後幹了什麼,但是不知道為什麼找不到相關資料了。現在重新研究一遍,順便記錄下來,方便以後查閱。基礎知識async 關鍵字標註一個方法,該方法返回值是一個Task、或者Task、void、包含GetAwaiter方法的類型。該方法通常包含一個await表達... ...
  • protected override void OnStartup(StartupEventArgs e){base.OnStartup(e);RegisterEvents();} private void RegisterEvents(){//TaskScheduler.UnobservedTas ...
  • 在敘述 "Controller" 一文中,有一處未做解釋,即CreateControllerFactory方法中ControllerActionDescriptor參數是如何產生的。這是因為其與Action的關聯性更大,所以放在本文中繼續描述。 回到MvcRouteHandler或者MvcAttri ...
  •     在Linux系統中,作業是由一個或多個關聯進程組成的。用戶可以運行多個作業並可以在作業間切換。而作業控制則是對作業的行為進行控制,允許用戶對作業的前後臺的進行切換和終止操作等。作業相關的控制命令如下所示: 後臺符號(&):讓作業在後臺運行 快捷鍵Ctrl+Z:讓作業切換到 ...
  • NAT訪問的許可權如下: 外網不可以訪問虛擬機,主機和虛擬機可以互訪,網路和主機也可以互訪; 1、打開虛擬機——編輯——虛擬網路編輯器——、 2、 3、進入虛擬機的linux系統點擊網路 4、 5、點擊ipv4 6、配置虛擬機的ip ip地址前三位要和網關前三位保持一致 子網掩碼都是一樣的 網關要和虛 ...
  • 一、進程與線程 進程是處於執行期的程式,但是並不僅僅局限於一段可執行程式代碼。通常,進程還要包含其他資源,像打開的文件,掛起的信號,內核內部數據,處理器狀態,一個或多個具有記憶體映射的記憶體地址空間及一個或多個執行線程,當然還包括用來存放全局變數的數據段等。在Linux內核中,進程也通常叫做任務。 執行 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...