Nginx教程
Nginx教程
1. 課程目標
1.1. 了解反向代理和負載均衡的概念
1.2. 掌握Nginx的安裝和使用
1.3. 利用Nginx實現負載均衡
2. Nginx相關概念
2.1. 反向代理
反向代理(Reverse Proxy)方式是指以代理伺服器來接受internet上的連接請求,然後將請求轉發給內部網路上的伺服器,並將從伺服器上得到的結果返回給internet上請求連接的客戶端,此時代理伺服器對外就表現為一個伺服器。
2.2. 負載均衡
負載均衡,英文名稱為Load Balance,是指建立在現有網路結構之上,並提供了一種廉價有效透明的方法擴展網路設備和伺服器的帶寬、增加吞吐量、加強網路數據處理能力、提高網路的靈活性和可用性。其原理就是數據流量分攤到多個伺服器上執行,減輕每台伺服器的壓力,多台伺服器共同完成工作任務,從而提高了數據的吞吐量。
3. Nginx的安裝
3.1. 下載nginx
官網:http://nginx.org/
3.2. 上傳並解壓nginx
tar -zxvf nginx-1.8.1.tar.gz -C
/usr/local/src3.3. 編譯nginx
#進入到nginx源碼目錄
cd /usr/local/src/nginx-1.8.1
#檢查安裝環境,並指定將來要安裝的路徑
./configure --prefix=/usr/local/nginx
#缺包報錯 ./configure: error: C compiler cc is not found
#使用YUM安裝缺少的包
yum
-y install gcc pcre-devel openssl openssl-devel#編譯安裝
make
&& make install
安裝完後測試是否正常:
/usr/loca/nginx/sbin/nginx
查看埠是否有ngnix進程監聽
netstat
-ntlp | grep 804. 配置nginx
4.1. 配置反向代理
1.修改nginx配置文件
server {
listen 80;
server_name nginx-01.itcast.cn; #nginx所在伺服器的主機名
#反向代理的配置
location / { #攔截所有請求
root html;
proxy_pass http://192.168.0.21:8080; #這裡是代理走向的目標伺服器:tomcat
}
}
2.啟動tomcat-01上的tomcat
3.啟動nginx-01上的nginx
./nginx
重啟:
kill -HUP `cat /usr/local/nginx/logs/nginx.pid
`參考網址:http://www.cnblogs.com/jianxie/p/3990377.html
4.2. 動靜分離
#動態資源 index.jsp
location ~ .*.(jsp|do|action)$ {
proxy_pass http://tomcat-01.itcast.cn:8080;
}
#靜態資源
location ~
.*.(html|js|css|gif|jpg|jpeg|png)$ {
expires 3d;
}
4.3. 負載均衡
在http這個節下面配置一個叫upstream的,後面的名字可以隨意取,但是要和location下的proxy_pass http://後的保持一致。
http {
是在http裡面的, 已有http, 不是在server里,在server外面
upstream tomcats
{server shizhan02:8080 weight=1;#weight表示多少個
server shizhan03:8080 weight=1;
server shizhan04:8080 weight=1;
}
#卸載server里
location ~ .*.(jsp|do|action) {
proxy_pass http://tomcats; #tomcats是後面的tomcat伺服器組的邏輯組號
}
}
5. 利用keepalived實現高可靠(HA)
5.1. 高可靠概念
HA(High Available), 高可用性集群,是保證業務連續性的有效解決方案,一般有兩個或兩個以上的節點,且分為活動節點及備用節點。
5.2. 高可靠軟體keepalived
keepalive是一款可以實現高可靠的軟體,通常部署在2台伺服器上,分為一主一備。Keepalived可以對本機上的進程進行檢測,一旦Master檢測出某個進程出現問題,將自己切換成Backup狀態,然後通知另外一個節點切換成Master狀態。
5.3. keepalived安裝
下載keepalived官網:http://keepalived.org
將keepalived解壓到/usr/local/src目錄下
tar -zxvf keepalived-1.2.19.tar.gz -C /usr/local/src
進入到/usr/local/src/keepalived-1.2.19目錄
cd /usr/local/src/keepalived-1.2.19
開始configure
./configure
--prefix=/usr/local/keepalived#編譯並安裝
make && make install
5.4. 將keepalived添加到系統服務中
拷貝執行文件
cp /usr/local/keepalived/sbin/keepalived
/usr/sbin/將init.d文件拷貝到etc下,加入開機啟動項
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived
/etc/init.d/keepalived將keepalived文件拷貝到etc下
cp /usr/local/keepalived/etc/sysconfig/keepalived
/etc/sysconfig/創建keepalived文件夾
mkdir -p
/etc/keepalived
將keepalived配置文件拷貝到etc下
cp /usr/local/keepalived/etc/keepalived/keepalived.conf
/etc/keepalived/keepalived.conf添加可執行許可權
chmod
+x/etc/init.d/keepalived##以上所有命令一次性執行:
cp
/usr/local/keepalived/sbin/keepalived /usr/sbin/cp
/usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalivedcp
/usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/mkdir
-p /etc/keepalivedcp
/usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.confchmod
+x /etc/init.d/keepalivedchkconfig
--add keepalivedchkconfig
keepalived on添加keepalived到開機啟動
chkconfig --add keepalived
chkconfig keepalived on
5.5. 配置keepalived虛擬IP
修改配置文件: /etc/keepalived/keepalived.conf
#MASTER節點
global_defs {
}
vrrp_instance
VI_1 {state MASTER #指定A節點為主節點 備用節點上設置為BACKUP即可
interface eth0 #綁定虛擬IP的網路介面
virtual_router_id 51 #VRRP組名,兩個節點的設置必須一樣,以指明各個節點屬於同一VRRP組
priority 100 #主節點的優先順序(1-254之間),備用節點必須比主節點優先順序低
advert_int 1 #組播信息發送間隔,兩個節點設置必須一樣
authentication { #設置驗證信息,兩個節點必須一致
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #指定虛擬IP, 兩個節點設置必須一樣
192.168.33.60/24 #如果兩個nginx的ip分別是192.168.33.61,,...62,則此處的虛擬ip跟它倆同一個網段即可
}
}
#BACKUP節點
global_defs {
}
vrrp_instance
VI_1 {state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.33.60/24
}
}
#分別啟動兩台機器上的keepalived
service keepalived start
測試:
殺掉master上的keepalived進程,你會發現,在slave機器上的eth0網卡多了一個ip地址
查看ip地址的命令: ip addr
5.6. 配置keepalived心跳檢查
原理:
Keepalived並不跟nginx耦合,它倆完全不是一家人
但是keepalived提供一個機制:讓用戶自定義一個shell腳本去檢測用戶自己的程序,返回狀態給keepalived就可以了
#MASTER節點
global_defs {
}
vrrp_script chk_health {
script "[[
`ps -ef | grep nginx | grep -v grep | wc -l` -ge 2 ]] && exit 0 || exit 1"interval 1 #每隔1秒執行上述的腳本,去檢查用戶的程序ngnix
weight -2
}
vrrp_instance
VI_1 {state MASTER
interface eth0
virtual_router_id 1
priority 100
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_health
}
virtual_ipaddress {
10.0.0.10/24
}
notify_master
"/usr/local/keepalived/sbin/notify.sh master"notify_backup
"/usr/local/keepalived/sbin/notify.sh backup"notify_fault
"/usr/local/keepalived/sbin/notify.sh fault"}
#添加切換通知腳本
vi /usr/local/keepalived/sbin/notify.sh
#!/bin/bash
case
"$1" inmaster)
/usr/local/nginx/sbin/nginx
exit 0
;;
backup)
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx
exit 0
;;
fault)
/usr/local/nginx/sbin/nginx -s stop
exit 0
;;
*)
echo Usage: notify.sh
{master|backup|fault}exit 1
;;
esac
#添加執行許可權
chmod +x
/usr/local/keepalived/sbin/notify.shglobal_defs {
}
vrrp_script
chk_health {script "[[ `ps -ef | grep nginx |
grep -v grep | wc -l` -ge 2 ]] && exit 0 || exit 1"interval 1
weight -2
}
vrrp_instance
VI_1 {state BACKUP
interface eth0
virtual_router_id 1
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_health
}
virtual_ipaddress {
10.0.0.10/24
}
notify_master
"/usr/local/keepalived/sbin/notify.sh master"notify_backup "/usr/local/keepalived/sbin/notify.sh
backup"notify_fault
"/usr/local/keepalived/sbin/notify.sh fault"}
#在第二台機器上添加notify.sh腳本
#分別在兩台機器上啟動keepalived
service keepalived start
chkconfig keepalived on
?$>??推薦閱讀:
※基於ReactRouter實現的SPA應用的Nginx配置
※為什麼 node.js 的官網不用 node.js 而用 nginx 搭建?
※Nginx源碼中值得學習的編程風格有哪些?
※nginx static module vs redis,誰更快?
※actor模型跟nginx中worker_process的相同和差異有哪些?
TAG:Nginx |