1、理論部分
1.1、負載均衡的實作
負載程序架構的實作方式有多種,一般分為:
1)TCP層實作的負載均衡
2)應用層實作的負載均衡
1.1.1、TCP層實作的負載均衡
例如:
lvs(排程性能強悍)
1.1.2、應用層實作的負載均衡
nginx,haproxy,apache(mod_proxy),varnish,squid
1.2、nginx的功能:
1.2.1、基本的HTTP伺服器特性
1)提供靜态和指數檔案,自動标引;打開的檔案描述符緩存;
2)加速反向代理緩存;負載平衡和容錯;
3)加速與緩存的支援FastCGI,uwsgi,SCGI,memcached伺服器;負載平衡和容錯;
4)子產品化的體系結構。過濾器包括gzip位元組範圍,分塊反應,XSLT,SSI,圖像變換過濾器。多個SSI包含在單個頁面可以并行處理,如果他們通過代理或FastCGI處理/ uwsgi SCGI伺服器;
SSL和TLS SNI的支援;
5)支援HTTP / 2權重和基于依賴關系的優先級。
1.2.2、其他HTTP伺服器功能
基于名稱的和基于ip的虛拟伺服器;
維生和管線式連接配接支援;
靈活的配置;
重新配置和一個可執行的更新沒有客戶服務的中斷;
通路日志格式,緩沖寫日志,快日志輪轉,syslog日志;
1.3、nginx架構
<a href="http://s3.51cto.com/wyfs02/M01/74/63/wKiom1YboYGQpFPQAADVDuhfnM8380.jpg" target="_blank"></a>
1.3.1、程序的分類
1)一個master程序
2)多個worker程序
1.3.2、程序的功能
master程序用來管理worker程序
1)master程序接收來自外界的信号,向worker程序發送信号。
2)master監控worker程序的運作狀态,當worker程序異常退出,會自動重新開機worker程序
worker程序
1)處理基本的網絡事件
2)程序間對等并互相獨立,同等競争來自用戶端的請求
3)同一請求隻能由一個worker程序處理,其他程序不作處理。
4)worker程序數是可以設定的(一般與機器cpu核心數一緻)
1.3.3、master程序的管理
方法一:
1
<code>kill</code> <code>-HUP pid </code><code>#重新開機master程序</code>
方法二:
2
<code>.</code><code>/nginx</code> <code>-s reload </code><code>#重新開機master程序</code>
<code>.</code><code>/nginx</code> <code>-s stop </code><code>#停止master程序</code>
1.3.4、程序重新開機過程
1)master重新加載配置檔案
2)啟動新的worker程序并接受新的請求
3)通知老的worker程序關閉
4)老的worker程序接收到信号後,不再接受新的處理請求并處理完成已接受請求後關閉
1.3.5、worker程序的請求處理過程
1)master程序建立listen的socket(listenfd)
2)用戶端通路伺服器的80端口觸發請求
3)master程序fork出多個worker程序
4)worker程序搶accept_mutex,搶到互斥鎖的程序注冊listenfd讀事件并調用accept接受該連結
5)當worker程序accept這個連接配接後開始讀取請求,解析請求,處理請求并産生資料傳回用戶端
1.3.6、worker程序的優點
1)worker程序保持獨立性,不需要加鎖,保持鎖帶來的系統資源開銷
2)行的程序獨立不互相影響,一個程序退出,其他程序仍然繼續工作,保持服務的穩定性
1.4、nginx的事件處理
nginx處理請求的方式:
異步非阻塞
apache常用工作方式:
1)異步非阻塞(與自帶某些子產品有沖突)
2)獨占工作線程(幾千并發時,同時開啟幾千線程,線程占用大量記憶體,其上下文切換帶來大量cpu開銷)
請求的過程,接受請求->建立連接配接->接收資料後發送資料的本質是讀寫事件,非阻塞事件處理是每個環節無需等待,防止因為等待下一個環節的事件而耗費系統資源。
非阻塞:
事件沒有準備好,馬上傳回EAGAIN,告訴你,事件還沒有準備好,等待一段時間再次檢查事件,直到事件準備好再執行,沒有準備好期間可以進行其他事件處理(反複檢查也帶來系統開銷)
異步非阻塞:
同時監聽多個事件,調用事件是阻塞的,但可以設定逾時時間。在逾時時間内,如果有事件準備好就傳回
例子:事件準備好了,我們就去讀寫,當讀寫傳回eagain時,我們再次将他加入epoll裡面等着。(事件準備好就處理,沒有準備好就加入到epoll裡面)
2、實驗部分
2.1、LNMP的實作
2.1.1、主機資訊
wwwSer:
ipaddress=10.168.0.180
hostname=lnmp
client:
ipaddress=10.168.0.181
hostname=client
2.1.2、RPM包的安裝
In wwwSer:
安裝基礎nginx相關包:
<code>yum -y </code><code>install</code> <code>http:</code><code>//nginx</code><code>.org</code><code>/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0</code><code>.el6.ngx.noarch.rpm</code>
<code>yum -y </code><code>install</code> <code>nginx</code>
安裝MySQL相關包
<code>yum -y </code><code>install</code> <code>mysql-server mysql mysql-devel</code>
安裝PHP相關包
<code>yum -y </code><code>install</code> <code>php-fpm php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-mssql php-snmp php-soap php-tidy</code>
2.1.3、PHP部配置設定置
vim編輯/etc/php.ini并将以下行的注解去掉(啟用)
<code>cgi.fix_pathinfo=1</code>
2.1.4、Nginx部配置設定置
1)增加Nginx預設目錄的fastcgi解析(可選)
vim編輯/etc/nginx/nginx.conf
在“include /etc/nginx/conf.d/*.conf;”語句前加入如下代碼:
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<code> </code><code>server {</code>
<code> </code><code>listen 80;</code>
<code> </code><code>server_name _;</code>
<code> </code><code>#charset koi8-r;</code>
<code> </code><code>#access_log logs/host.access.log main;</code>
<code> </code><code>location / {</code>
<code> </code><code>root </code><code>/usr/share/nginx/html</code><code>;</code>
<code> </code><code>index index.php index.html index.htm;</code>
<code> </code><code>}</code>
<code> </code><code>error_page 404 </code><code>/404</code><code>.html;</code>
<code> </code><code>location = </code><code>/404</code><code>.html {</code>
<code> </code><code># redirect server error pages to the static page /50x.html</code>
<code> </code><code>#</code>
<code> </code><code>error_page 500 502 503 504 </code><code>/50x</code><code>.html;</code>
<code> </code><code>location = </code><code>/50x</code><code>.html {</code>
<code> </code><code># proxy the PHP scripts to Apache listening on 127.0.0.1:80</code>
<code> </code><code>#location ~ \.php$ {</code>
<code> </code><code># proxy_pass http://127.0.0.1;</code>
<code> </code><code>#}</code>
<code> </code>
<code> </code><code># pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000</code>
<code> </code><code>location ~ \.php$ {</code>
<code> </code><code>root </code><code>/usr/share/nginx/html</code><code>;</code>
<code> </code><code>fastcgi_pass 127.0.0.1:9000;</code>
<code> </code><code>fastcgi_index index.php;</code>
<code> </code><code>fastcgi_param SCRIPT_FILENAME </code><code>/usr/share/nginx/html</code><code>$fastcgi_script_name;</code>
<code> </code><code>include fastcgi_params;</code>
<code> </code><code># deny access to .htaccess files, if Apache's document root</code>
<code> </code><code># concurs with nginx's one</code>
<code> </code><code>location ~ /\.ht {</code>
<code> </code><code>deny all;</code>
<code> </code><code>}</code>
2)增加虛拟機(必須)
建立虛拟機跟目錄:
<code>mkdir</code> <code>/home/cmdschool</code>
<code>chown</code> <code>-R apache:apache </code><code>/home/cmdschool</code>
vim編輯/etc/nginx/conf.d/www.cmdschool.org.conf
<code>server {</code>
<code> </code><code>listen 80;</code>
<code> </code><code>server_name www.cmdschool.org;</code>
<code> </code><code>location / {</code>
<code> </code><code>root </code><code>/home/cmdschool/</code><code>;</code>
<code> </code><code>index index.php index.html index.htm;</code>
<code> </code><code>access_log </code><code>/var/log/www</code><code>.cmdschool.org.access.log main;</code>
<code> </code><code>location ~ [^/]\.php(/|$) {</code>
<code> </code><code>fastcgi_split_path_info ^(.+?\.php)(/.*)$;</code>
<code> </code><code>if</code> <code>(!-f $document_root$fastcgi_script_name) {</code>
<code> </code><code>return</code> <code>404;</code>
<code> </code><code>root </code><code>/home/cmdschool/</code><code>;</code>
<code> </code><code>fastcgi_pass 127.0.0.1:9000;</code>
<code> </code><code>fastcgi_index index.php;</code>
<code> </code><code>fastcgi_param SCRIPT_FILENAME </code><code>/usr/share/nginx/html</code><code>$fastcgi_script_name;</code>
<code> </code><code>include fastcgi_params;</code>
<code>}</code>
2.2.5、啟動服務
啟動php服務并設定開機自啟動
<code>/etc/init</code><code>.d</code><code>/php-fpm</code> <code>start</code>
<code>chkconfig php-fpm on</code>
啟動nginx服務并設定開機自啟動
<code>/etc/init</code><code>.d</code><code>/nginx</code> <code>start</code>
<code>chkconfig nginx on</code>
啟動mysql服務并設定自動啟動
<code>/etc/init</code><code>.d</code><code>/mysqld</code> <code>start</code>
<code>chkconfig mysqld on</code>
注:關于mysql的安全選項,請運作以下腳本設定,這裡不詳述
<code>mysql_secure_installation</code>
2.2.6、防火牆設定
vim編輯/etc/sysconfig/iptables
<code>-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT</code>
重新開機iptables
<code>/etc/init</code><code>.d</code><code>/iptables</code> <code>restart</code>
2.2.7、測試
1)服務php解析測試:
vim編輯/usr/share/nginx/html/index.php
加入如下内容:
<code><?php</code>
<code>phpinfo();</code>
<code>?></code>
In client:
<code>curl http:</code><code>//10</code><code>.168.0.180</code>
2)虛拟伺服器測試:
vim編輯/home/cmdschool/index.php
vim編輯/etc/hosts
加入如下内容(DNS解析):
<code>10.168.0.180 www.cmdschool.org</code>
測試:
<code>curl http:</code><code>//www</code><code>.cmdschool.org</code>
----------------------------------------------------------
理論部分參閱資料:
1)官方:
<a href="http://www.nginx.org/" target="_blank">http://www.nginx.org</a>
2)Tengine:
<a href="http://tengine.taobao.org/" target="_blank">http://tengine.taobao.org</a>
本文轉自 tanzhenchao 51CTO部落格,原文連結:http://blog.51cto.com/cmdschool/1702135,如需轉載請自行聯系原作者