Nginx介紹
Nginx是一個高性能的HTTP和反向代理服務器,也是一個郵件代理服務器。由俄羅斯的程序設計師Igor Sysoev所開發,官方測試nginx能夠支撐5萬并發鏈接,并且cpu、內存等資源消耗卻非常低,運行非常穩定。所以其特點是占有內存少,并發能力強,事實上Nginx的并發能力確實在同類型的網頁服務器中表現較好。
Nginx基礎特性
模塊化設計,較好的擴展性
高可靠性
支持熱部署:不停機更新配置文件,升級版本,更換日志文件
低內存消耗:10000個keep-alive連接模式下的非活動連接,僅需2.5M內存
event-driven,aio,mmap,sendfile
nginx的進程結構
web 請求處理機制
多進程方式:服務器每接收到一個客戶端請求就有服務器的主進程生成一個子進程響應客戶端,直
到用戶關閉連接,這樣的優勢是處理速度快,子進程之間相互獨立,但是如果訪問過大會導致服務
器資源耗盡而無法提供請求
多線程方式:與多進程方式類似,但是每收到一個客戶端請求會有服務進程派生出一個線程和此客
戶端進行交互,一個線程的開銷遠遠小于一個進程,因此多線程方式在很大程度減輕了 web 服務器
對系統資源的要求,但是多線程也有自己的缺點,即當多個線程位于同一個進程內工作的時候,可
以相互訪問同樣的內存地址空間,所以他們相互影響,一旦主進程掛掉則所有子線程都不能工作
了, IIS服務器使用了多線程的方式,需要間隔一段時間就重啟一次才能穩定。Nginx 是多進程組織模型,而且是一個由 Master 主進程和 Worker 工作進程組成。
主進程 (master process) 的功能:
對外接口:接收外部的操作(信號)
對內轉發:根據外部的操作的不同,通過信號管理 Worker
監控:監控 worker 進程的運行狀態,worker 進程異常終止后,自動重啟 worker 進程
讀取Nginx 配置文件并驗證其有效性和正確性
建立、綁定和關閉socket連接
按照配置生成、管理和結束工作進程
接受外界指令,比如重啟、升級及退出服務器等指令
不中斷服務,實現平滑升級,重啟服務并應用新的配置
開啟日志文件,獲取文件描述符
不中斷服務,實現平滑升級,升級失敗進行回滾處理
編譯和處理perl腳本
工作進程( worker process )的功能:
所有 Worker 進程都是平等的
實際處理:網絡請求,由 Worker 進程處理
Worker進程數量:一般設置為核心數,充分利用CPU資源,同時避免進程數量過多,導致進程競爭
CPU資源,
增加上下文切換的損耗
接受處理客戶的請求
將請求依次送入各個功能模塊進行處理
I/O調用,獲取響應數據
與后端服務器通信,接收后端服務器的處理結果
緩存數據,訪問緩存索引,查詢和調用緩存數據
發送請求結果,響應客戶的請求
接收主程序指令,比如重啟、升級和退出等
Nginx模塊介紹
核心模塊:是 Nginx 服務器正常運行必不可少的模塊,提供錯誤日志記錄 、配置文件解析 、事件 驅動機制 、進程管理等核心功能
標準HTTP模塊:提供 HTTP 協議解析相關的功能,比如: 端口配置 、 網頁編碼設置 、 HTTP響應 頭設置 等等
可選HTTP模塊:主要用于擴展標準的 HTTP 功能,讓 Nginx 能處理一些特殊的服務,比如: Flash
多媒體傳輸 、解析 GeoIP 請求、 網絡傳輸壓縮 、 安全協議 SSL 支持等
郵件服務模塊:主要用于支持 Nginx 的 郵件服務 ,包括對 POP3 協議、 IMAP 協議和 SMTP協議的 支持
Stream服務模塊: 實現反向代理功能,包括TCP協議代理
第三方模塊:是為了擴展 Nginx 服務器應用,完成開發者自定義功能,比如: Json 支持、 Lua 支 持等
nginx的安裝
Nginx 版本
Mainline version 主要開發版本,一般為奇數版本號,比如1.19
Stable version 當前最新穩定版,一般為偶數版本,如:1.20
Legacy versions 舊的穩定版,一般為偶數版本,如:1.18
Nginx安裝可以使用yum或源碼安裝,但是推薦使用源碼編譯安裝
yum 的版本比較舊
編譯安裝可以更方便自定義相關路徑
使用源碼編譯可以自定義相關功能,更方便業務的上的使用
[root@Nginx ~]# dnf install gcc pcre-devel zlib-devel openssl-devel -y
[root@Nginx nginx-1.24.0]# useradd -s /sbin/nologin -M nginx
[root@Nginx nginx]# tar zxf nginx-1.24.0.tar.gz
[root@Nginx nginx-1.24.0]# useradd -s /sbin/nologin -M nginx
[root@Nginx nginx]# cd nginx-1.24.0/
[root@Nginx nginx-1.24.0]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[root@Nginx nginx-1.24.0]# ./configure --prefix=/usr/local/nginx \
--user=nginx \ # 指定nginx運行用戶
--group=nginx \ # 指定nginx運行組
--with-http_ssl_module \ # 支持https://
--with-http_v2_module \ # 支持http版本2
--with-http_realip_module \ # 支持ip透傳
--with-http_stub_status_module \ # 支持狀態頁面
--with-http_gzip_static_module \ # 支持壓縮
--with-pcre \ # 支持正則
--with-stream \ # 支持tcp反向代理
--with-stream_ssl_module \ # 支持tcp的ssl加密
--with-stream_realip_module # 支持tcp的透傳ip
[root@Nginx nginx-1.24.0]# make && make install
配置nginx的啟動文件
[root@Nginx ~]# vim /lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@Nginx ~]# systemctl daemon-reload
[root@Nginx ~]# systemctl start nginx
核心配置示例
1、新建一個PCweb站點
(1)、在總配置文件中定義子配置文件路徑
use 指定nginx的工作模式。nginx支持的工作模式有 select 、 poll 、 kqueue 、 epoll、 rtsig 和 /dev/poll 。其中 select 和 poll 都是標準的工作模式, kqueue 和 epoll 是高效的工作模式,不同的是 epoll 用在Linux平臺上,而 kqueue 用在BSD系統中,因為Mac基于BSD,所以Mac也得用這個模式,對于Linux系統,epoll工作模式是首選。
?
vim /usr/local/nginx/conf/nginx.conf
新建子配置文件路徑并編輯子配置文件
mkdir /usr/local/nginx/conf.d
vim /usr/local/nginx/conf.d/vhost.conf#寫入網站首頁內容
echo www.bjhzj.org:hello world! > /data/web/html/index.html
配置訪問不同路徑的網頁內容
vim /usr/local/nginx/conf.d/vhost.conf#寫入該路徑的網頁內容
echo test1:hello world! > /data/web/test1/index.html
創建軟連接,定義路徑別名
location 的詳細使用
vim /usr/local/nginx/conf.d/vhost.conf
echo web2/test/index.html > /data/web2/test/index.html
nginx -s reload
區分大小寫:~
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf [root@nginx ~]# mkdir /data/web3/HTML
[root@nginx ~]# mkdir /data/web3/html[root@nginx ~]# echo html/HTML > /data/web3/html/index.html
[root@nginx ~]# nginx -s reload
不區分大小寫:~*
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
[root@nginx ~]# echo ~*:html/HTML > /data/web3/html/index.html
用戶認證
htpasswd -cm /usr/local/nginx/.htpasswd admin
vim /usr/local/nginx/conf.d/vhost.conf
mkdir /data/web/gyj -p
echo bjh > /data/web/bjh/index.html
nginx -s reload
自定義錯誤頁面

