Nginx 簡介
一、Nginx概述
1.1 概述
Nginx(“engine x”)是一個(gè)高性能的 HTTP /反向代理的服務(wù)器及電子郵件(IMAP/POP3)代理服務(wù)器。
官方測試nginx能夠支撐5萬并發(fā),并且cpu,內(nèi)存等資源消耗卻非常低,運(yùn)行非常穩(wěn)定。最重要的是開源,免費(fèi),可商用的。
Nginx還支持熱部署,幾乎可以做到7 * 24 小時(shí)不間斷運(yùn)行,即時(shí)運(yùn)行數(shù)個(gè)月也不需要重啟,還能夠在不間斷服務(wù)的情況下對軟件進(jìn)行升級維護(hù)。
1.2 Nginx應(yīng)用場景
1、單機(jī)環(huán)境下參考服務(wù)器配置。 并發(fā)連接數(shù)在7000+ -8000左右。 集群模式20000+。
2、作為 Web 服務(wù)器:相比 Apache,Nginx 使用更少的資源,支持更多的并發(fā)連接,體現(xiàn)更高的效率,這點(diǎn)使 Nginx尤其受到虛擬主機(jī)提供商的歡迎。能夠支持高達(dá) 50,000 個(gè)并發(fā)連接數(shù)的響應(yīng)。
3、作為負(fù)載均衡服務(wù)器:Nginx 既可以在內(nèi)部直接支持 Rails 和 PHP,也可以支持作為 HTTP代理服務(wù)器 對外進(jìn)行服務(wù)。Nginx 用 C 編寫, 不論是系統(tǒng)資源開銷還是 CPU 使用效率都比 Perlbal 要好的多。
4、作為郵件代理服務(wù)器:Nginx 同時(shí)也是一個(gè)非常優(yōu)秀的郵件代理服務(wù)器(最早開發(fā)這個(gè)產(chǎn)品的目的之一也是作為郵件代理服務(wù)器),Last.fm 描述了成功并且美妙的使用經(jīng)驗(yàn)。
5、Nginx 安裝非常的簡單,配置文件 非常簡潔(還能夠支持perl語法),Bug非常少的服務(wù)器。
二、Nginx安裝
2.1 進(jìn)入官網(wǎng)下載
2.2 安裝相關(guān)依賴
2.2.1 第一步
1、 避免:Nginx 與 httpd 發(fā)生端口沖突
卸載:可能已經(jīng)安裝的 httpd,從而避免發(fā)生<端口沖突>
(rpm -qa | grep -P "^httpd-([0-9].)+") && rpm -e --nodeps httpd || echo "未安裝"
2、 下載:Nginx 源碼安裝包,并解壓
cd ~
which wget || yum install -y wget
wget http://nginx.org/download/nginx-1.19.1.tar.gz
3、安裝其他依賴
yum install -y gcc pcre-devel zlib-devel
4、創(chuàng)建運(yùn)行賬戶nginx
useradd -M -s /sbin/nologin nginx
2.3 安裝nginx
-
解壓nginx-xx.tar.gz包
tar -axf nginx-1.19.1.tar.gz
-
進(jìn)入解壓目錄,執(zhí)行./configure 設(shè)置安裝路徑和運(yùn)行賬戶
cd ~/nginx-1.19.1 ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
-
make&&make install
make && make install
-
配置網(wǎng)頁
cat >/usr/local/nginx/conf/nginx.conf <<EOF worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; charset utf-8; server { listen 80; server_name localhost; include conf.d/*.conf; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } EOF #創(chuàng)建輔助配置文件目錄 [ -d /usr/local/nginx/conf/conf.d ] || mkdir -p /usr/local/nginx/conf/conf.d
2.4 設(shè)置環(huán)境變量,同時(shí)設(shè)置開機(jī)自啟
1、設(shè)置變量
cat > /etc/profile.d/nginx.sh<<EOF
export PATH="/usr/local/nginx/sbin:\$PATH"
EOF
2、刷新環(huán)境
source /etc/profile
3、啟停:Nginx 服務(wù)進(jìn)程
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.d/rc.local ## 設(shè)置:開機(jī)自啟動
chmod +x /etc/rc.d/rc.local
nginx 或 nginx -c /usr/local/nginx/conf/nginx.conf ## 啟動:Nginx 服務(wù)
2.5 訪問
直接瀏覽器輸入虛擬機(jī)ip地址測試
三、nginx常用命令和配置文件
3.1 常用命令
#查看版本
nginx -v
#檢查配置文件錯(cuò)誤
nginx -t
#啟動nginx
nginx
#關(guān)閉nginx
nginx -s stop
#重加載nginx
nginx -s reload
3.2 配置文件詳細(xì)講解
#配置文件位置
位置:/usr/local/nginx/conf/nginx.conf
★ 查看:<Nginx 主配置文件>的<默認(rèn)配置>
# cat /usr/local/nginx/conf/nginx.conf | grep -vE "^\s*(#|$)"
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
## 導(dǎo)入:<MIME 類型定義配置文件>,其<路徑>相對于[當(dāng)前目錄]
## <mime.type 配置文件>定義:什么類型的<文件>,需用什么<應(yīng)用組件>打開?
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
charset utf-8; ## 設(shè)置語音編碼為utf-8,使其頁面支持中文
server {
listen 80; ## 設(shè)置:偵聽端口和 IP 地址
server_name localhost;
## 導(dǎo)入:<自定義配置文件>(可以是相對路徑 ? 以<主配置文件>的<當(dāng)前目錄>為<根目錄>)
include conf.d/*.conf;
location / {
root html;
index index.html index.htm; ## 定義:<默認(rèn)首頁文件名>
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
3.3 理解:<Nginx 主配置文件>的<語法格式>及<默認(rèn)配置>
┍
main 主配置段 ┤ worker_processes 1; ## 指定:<nginx: worker process 工作進(jìn)程>的<數(shù)量>
│ ## 單 CPU,建議:1 個(gè)
│ ## 多 CPU,建議:CPU 總核心數(shù)
└
┍ events {
evens 事件配置段 ┤ worker_connections 1024; ## 設(shè)置:單個(gè)工作進(jìn)程的<并發(fā)最大連接>
└ }
┍ http {
│ include mime.types;
│ default_type application/octet-stream;
│ sendfile on;
│ keepalive_timeout 65;
│ server { ─────────────────────────────┐
│ listen 80; │
http 網(wǎng)站配置段 ┤ server_name localhost; │
│ ┍ location / { │
│ │ root html; │
│ │ index index.html index.htm; ├定義:虛擬主機(jī)
│ └ } │
│ error_page 500 502 503 504 /50x.html; │
│ ┍ location = /50x.html { │
│ │ root html; │
│ └ } │
│ }─────────────────────────────────────┘
└ }
四、Nginx反向代理與負(fù)載均衡
4.1 反向代理
**反向代理:**正好相反。對于客戶端來說,反向代理就好像目標(biāo)服務(wù)器。并且客戶端不需要進(jìn)行任何設(shè)置??蛻舳讼蚍聪虼戆l(fā)送請求,接著反向代理判斷請求走向何處,并將請求轉(zhuǎn)交給客戶端,使得這些內(nèi)容就好像它自己的一樣,一次客戶端并會并會不感知到反向代理后面的服務(wù),因此不需要客戶端做任何設(shè)置,只需要把反向代理服務(wù)器當(dāng)成真正的服務(wù)器就好了。
4.2 負(fù)載均衡
負(fù)載均衡建立在現(xiàn)有網(wǎng)絡(luò)結(jié)構(gòu)之上,它提供一種鏈家有效透明的方法擴(kuò)展網(wǎng)絡(luò)設(shè)備和服務(wù)器的寬帶、增加吞吐量,加強(qiáng)網(wǎng)絡(luò)數(shù)據(jù)處理能力,提高網(wǎng)絡(luò)的靈活性和可用性。
Nginx proxy反向代理模塊 (默認(rèn)安裝)
功能 1:可以作為<應(yīng)用程序網(wǎng)關(guān)>
對外隱藏:<內(nèi)網(wǎng)服務(wù)器>的<IP地址>
對外發(fā)布:<內(nèi)網(wǎng)服務(wù)器>的<服務(wù)資源>
功能 2:可以實(shí)現(xiàn)<動靜分離>
通過 location URI 地址匹配,實(shí)現(xiàn):轉(zhuǎn)發(fā)<動態(tài)網(wǎng)頁的請求>。
vim /usr/local/nginx/conf/nginx.conf
location / {
proxy_pass http://服務(wù)器池;
} ##<上游服務(wù)器>指 被反向代理的服務(wù)器
或 proxy_pass http://后端服務(wù)器池的名字;
## 例如:
vim /usr/local/nginx/conf/nginx.conf
....
location / {
....
proxy_pass http://xm;
}
....
Nginx upstream 上游模塊(負(fù)載均衡)
#指令 和 功能
upstream 定義:一個(gè)命名的<后端服務(wù)器池>
server 定義:服務(wù)器池里的服務(wù)器
ip_hash 啟用:基于<IP地址哈希值>的<負(fù)載均衡算法>
負(fù)載均衡的部署方式
1.輪詢(默認(rèn))
每個(gè)請求按時(shí)間順序逐一分配到不同的后端服務(wù)器,如果后端服務(wù)器down掉,能自動剔除
vim /usr/local/nginx/conf/nginx.conf
....
http {
upstream xm { ##xm 服務(wù)器池的命名,不要有下劃線
server ip地址:80; ##上游服務(wù)器ip:端口
server ip地址:80;
}
...
}
保存后出去重載文件
nginx -s reload
# 例如:
vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
charset utf-8;
upstream test { ## 定義test組
server 192.168.106.147:80; ## 定義test組里有那些機(jī)器
server 192.168.106.148:80; ## 這幾條ip換成自己web服務(wù)器的IP地址
}
server {
listen 80;
server_name localhost;
include conf.d/*.conf;
location / {
root html;
index index.html index.htm;
proxy_pass http://test; ## 有請求就轉(zhuǎn)發(fā)到test組
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
2.weight (權(quán)重)
權(quán)重越大,被分配到的任務(wù)越多,被訪問的概率越高
vim /usr/local/nginx/conf/nginx.conf
....
http {
upstream xm {
server ip地址:80 weight=7; #默認(rèn)weight=1
server ip地址:80 weight=3;
}
...
}
#如上例,分別是70%和30%
保存后出去重載文件
nginx -s reload
## 例如:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
charset utf-8;
upstream test { ## 定義test組
server 192.168.106.147:80 weight=7; ## 定義test組內(nèi)的機(jī)器
server 192.168.106.148:80 weight=3; ## 定義test組內(nèi)的機(jī)器
}
server {
listen 80;
server_name localhost;
include conf.d/*.conf;
location / {
root html;
index index.html index.htm;
proxy_pass http://test;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
3.ip_hash(哈希算法)
客戶第一次訪問某個(gè)服務(wù)器后短時(shí)間斷開,再次訪問自動定位到該服務(wù)器
vim /usr/local/nginx/conf/nginx.conf
....
http {
upstream xm {
ip_hash;
server ip地址:80;
server ip地址:80;
}
...
}
保存后出去重載文件
nginx -s reload
# 例如:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
charset utf-8;
upstream test {
ip_hash; ## 定義哈希算法
server 192.168.106.147:80;
server 192.168.106.148:80;
}
server {
listen 80;
server_name localhost;
include conf.d/*.conf;
location / {
root html;
index index.html index.htm;
proxy_pass http://test;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
五、Nginx+php動靜分離
為了加快網(wǎng)站的解析速度,可以把動態(tài)頁面和靜態(tài)頁面由不同的服務(wù)器來解析,加快解析速度,降低原來單個(gè)服務(wù)器的壓力。一般來說,都需要將動態(tài)資源和靜態(tài)資源分開,由于Nginx的高并發(fā)和靜態(tài)資源緩存等特性,經(jīng)常將靜態(tài)資源部署在Nginx上。如果請求的是靜態(tài)資源,直接到靜態(tài)資源目錄獲取資源,如果是童泰資源的請求,則利用反向代理的原理,把請求轉(zhuǎn)發(fā)給對應(yīng)后臺應(yīng)用去處理,從而實(shí)現(xiàn)動靜分離。
1. 動態(tài)和靜態(tài)分離主要是通過nginx + PHP FPM實(shí)現(xiàn)的,其中nginx處理圖片、html等靜態(tài)文件,PHP處理動態(tài)程序。
2. 動態(tài)靜態(tài)分離是指在web服務(wù)器架構(gòu)中,將靜態(tài)頁面與動態(tài)頁面或靜態(tài)內(nèi)容接口與動態(tài)內(nèi)容接口分離,從而提高整個(gè)服務(wù)的訪問性能和可維護(hù)性的架構(gòu)設(shè)計(jì)方法。
3. 簡單地說,當(dāng)用戶請求時(shí),如果他只是訪問靜態(tài)請求,比如圖片和html, nginx會直接返回。如果他發(fā)送了一個(gè)動態(tài)請求,nginx會把這個(gè)請求發(fā)送給程序進(jìn)行動態(tài)處理
1、配合php實(shí)現(xiàn)動態(tài)頁面和靜態(tài)頁面分開處理
1、刪除httpd
rpm -e httpd --nodeps
2、安裝php及其組件
yum install -y php php-devel php-mysql
yum install -y php-fpm
3、啟動php及其組件,同時(shí)將其加入開機(jī)自啟
systemctl enable php-fpm
systemctl start php-fpm
2、修改運(yùn)行用戶
sed -i -r 's/^\s*user\s*=.*/user = nginx/' /etc/php-fpm.d/www.conf
sed -i -r 's/^\s*group\s*=.*/group = nginx/' /etc/php-fpm.d/www.conf
重啟服務(wù)
systemctl restart php-fpm
3、修改配置文件
cat > /usr/local/nginx/conf/conf.d/location_php.conf <<EOF
location ~ \.php$ {
root html;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
if (!-f \$document_root\$fastcgi_script_name) {
return 404;
}
}
EOF
重載nginx文章來源:http://www.zghlxwxcb.cn/news/detail-501399.html
nginx -s reload
4、編寫php頁面文章來源地址http://www.zghlxwxcb.cn/news/detail-501399.html
cat > /usr/local/nginx/html/index.php <<EOF
<?php
phpinfo();
?>
EOF
到了這里,關(guān)于大數(shù)據(jù)云計(jì)算運(yùn)維之Nginx反向代理與負(fù)載均衡的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!