目錄
- 1.簡介
- 2.相關概念
- 3.安裝運作
- 4.常見指令
- 5.配置檔案
- 6.配置執行個體
- 7.高可用叢集-主從模式
- 8.Nginx 原了解析
1.簡介
Nginx (“engine x”) 是一個高性能的 HTTP 和反向代理伺服器,特點是占有記憶體少,并發能力強,事實上 nginx 的并發能力确實在同類型的網頁伺服器中表現較好。
Nginx 可以作為靜态頁面的 web 伺服器,同時還支援 CGI 協定的動态語言,比如 perl、php等。但是不支援 java。Java 程式隻能通過與 tomcat 配合完成。Nginx 專為性能優化而開發,性能是其最重要的考量,實作上非常注重效率 ,能經受高負載的考驗,有報告表明能支援高達 50,000 個并發連接配接數。
Nginx 不僅可以做反向代理,實作負載均衡。還能用作正向代理來進行上網等功能。
2.相關概念
- 正向代理:如果把區域網路外的 Internet 想象成一個巨大的資源庫,則區域網路中的用戶端要通路 Internet,則需要通過代理伺服器來通路,這種代理服務就稱為正向代理。(即:需要在用戶端配置代理伺服器進行指定網站通路)
- 反向代理:其實用戶端對代理是無感覺的,因為用戶端不需要任何配置就可以通路,我們隻需要将請求發送到反向代理伺服器,由反向代理伺服器去選擇目标伺服器擷取資料後,在傳回給用戶端,此時反向代理伺服器和目标伺服器對外就是一個伺服器,暴露的是代理伺服器位址,隐藏了真實伺服器 IP 位址。(即:暴露的是代理伺服器位址,隐藏了真實伺服器IP位址)
- 負載均衡:并發請求量大時,增加伺服器的數量,然後将請求分發到各個伺服器上,将原先請求集中到單個伺服器上的情況改為将請求分發到多個伺服器上,将負載分發到不同的伺服器。(即:增加伺服器的數量,然後将請求分發到各個伺服器上,将原先請求集中到單個伺服器上的情況改為将請求分發到多個伺服器上,将負載分發到不同的伺服器,也就是我們所說的負載均衡)
- 動靜分離:為了加快網站的解析速度,可以把動态頁面和靜态頁面由不同的伺服器來解析,加快解析速度。降低原來單個伺服器的壓力。
3.安裝運作
- 安裝pcre(依賴)
#将安裝包放入 usr/src 目錄下
[[email protected] src]# ls
debug kernels nginx-1.12.2.tar.gz pcre-8.37.tar.gz
#解壓該壓縮檔案
[[email protected] src]# tar -xvf pcre-8.37.tar.gz
[[email protected] src]# cd pcre-8.37/
[[email protected] pcre-8.37]# ./configure
#編譯
[[email protected] pcre-8.37]# make && make install
#驗證是否安裝成功
[[email protected] pcre-8.37]# pcre-config --version
8.37
- 安裝其他依賴
- 安裝nginx
#将安裝包放入 /src目錄下
[[email protected] src]# ls
daemonize nginx-1.12.2.tar.gz prometheus-2.17.1.linux-amd64 pushgateway-1.2.0.linux-amd64.tar.gz
#解壓該壓縮檔案
[[email protected] src]# tar -vxf nginx-1.12.2.tar.gz
[[email protected] src]# cd nginx-1.12.2/
[[email protected] nginx-1.12.2]# ./configure
#編譯
[[email protected] src]# make && make install
#驗證是否安裝成功
[[email protected] sbin]# ls
nginx
[[email protected] sbin]# pwd
/usr/local/nginx/sbin
#啟動nginx
[[email protected] sbin]# ./nginx
[[email protected] sbin]# ps -ef | grep nginx
root 3196 1 0 22:28 ? 00:00:00 nginx: master process ./nginx
nobody 3198 3196 0 22:28 ? 00:00:00 nginx: worker process
root 3219 2996 0 22:29 pts/0 00:00:00 grep --color=auto nginx
#檢視配置檔案
[[email protected] ~]# cd /usr/local/nginx/conf/
[[email protected] conf]# ls
fastcgi.conf koi-utf nginx.conf uwsgi_params
fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.default
fastcgi_params mime.types scgi_params win-utf
fastcgi_params.default mime.types.default scgi_params.default
[[email protected] conf]# vim nginx.conf
#檢視已開發的端口
[[email protected] conf]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
#添加開放端口
[[email protected] conf]# firewall-cmd --add-port=80/tcp --permanent
success
#重新開機防火牆
[[email protected] conf]# firewall-cmd --reload
success
#檢視防火牆開放的端口
[[email protected] conf]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ssh
ports: 80/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
驗證是否安裝成功
預設啟動端口為80
4.常見指令
#使用nginx的時候必須進入該目錄
[[email protected] sbin]# cd /usr/local/nginx/sbin/
#檢視nginx版本
[[email protected] sbin]# ./nginx -v
nginx version: nginx/1.12.2
#啟動nginx
[[email protected] sbin]# ./nginx
#停止nginx
[[email protected] sbin]# ./nginx -s stop
#重新加載nginx
[[email protected] sbin]# ./nginx -s reload
5.配置檔案
#配置檔案中的内容,包含三部分
#1、全局塊:配置伺服器整體運作的配置指令(比如 worker_processes 1;處理并發數的配置)
#2、events塊:影響 Nginx 伺服器與使用者的網絡連接配接(比如 worker_connections 1024; 支援的最大連接配接數為 1024)
#3、http塊:包含 http 全局塊和 server 塊兩部分
#配置檔案位置
[[email protected] conf]# vim /usr/local/nginx/conf/nginx.conf
#從配置檔案開始到 events 塊之間的内容,主要會設定一些影響 nginx 伺服器整體運作的配置指令,主要包括配置運作 Nginx 伺服器的使用者(組)、允許生成的 worker process 數,程序 PID 存放路徑、日志存放路徑和類型以及配置檔案的引入等。
#user nobody;
#伺服器并發處理服務的關鍵配置,worker_processes 值越大,可以支援的并發處理量也越多,但是會受到硬體、軟體等裝置的制約
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#events 塊涉及的指令主要影響 Nginx 伺服器與使用者的網絡連接配接,常用的設定包括是否開啟對多 work process 下的網絡連接配接進行序列化,是否允許同時接收多個網絡連接配接,選取哪種事件驅動模型來處理連接配接請求,每個 word process 可以同時支援的最大連接配接數等
events {
worker_connections 1024;
}
#上述例子就表示每個 work process 支援的最大連接配接數為 1024
#這部分的配置對 Nginx 的性能影響較大,在實際中應該靈活配置
#這算是 Nginx 伺服器配置中最頻繁的部分,代理、緩存和日志定義等絕大多數功能和第三方子產品的配置都在這裡。需要注意的是:http 塊也可以包括 http 全局塊、server 塊。
http {
#http 全局塊配置的指令包括檔案引入、MIME-TYPE 定義、日志自定義、連接配接逾時時間、單連結請求數上限等。
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#這塊和虛拟主機有密切關系,虛拟主機從使用者角度看,和一台獨立的硬體主機是完全一樣的,該技術的産生是為了節省網際網路伺服器硬體成本。每個 http 塊可以包括多個 server 塊,而每個 server 塊就相當于一個虛拟主機。而每個 server 塊也分為全局 server 塊,以及可以同時包含多個 locaton 塊。
server {
#全局 server 塊,常見的配置是本虛拟機主機的監聽配置和本虛拟主機的名稱或 IP 配置。
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#一個 server 塊可以配置多個 location 塊。這塊的主要作用是基于 Nginx 伺服器接收到的請求字元串(例如 server_name/uri-string),對虛拟主機名稱(也可以是 IP 别名)之外的字元串(例如 前面的 /uri-string)進行比對,對特定的請求進行處理。位址定向、資料緩存和應答控制等功能,還有許多第三方子產品的配置也在這裡進行。
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
6.配置執行個體
-
反向代理執行個體一
實作效果:使用 nginx 反向代理,通路 www.123.com 直接跳轉到 127.0.0.1:8080
準備工作:将tomcat放入linux的/usr/src目錄下
#解壓tomcat
[[email protected] src]# tar -xvf apache-tomcat-7.0.70.tar.gz
#安裝tomcat前應該先裝jdk,一般系統中會自帶
[[email protected] src]# java -version
#啟動tomcat
[[email protected] bin]# cd /usr/src/apache-tomcat-7.0.70/bin
[[email protected] bin]# ./startup.sh
#檢視啟動日志
[[email protected] logs]# tail -f /usr/src/apache-tomcat-7.0.70/logs/catalina.out
四月 11, 2020 11:59:52 下午 org.apache.catalina.startup.HostConfig deployDirectory
資訊: Deploying web application directory /usr/src/apache-tomcat-7.0.70/webapps/manager
四月 11, 2020 11:59:52 下午 org.apache.catalina.startup.HostConfig deployDirectory
資訊: Deployment of web application directory /usr/src/apache-tomcat-7.0.70/webapps/manager has finished in 154 ms
四月 11, 2020 11:59:52 下午 org.apache.coyote.AbstractProtocol start
資訊: Starting ProtocolHandler ["http-bio-8080"]
四月 11, 2020 11:59:52 下午 org.apache.coyote.AbstractProtocol start
資訊: Starting ProtocolHandler ["ajp-bio-8009"]
四月 11, 2020 11:59:52 下午 org.apache.catalina.startup.Catalina start
資訊: Server startup in 2818 ms
#對外開放8080端口
[[email protected] logs]# firewall-cmd --add-port=8080/tcp --permanent
success
[[email protected] logs]# firewall-cmd --reload
success
- 通路過程
- windows伺服器浏覽器輸入www.123.com
- 浏覽器先找host檔案查找域名映射的ip位址,若無則在網絡DNS域名解析器映射的ip
- 在host檔案找到映射位址為192.168.17.129:80進行通路到nginx伺服器
- nginx伺服器進行反向代理到127.0.0.1:8080通路到tomcat
- 具體配置
- 在 windows 系統的 host 檔案進行域名和 ip 對應關系的配置
- 在 nginx 進行請求轉發的配置(反向代理配置)
-
測試
修改配置檔案後,重新開機nignx伺服器
[[email protected] sbin]# ./nginx
-
反向代理執行個體二
實作效果:nginx 監聽端口為 9001,通路 http://192.168.17.129:9001/edu/ 直接跳轉到 127.0.0.1:8080,通路 http:// 192.168.17.129:9001/vod/ 直接跳轉到 127.0.0.1:8081
準備工作:準備兩個 tomcat 伺服器,一個 8080 端口,一個 8081 端口。建立檔案夾和測試頁面
#進入安裝包存放目錄
[[email protected] ~]# cd /usr/local/src
#建立tomcat8080和8081檔案夾
[[email protected] src]# mkdir tomcat8080
[[email protected] src]# mkdir tomcat8081
[[email protected] src]# cd tomcat8080
[[email protected] tomcat8080]# rz -E
rz waiting to receive.
[[email protected] tomcat8080]# cd /usr/local/src/tomcat8081
[[email protected] tomcat8081]# rz -E
rz waiting to receive.
#顯示正在運作的tomcat
[[email protected] ~]# ps -ef | grep tomcat
root 5049 1 0 18:47 ? 00:00:32 /usr/bin/java -Djava.util.logging.config.file=/usr/src/apache-tomcat-7.0.70/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.endorsed.dirs=/usr/src/apache-tomcat-7.0.70/endorsed -classpath /usr/src/apache-tomcat-7.0.70/bin/bootstrap.jar:/usr/src/apache-tomcat-7.0.70/bin/tomcat-juli.jar -Dcatalina.base=/usr/src/apache-tomcat-7.0.70 -Dcatalina.home=/usr/src/apache-tomcat-7.0.70 -Djava.io.tmpdir=/usr/src/apache-tomcat-7.0.70/temp org.apache.catalina.startup.Bootstrap start
root 12307 12196 0 22:54 pts/2 00:00:00 grep --color=auto tomcat
#通過ID殺掉這個程序
[[email protected] ~]# kill -9 5049
#可看到程序已經處理
[[email protected] ~]# ps -ef | grep tomcat
root 12342 12196 0 22:57 pts/2 00:00:00 grep --color=auto tomcat
#解壓端口為8080的tomcat
[[email protected] ~]# cd /usr/local/src/tomcat8080
[[email protected] tomcat8080]# tar -xvf apache-tomcat-9.0.33.tar.gz
#啟動8080tomcat
[[email protected] tomcat8080]# cd apache-tomcat-9.0.33/bin/
[[email protected] bin]# ./startup.sh
Using CATALINA_BASE: /usr/local/src/tomcat8080/apache-tomcat-9.0.33
Using CATALINA_HOME: /usr/local/src/tomcat8080/apache-tomcat-9.0.33
Using CATALINA_TMPDIR: /usr/local/src/tomcat8080/apache-tomcat-9.0.33/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/src/tomcat8080/apache-tomcat-9.0.33/bin/bootstrap.jar:/usr/local/src/tomcat8080/apache-tomcat-9.0.33/bin/tomcat-juli.jar
Tomcat started.
#解壓端口為8081的tomcat
[[email protected] ~]# cd /usr/local/src/tomcat8081
[[email protected] tomcat8081]# tar -xvf apache-tomcat-9.0.33.tar.gz
#修改端口為8081
[[email protected] conf]# vim /usr/local/src/tomcat8081/apache-tomcat-9.0.33/conf/server.xml
...
<Server port="8015" shutdown="SHUTDOWN">
...
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
...
#啟動
[[email protected] bin]# cd /usr/local/src/tomcat8081/apache-tomcat-9.0.33/bin
[[email protected] bin]# ./startup.sh
#防火牆開放8081端口
[[email protected] bin]# firewall-cmd --add-port=8080/tcp --permanent
#重新開機防火牆
[[email protected] bin]# firewall-cmd --reload
[[email protected] tomcat8080]# cd apache-tomcat-9.0.33/
[[email protected] apache-tomcat-9.0.33]# cd webapps/
[[email protected] webapps]# mkdir edu
[[email protected] webapps]# cd edu
[[email protected] edu]# vim a.html
<h1>8080!</h1>
[[email protected] tomcat8081]# cd apache-tomcat-9.0.33/
[[email protected] apache-tomcat-9.0.33]# cd webapps/
[[email protected] webapps]# mkdir vod
[[email protected] webapps]# cd vod
[[email protected] vod]# vim a.html
<h1>8081!</h1>
- 具體配置:找到 nginx 配置檔案,進行反向代理配置
[[email protected] sbin]# vim /usr/local/nginx/conf/nginx.conf
...
# another virtual host using mix of IP-, name-, and port-based configuration
#
server {
listen 9001;
server_name 192.168.172.156;
#用正規表達式形式,隻要包含/edu/就跳轉
location ~ /edu/ {
proxy_pass http://127.0.0.1:8080;
}
location ~ /vod/ {
proxy_pass http://127.0.0.1:8081;
}
}
...
#此時需要保證9001、8080、8081端口都開放
[[email protected] conf]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ssh
ports: 80/tcp 8080/tcp 8081/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
#開放9001端口
[[email protected] conf]# firewall-cmd --add-port=9001/tcp --permanent
success
[[email protected] conf]# firewall-cmd --reload
success
#重新開機nginx
[[email protected] conf]# cd /usr/local/nginx/sbin/
[[email protected] sbin]# ./nginx -s stop
[[email protected] sbin]# ./nginx
-
負載均衡
實作效果:浏覽器位址欄輸入位址 http://192.168.172.156/edu/a.html,負載均衡效果,平均 8080和 8081 端口中
[[email protected] tomcat8081]#cd webapps
[[email protected] webapps]# mkdir edu
#建立不同位址伺服器的相同檔案路徑
[[email protected] edu]# vim a.html
<h1>8081!</h1>
#對nginx配置檔案進行修改
[[email protected] edu]# vim /usr/local/nginx/conf/nginx.conf
#在 nginx 的配置檔案中進行負載均衡的配置
#重新開機nginx
[[email protected] nginx]# cd /usr/local/nginx/sbin/
[[email protected] sbin]# ./nginx -s stop
[[email protected] sbin]# ./nginx
實驗結果:
随着網際網路資訊的爆炸性增長,負載均衡(load balance)已經不再是一個很陌生的話題,顧名思義,負載均衡即是将負載分攤到不同的服務單元,既保證服務的可用性,又保證響應足夠快,給使用者很好的體驗。快速增長的通路量和資料流量催生了各式各樣的負載均衡産品,很多專業的負載均衡硬體提供了很好的功能,但卻價格不菲,這使得負載均衡軟體大受歡迎,nginx 就是其中的一個,在 linux 下有 Nginx、LVS、Haproxy 等等服務可以提供負載均衡服務,而且 Nginx 提供了幾種配置設定方式(政策):
-
1、輪詢(預設)
每個請求按時間順序逐一配置設定到不同的後端伺服器,如果後端伺服器 down 掉,能自動剔除。
-
2、weight
weight 代表權,重預設為 1,權重越高被配置設定的用戶端越多
指定輪詢幾率,weight 和通路比率成正比,用于後端伺服器性能不均的情況。 例如:
upstream server_pool{
server 192.168.5.21 weight=10;
server 192.168.5.22 weight=10;
}
-
3、ip_hash
每個請求按通路 ip 的 hash 結果配置設定,這樣每個訪客固定通路一個後端伺服器,可以解決 session 的問題。 例如:
upstream server_pool{
ip_hash;
server 192.168.5.21:80;
server 192.168.5.22:80;
}
-
4、fair(第三方)
按後端伺服器的響應時間來配置設定請求,響應時間短的優先配置設定。
upstream server_pool{
server 192.168.5.21:80; server 192.168.5.22:80;fair;
}
-
動靜分離
簡介解釋:Nginx 動靜分離簡單來說就是把動态跟靜态請求分開,不能了解成隻是單純的把動态頁面和靜态頁面實體分離。嚴格意義上說應該是動态請求跟靜态請求分開,可以了解成使用 Nginx 處理靜态頁面,Tomcat 處理動态頁面。動靜分離從目前實作角度來講大緻分為兩種,一種是純粹把靜态檔案獨立成單獨的域名,放在獨立的伺服器上,也是目前主流推崇的方案;另外一種方法就是動态跟靜态檔案混合在一起釋出,通過 nginx 來分開。通過 location 指定不同的字尾名實作不同的請求轉發。通過 expires 參數設定,可以使浏覽器緩存過期時間,減少與伺服器之前的請求和流量。具體 Expires 定義:是給一個資源設定一個過期時間,也就是說無需去服務端驗證,直接通過浏覽器自身确認是否過期即可,是以不會産生額外的流量。此種方法非常适合不經常變動的資源。(如果經常更新的檔案,不建議使用 Expires 來緩存),我這裡設定 3d,表示在這 3 天之内通路這個 URL,發送一個請求,比對伺服器該檔案最後更新時間沒有變化,則不會從伺服器抓取,傳回狀态碼304,如果有修改,則直接從伺服器重新下載下傳,傳回狀态碼 200。
實驗準備:
[[email protected] /]# mkdir data
[[email protected] /]# cd data
[[email protected] data]# mkdir www
[[email protected] data]# mkdir image
[[email protected] data]# ls
image www
[[email protected] data]# cd www
[[email protected] image]# vim a.html
<h1>test html !!</h1>
[[email protected] image]# cd ..
[[email protected] data]# cd image/
[[email protected] image]# rz -E
rz waiting to receive.
[[email protected] image]# ls
n.png
[[email protected] image]# cd /usr/local/nginx/sbin/
[[email protected] sbin]# ./nginx
[[email protected] sbin]# vim ../conf/nginx.conf
#配置nginx
[[email protected] ~]# cd /usr/local/nginx/conf/
[[email protected] conf]# vim nginx.conf
...
server {
listen 80;
server_name 192.168.172.158;
#access_log logs/host.access.log main;
location /www/ {
root /data/;
index index.html index.htm;
}
location /image/ {
root /data/;
}
...
#重新開機nginx
[[email protected] conf]# cd /usr/local/nginx/sbin/
[[email protected] sbin]# ./nginx -s stop
[[email protected] sbin]# ./nginx
測試
7.高可用叢集-主從模式
痛點:nginx可能會存在當機的情況,這時請求無法實作效果
解決:配置主從伺服器,其中需要兩台nginx伺服器、keepalived和虛拟ip
兩天機器都需要安裝nginx
兩台伺服器安裝keepalived
#安裝
[[email protected] sbin]# yum install keepalived -y
#檢查安裝成功與否
[[email protected] sbin]# rpm -q -a keepalived
keepalived-1.3.5-16.el7.x86_64
進行主從高可用配置
#對主伺服器進行相關配置
[[email protected] keepalived]# vim /etc/keepalived/keepalived.conf
global_defs { # 全局配置
notification_email {
[email protected].loc
[email protected].loc
[email protected].loc
}
notification_email_from Alexandre.[email protected].loc
smtp_server 192.168.17.129
smtp_connect_timeout 30
router_id LVS_DEVEL # 通過這個名字能通路到主機/伺服器,對應/etc/hosts檔案
}
vrrp_script chk_http_port { # 腳本配置
script "/usr/local/src/nginx_check.sh"
interval 2 #(檢測腳本執行的間隔),機關秒
weight 2 # 權重,當腳本成立目前主機權重+var
}
vrrp_instance VI_1 { # 虛拟IP配置
state MASTER # 主伺服器上将 MASTER
interface ens33 # 網卡
virtual_router_id 51 # 主、備機的 virtual_router_id 必須相同
priority 100 # 主、備機取不同的優先級,主機值較大,備份機值較小
advert_int 1 # 檢測心跳間隔時間
authentication { # 權限校驗方式
auth_type PASS # 方式是密碼
auth_pass 1111
}
virtual_ipaddress {
192.168.17.50 # VRRP H 虛拟位址,可綁定多個虛拟ip
}
}
#對從伺服器進行配置
global_defs {
notification_email {
[email protected].loc
[email protected].loc
[email protected].loc
}
notification_email_from Alexandre.[email protected].loc
smtp_server 192.168.17.129
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/usr/local/src/nginx_check.sh"
interval 2 #(檢測腳本執行的間隔)
weight 2
}
vrrp_instance VI_1 {
state BACKUP # 主伺服器上将 MASTER
interface ens33 //網卡
virtual_router_id 51 # 主、備機的 virtual_router_id 必須相同
priority 90 # 主、備機取不同的優先級,主機值較大,備份機值較小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.17.50 // VRRP H 虛拟位址
}
}
#自動檢測腳本,分别放在主從伺服器的/usr/local/src/目錄下,其名稱為nginx_check.sh
#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
兩台伺服器上nginx和keepalived啟動,即可
[[email protected] sbin]# ./nginx
[[email protected] sbin]# systemctl start keepalived.service
[[email protected] sbin]# ps -ef | grep keepalived
最後測試,浏覽器輸入
192.168.17.50
發現可以通路,在主伺服器中輸入指令
ip a
發現ens33中
inet 192.168.17.50/32 ens33
,表示它現在已經綁定了這個虛拟ip。之後将主伺服器nginx和keepalived停止
[[email protected] sbin]# ./nginx -s stop
[[email protected] sbin]# systemctl stop keepalived.service
[[email protected] sbin]# ps -ef | grep keepalived
再繼續通路
192.168.17.50
,發現頁面内容沒變,依然能夠正常通路。并且在從伺服器中輸入指令
ip a
發現ens33中
inet 192.168.17.50/32 ens33
,表示它現在也已經綁定了這個虛拟ip
8.Nginx 原了解析
一個 master 和多個 woker 有好處:
- 可以使用 nginx –s reload 熱部署,利用 nginx 進行熱部署操作
- 每個 woker 是獨立的程序,如果有其中的一個 woker 出現問題,其他 woker 獨立的,繼續進行争搶,實作請求過程,不會造成服務中斷
發送請求,占用了 woker 的幾個連接配接數?2 或者 4 個
nginx 有一個 master,有四個 woker,每個 woker 支援最大的連接配接1024,支援的最大并發數是多少?普通的靜态通路最大并發數是: worker_connections * worker_processes /2,而如果是 HTTP 作 為反向代理來說,最大并發數量應該是 worker_connections * worker_processes/4。