自定義錯誤日志
[root@nginx ~]# ll /usr/local/nginx/logs/
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
[root@nginx ~]# mkdir /var/log/gaoyingjie.org[root@nginx ~]#cat /var/log/gaoyingjie.org/access.log
[root@nginx ~]#cat /var/log/gaoyingjie.org/error.log
檢測文件是否存在
若不存在則定義到defaul.html頁面
編輯配置文件
vim /usr/local/nginx/conf.d/vhost.conf
mkdir /data/web/html/error
echo error default > /data/web/html/error/default.html
curl www.gaoyingjie.org/123.html #不存在123.html文件
測試
nginx的長鏈接
nginx 做為下載服務器
輸入www.gaoyingjie.org/download進去下載頁
nginx的狀態頁
nginx的變量使用
添加echo模塊
上傳echo模塊的壓縮包,查看已安裝好的配置文件和模塊:
nginx -V
重新編譯模塊:
[root@nginx ~]# nginx -V
nginx version: nginx/1.24.0
built by gcc 11.4.1 20230605 (Red Hat 11.4.1-2) (GCC)
built with OpenSSL 3.0.7 1 Nov 2022
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --user=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_realip_module #將--add-module=/root/echo-nginx-module-0.63添加到已編譯的模塊后面重新編譯
[root@nginx nginx-1.24.0]# ./configure --prefix=/usr/local/nginx --user=nginx --user=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_realip_module --add-module=/root/echo-nginx-module-0.63[root@nginx nginx-1.24.0]#make &&make install
內置變量
編輯子配置文件:
[root@php ~]# vim /usr/local/nginx/conf.d/vhosts.conf
[root@php ~]# mkdir /data/web/html/var
[root@php ~]# echo /data/web/html/var > /data/web/html/var/index.html
[root@php ~]# curl www.gaoyingjie.org/var?name=gyj&&id=6666
自定義變量
假如需要自定義變量名稱和值,使用指令 set $variable value;
指定 key 并給其定義一個變量,變量可以調用 Nginx 內置變量賦值給 key ,另外set 定義格式為 set $key value , value 可以是 text, variables 和兩者的組合。
編輯子配置文件:
[root@php ~]# vim /usr/local/nginx/conf.d/vhosts.conf
[root@php ~]# curl www.gaoyingjie.org/var
nginx rewrite的相關功能
rewrite模塊指令
if指令
= # 比較變量和字符串是否相等,相等時 if 指令認為該條件為 true ,反之為 false
!= # 比較變量和字符串是否不相等,不相等時 if 指令認為條件為 true ,反之為 false
~ # 區分大小寫字符,可以通過正則表達式匹配,滿足匹配條件為真,不滿足匹配條件為假
!~ # 區分大小寫字符 , 判斷是否匹配,不滿足匹配條件為真,滿足匹配條件為假
~* # 不區分大小寫字符,可以通過正則表達式匹配,滿足匹配條件為真,不滿足匹配條件為假
!~* # 不區分大小字符 , 判斷是否匹配,滿足匹配條件為假,不滿足匹配條件為真
-f 和 !-f # 判斷請求的文件是否存在和是否不存在
-d 和 !-d # 判斷請求的目錄是否存在和是否不存在
-x 和 !-x # 判斷文件是否可執行和是否不可執行
-e 和 !-e # 判斷請求的文件或目錄是否存在和是否不存在 ( 包括文件,目錄,軟鏈接 )
# 注意:
# 如果 $ 變量的值為空字符串或 0 ,則 if 指令認為該條件為 false ,其他條件為 true 。
#nginx 1.0.1 之前 $ 變量的值如果以 0 開頭的任意字符串會返回 false
編輯配置文件:
[root@php ~]# vim /usr/local/nginx/conf.d/vhosts.conf
[root@php ~]# nginx -s reload
[root@php ~]# mkdir /data/web/html/{test1,test2}
[root@php ~]# echo /data/web/html/test1 > /data/web/html/test1/index.html
[root@php ~]# echo /data/web/html/test2 > /data/web/html/test2/index.html
rewrite案例
rewirte正則表達式格式:
. # 匹配除換行符以外的任意字符
\w # 匹配字母或數字或下劃線或漢字
\s # 匹配任意的空白符
\d # 匹配數字
\b # 匹配單詞的開始或結束
^ # 匹配字付串的開始
$ # 匹配字符串的結束
* # 匹配重復零次或更多次
+ # 匹配重復一次或更多次
? # 匹配重復零次或一次
(n) # 匹配重復 n 次
{n,} # 匹配重復 n 次或更多次
{n,m} # 匹配重復 n 到 m 次
*? # 匹配重復任意次,但盡可能少重復
+? # 匹配重復 1 次或更多次,但盡可能少重復
?? # 匹配重復 0 次或 1 次,但盡可能少重復
{n,m}? # 匹配重復 n 到 m 次,但盡可能少重復
{n,}? # 匹配重復 n 次以上,但盡可能少重復
\W # 匹配任意不是字母,數字,下劃線,漢字的字符
\S # 匹配任意不是空白符的字符
\D # 匹配任意非數字的字符
\B # 匹配不是單詞開頭或結束的位置
[^x] # 匹配除了 x 以外的任意字符
[^lee] # 匹配除了 magedu 這幾個字母以外的任意字符
rewrite案例:break與last
break:用于中斷當前相同作用域(location)中的其他 Nginx 配置 與該指令處于同一作用域的Nginx 配置中,位于它前面的配置生效 位于后面的 ngx_http_rewrite_module 模塊中指令就不再執行 Nginx服務器在根據配置處理請求的過程中遇到該指令的時候,回到上一層作用域繼續向下讀取配置,該指令可以在server 塊和 locationif 塊中使用。
編輯配置文件:
[root@php ~]# vim /usr/local/nginx/conf.d/vhosts.conf
[root@php ~]# nginx -s reload
測試:
[root@php ~]# mkdir /data/web/html/break
[root@php ~]# echo /data/web/html/break > /data/web/html/break/index.html
[root@php ~]# mkdir /data/web/html/last
[root@php ~]# echo /data/web/html/last > /data/web/html/last/index.html
rewrite案例:自動跳轉https
判斷文件是否存在:

nginx防盜鏈
準備一個web服務器(172.25.254.10),寫入網站內容,在該站點盜取另一個(172.25.254.100)的圖片資源。
<html><head><meta http-equiv=Content-Type content="text/html;charset=utf-8"><title>盜鏈</title>
</head><body><img src="http://www.gaoyingjie.org/images/1.png" ><h1 style="color:red">歡迎大家</h1><p><a href=http://www.gaoyingjie.org</a>出門見喜</p></body></html>
在172.25.254.100的服務器的images文件夾內上傳圖片1.png。
瀏覽器輸入:http://172.25.254.10可以看到該站點自動連接到100服務器的圖片
實現防盜鏈
配置文件:
在100服務器http的發布目錄內上傳盜鏈圖片,意味著當其他站點盜鏈1.png時,會自動替換成盜鏈圖片。
?重啟nginx后,再次訪問:http://172.25.254.10,就會出現盜鏈圖片。
nginx的反向代理
指定location實現反向代理(動靜分離)
參數介紹:
proxy_pass;
# 用來設置將客戶端請求轉發給的后端服務器的主機
# 可以是主機名 ( 將轉發至后端服務做為主機頭首部 ) 、 IP 地址:端口的方式
# 也可以代理到預先設置的主機群組,需要模塊 ngx_http_upstream_module 支持
proxy_pass_header field;
# 透傳
# 默認 nginx 在響應報文中不傳遞后端服務器的首部字段 Date, Server, X-Pad, X-Accel 等參數
# 如果要傳遞的話則要使用 proxy_pass_header field 聲明將后端服務器返回的值傳遞給客戶端
#field 首部字段大小不敏感
# 示例 : 透傳后端服務器的 Server 和 Date 首部給客戶端 , 同時不再響應報中顯示前端服務器的 Server 字段
proxy_pass_header Server;
proxy_pass_header Date;
proxy_pass_request_body on | off;
# 是否向后端服務器發送 HTTP 實體部分 , 可以設置在 http,server 或 location 塊,默認即為開啟
proxy_pass_request_headers on | off;
# 是否將客戶端的請求頭部轉發給后端服務器,可以設置在 http,server 或 location 塊,默認即為開啟
proxy_set_header;
# 可更改或添加客戶端的請求頭部信息內容并轉發至后端服務器,比如在后端服務器想要獲取客戶端的真實 IP 的
時候,就要更改每一個報文的頭部
proxy_connect_timeout time;
# 配置 nginx 服務器與后端服務器嘗試建立連接的超時時間,默認為 60 秒
用法如下: proxy_connect_timeout 6s;
#60s 為自定義 nginx 與后端服務器建立連接的超時時間 , 超時會返回客戶端 504 響應碼
proxy_read_timeout time;
# 配置 nginx 服務器向后端服務器或服務器組發起 read 請求后,等待的超時時間,默認 60s
proxy_send_timeout time;
# 配置 nginx 項后端服務器或服務器組發起 write 請求后,等待的超時 時間,默認 60s
proxy_http_version 1.0;
# 用于設置 nginx 提供代理服務的 HTTP 協議的版本,默認 http 1.0
proxy_ignore_client_abort off;
# 當客戶端網絡中斷請求時, nginx 服務器中斷其對后端服務器的請求。即如果此項設置為 on 開啟,則服務器、
會忽略客戶端中斷并一直等著代理服務執行返回,如果設置為 off ,則客戶端中斷后 Nginx 也會中斷客戶端請求
并立即記錄 499 日志,默認為 off 。
準備兩臺webserver,172.25.254.10、172.25.254.20,在10上安裝php,并寫入內容
vim/var/www/index.php
在20上寫入網站首頁內容并修改端口為8080:
echo static:172.25.254.20 > /var/www/html/static/index.htmlvim /etc/httpd/conf/httpd.conf
在100上nginx配置文件:、
反向代理的緩存功能
緩存配置:在nginx的總配置文件http塊內
子配置文件:
檢測:驗證緩存目錄結構及文件大小
nginx的負載均衡
實驗環境
主機 | IP |
nginx | 172.25.254.100 |
apache(server) | 172.25.254.10 |
apache1(server) | 172.25.254.20 |
部署后端的兩臺server
#server1
[root@apache20 ~]# yum install httpd -y
[root@apache20 ~]# echo "web1 172.25.254.10" > /var/www/html/index.html
[root@apache20 ~]# systemctl enable --now httpd
#server2
[root@apache30 ~]# yum install httpd -y
[root@apache30 ~]# echo "web2 172.25.254.20" >> /var/www/html/index.html
[root@apache30 ~]# systemctl enable --now httpd#訪問測試
[root@centos8 ~]# curl http://172.25.254.20
web1 172.25.254.10
[root@centos8 ~]# curl http://172.25.254.30
web2 172.25.254.20
基于cookie的會話綁定
?檢測:基于cookie值的變化而訪問不同的服務器,cookie值相同則訪問相同服務器,實現會話綁定
實現nginx的四層負載均衡
[root@apache20 ~]# yum install mariadb-server -y
[root@apache20 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
#server1
[root@server1 ~]# mysql -e "grant all on *.* to gyj@'%' identified by 'gyj';"#server2
[root@server2 ~]# mysql -e "grant all on *.* to gyj@'%' identified by 'gyj';"
在兩臺server中開啟mysql,查看server_id:
mysql -ugyj -pgyj -h 172.25.254.10 -e "select @@server_id"
配置nginx:
vim /usr/local/nginx/conf.d/tcp.conf
因為該配置文件tcp.conf為四層代理,故在總配置文件的http塊之外加上子配置文件路徑
vim /usr/local/nginx/conf/nginx.conf
在100主機上下載mariadb客戶端:
dnf install mariadb -y
重啟nginx并訪問測試:
mysql -u gyj -pgyj -h 172.25.254.100
一次登錄為server_id為10
二次登錄為20:
dns
在后端兩臺server下載dns,并且修改配置文件
vim /etc/named.conf
vim /etc/named.rfc1912.zones
配置named.gaoyingjie.org文件:
cd /var/named/
cp named.localhost named.gaoyingjie.org -p
vim named.gaoyingjie.org
將這臺10server的配置文件遠程復制到另一臺server20:
scp -p /etc/nama.{conf,rfc1912.zones} root@172.25.254.20:/etc/
配置20的named.gaoyingjie.org文件
[root@apache1 named]# cp named.localhost named.gaoyingjie.org
[root@apache1 named]# vim named.gaoyingjie.org
dig www.gaoyingjie.org 172.25.254.10
dig www.gaoyingjie.org 172.25.254.20
修改nginx的配置文件:
vim /usr/local/nginx/conf.d/tcp.conf
啟動兩臺server的dns,并且查看是否解析成功:
dig www.gaoyingjie.org 172.25.254.10
dig www.gaoyingjie.org 172.25.254.20