nginx源碼編譯
本實驗采用nginx源碼編譯的安裝方式,需要準備一個tar包,可從nginx官網上下載。
下載地址:nginx: downloadhttps://nginx.org/en/download.html
將下載好的壓縮包傳到虛擬機中的自定義目錄下
[root@webserver ~]# ls
anaconda-ks.cfg Documents Music Pictures rh9.3.repo Videos
Desktop Downloads nginx-1.24.0.tar.gz Public Templates
解壓:
[root@webserver ~]# tar zxf nginx-1.24.0.tar.gz
解壓后,進入nginx-1.24.0/目錄下可看到以下目錄文件
[root@webserver ~]# cd nginx-1.24.0/
[root@webserver nginx-1.24.0]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
其中各個目錄作用如下
構建與配置相關
-
auto/
作用:包含大量用于configure
腳本的輔助腳本和配置文件。-
定義了不同操作系統、編譯器、模塊的檢測邏輯。
-
比如
auto/cc/
下是編譯器檢測,auto/modules
是模塊配置。 -
用戶無需手動修改,由
configure
自動調用。
-
-
configure
作用:Nginx 的核心配置腳本(可執行文件)。-
運行
./configure
會檢測系統環境、生成編譯所需的Makefile
。 -
通過參數(如
--prefix
、--with-http_ssl_module
)定制功能。
-
-
CHANGES
作用:英文版更新日志,記錄每個版本的功能變更、修復的Bug、新增特性(按版本倒序排列)。 -
CHANGES.ru
作用:俄文版更新日志(Nginx 作者 Igor Sysoev 是俄羅斯人),內容與CHANGES
一致,但面向俄語用戶。 -
LICENSE
作用:Nginx 的許可證文件(2-Clause BSD License),允許自由使用、修改和分發。 -
README
作用:簡潔的項目介紹,通常包含編譯安裝的快速指南和官方文檔鏈接。 -
man/
作用:Nginx 的手冊頁(Manual Pages),包含nginx.8
等文件。-
安裝后可通過
man nginx
查看命令行用法。
-
-
conf/
作用:存放默認的示例配置文件。-
如
nginx.conf
(主配置)、fastcgi.conf
(FastCGI 配置模板)。 -
安裝時會被復制到
--prefix/conf/
(默認/usr/local/nginx/conf/
)。
-
-
src/
作用:Nginx 源代碼的核心目錄,按模塊分層:-
src/core/
:核心代碼(如主函數、內存管理)。 -
src/http/
:HTTP 模塊(處理 Web 請求)。 -
src/mail/
:郵件代理模塊(POP3/IMAP/SMTP)。 -
src/stream/
:TCP/UDP 代理模塊(1.9.0+ 新增)。
-
-
html/
作用:默認的靜態網頁文件:-
index.html
(歡迎頁)和50x.html
(錯誤頁)。 -
安裝后位于
--prefix/html/
,是 Nginx 初始測試時訪問的頁面。
-
#創建nginx服務使用的用戶
useradd -s /sbin/nologin -M nginx -s /sbin/nologin為禁止遠程登陸 -M為不創建家目錄#安裝環境所需要的軟件
# dnf install gcc pcre-devel zlib-devel openssl-devel -y
#檢測環境
./configure --prefix=/usr/local/nginx \--user=nginx \--group=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_ssl_module \--with-stream_realip_module#縮小安裝,在nginx目錄下
cd nginx-1.24.0/
ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
vim auto/cc/gcc
# debug
#CFLAGS="$CFLAGS -g" ---將此行注釋掉即可#檢測完畢后編譯
make#安裝編譯完成后的可執行文件和庫文件以及配置文件到指定目錄下
make install
nginx平滑升級和回滾
?平滑升級需要兩個不同版本的nginx包存在
[root@webserver sbin]# curl -I 192.168.75.10 ---查看nginx版本
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Thu, 24 Jul 2025 02:29:25 GMT
Content-Type: text/html
Content-Length: 14
Last-Modified: Thu, 24 Jul 2025 02:12:36 GMT
Connection: keep-alive
ETag: "68819694-e"
Accept-Ranges: bytes[root@webserver sbin]# cp -p nginx nginx.old ---將24版nginx備份[root@webserver sbin]# ls
nginx nginx.old[root@webserver sbin]# /bin/cp -f /root/nginx-1.26.1/objs/nginx /usr/local/sbin/nginx ---將26版nginx放到該目錄下[root@webserver sbin]# ps aux | grep nginx ---查看進程
root 2715 0.0 0.0 4416 360 ? Ss 10:36 0:00 nginx: master process nginx
nobody 2716 0.0 0.2 12624 5172 ? S 10:36 0:00 nginx: worker process
root 2718 0.0 0.1 221664 2296 pts/0 S+ 10:36 0:00 grep --color= auto nginx[root@webserver sbin]# kill -USR2 2715(舊服務master進程號) ---重新加載配置文件、啟用新的工作進程(只啟動進程,不監聽端口),以實現平滑升級而不導致服務中斷。[root@webserver sbin]#
[root@webserver sbin]# ps aux | grep nginx
root 2715 0.0 0.1 4416 1844 ? Ss 10:36 0:00 nginx: master process nginx
nobody 2716 0.0 0.2 12624 5172 ? S 10:36 0:00 nginx: worker process
root 2720 0.0 0.1 4420 2516 ? S 10:36 0:00 nginx: master process nginx
nobody 2721 0.0 0.2 12624 5160 ? S 10:36 0:00 nginx: worker process
root 2723 0.0 0.1 221664 2296 pts/0 S+ 10:36 0:00 grep --color= auto nginx[root@webserver sbin]# kill -WINCH 2715 ---回收舊版本進程
[root@webserver sbin]#
[root@webserver sbin]# ps aux | grep nginx
root 2715 0.0 0.1 4416 1844 ? Ss 10:36 0:00 nginx: master process nginx
root 2720 0.0 0.1 4420 2516 ? S 10:36 0:00 nginx: master process nginx
nobody 2721 0.0 0.2 12624 5160 ? S 10:36 0:00 nginx: worker process
root 2737 0.0 0.1 221664 2280 pts/0 S+ 10:38 0:00 grep --color= auto nginx[root@webserver sbin]# curl -I 192.168.75.10
HTTP/1.1 200 OK
Server: nginx/1.26.1 #版本更新成功
Date: Thu, 24 Jul 2025 02:39:10 GMT
Content-Type: text/html
Content-Length: 14
Last-Modified: Thu, 24 Jul 2025 02:12:36 GMT
Connection: keep-alive
ETag: "68819694-e"
Accept-Ranges: bytes
?
?
nginx回滾
[root@webserver sbin]# curl -I 192.168.75.10
HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Thu, 24 Jul 2025 03:05:58 GMT
Content-Type: text/html
Content-Length: 14
Last-Modified: Thu, 24 Jul 2025 02:12:36 GMT
Connection: keep-alive
ETag: "68819694-e"
Accept-Ranges: bytes[root@webserver sbin]# ll
total 9400
-rwxr-xr-x 1 root root 3940656 Jul 24 10:35 nginx
-rwxr-xr-x 1 root root 5678264 Jul 23 15:40 nginx.old[root@webserver sbin]# mv nginx nginx.new
[root@webserver sbin]#
[root@webserver sbin]# mv nginx.old nginx
[root@webserver sbin]# ll
total 9400
-rwxr-xr-x 1 root root 5678264 Jul 23 15:40 nginx
-rwxr-xr-x 1 root root 3940656 Jul 24 10:35 nginx.new[root@webserver sbin]# ps aux | grep nginx
root 2795 0.0 0.1 9864 2496 ? Ss 11:01 0:00 nginx: master process nginx
root 2822 0.0 0.1 4420 2516 ? S 11:04 0:00 nginx: master process nginx
nobody 2823 0.0 0.2 12624 5180 ? S 11:04 0:00 nginx: worker process
root 2849 0.0 0.1 221664 2280 pts/0 S+ 11:06 0:00 grep --color=auto nginx[root@webserver sbin]# kill -HUP 2795 ---拉起舊版本master進程[root@webserver sbin]# ps aux | grep nginx
root 2795 0.0 0.1 9864 2496 ? Ss 11:01 0:00 nginx: master process nginx
root 2822 0.0 0.1 4420 2516 ? S 11:04 0:00 nginx: master process nginx
nobody 2823 0.0 0.2 12624 5180 ? S 11:04 0:00 nginx: worker process
nginx 2850 0.0 0.2 13760 4756 ? S 11:07 0:00 nginx: worker process
root 2852 0.0 0.1 221664 2296 pts/0 S+ 11:07 0:00 grep --color=auto nginx[root@webserver sbin]# kill -WINCH 2822 ---回收新版本master進程[root@webserver sbin]# ps aux | grep nginx
root 2795 0.0 0.1 9864 2496 ? Ss 11:01 0:00 nginx: master process nginx
root 2822 0.0 0.1 4420 2516 ? S 11:04 0:00 nginx: master process nginx
nginx 2850 0.0 0.2 13760 4756 ? S 11:07 0:00 nginx: worker process
root 2854 0.0 0.1 221664 2300 pts/0 S+ 11:08 0:00 grep --color=auto nginx[root@webserver sbin]# curl -I 192.168.75.10
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Thu, 24 Jul 2025 03:08:21 GMT
Content-Type: text/html
Content-Length: 14
Last-Modified: Thu, 24 Jul 2025 02:12:36 GMT
Connection: keep-alive
ETag: "68819694-e"
Accept-Ranges: bytes[root@webserver sbin]# kill -9 2822 ---[root@webserver sbin]# ps aux | grep nginx
root 2795 0.0 0.1 9864 2496 ? Ss 11:01 0:00 nginx: master process nginx
nginx 2850 0.0 0.2 13760 4756 ? S 11:07 0:00 nginx: worker process
root 2858 0.0 0.1 221664 2300 pts/0 S+ 11:08 0:00 grep --color=auto nginx[root@webserver sbin]# curl -I 192.168.75.10
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Thu, 24 Jul 2025 03:09:00 GMT
Content-Type: text/html
Content-Length: 14
Last-Modified: Thu, 24 Jul 2025 02:12:36 GMT
Connection: keep-alive
ETag: "68819694-e"
Accept-Ranges: bytes
?
?
?
源碼編譯nginx啟用system命令
#在瀏覽器中訪問
https://mailman.nginx.org/pipermail/nginx/2017-April/053368.html#復制配置文件并更改
[root@wb-nginx nginx-1.26.1]# vim /etc/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target[Service]
Type=forking
PIDFile=/run/nginx.pid ---改為當前nginx.pid的目錄
ExecStartPre=/usr/sbin/nginx -t ---改為當前nginx所在目錄
ExecStart=/usr/sbin/nginx ---改為當前nginx所在目錄
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true[Install]
WantedBy=multi-user.target#更改為
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx #此目錄為軟鏈接,實際指向/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true[Install]
WantedBy=multi-user.target#編輯完成后保存退出
#清空當前nginx的所有進程,避免影響systemctl重啟nginx服務
[root@webserver system]# pkill -f nginx#查看是否清空
[root@webserver system]# netstat -antulpe | grep 80#重載systemctl
[root@webserver system]# systemctl daemon-reload#啟動nginx
[root@webserver system]# systemctl start nginx#查看狀態
[root@webserver system]# systemctl status nginx.service
● nginx.service - The NGINX HTTP and reverse proxy serverLoaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: di>Active: active (running) since Thu 2025-07-24 12:47:07 CST; 13s agoProcess: 4073 ExecStartPre=/usr/local/nginx/sbin/nginx -t (code=exited, sta>Process: 4074 ExecStart=/usr/local/sbin/nginx (code=exited, status=0/SUCCES>Main PID: 4075 (nginx)Tasks: 2 (limit: 10702)Memory: 1.6MCPU: 13msCGroup: /system.slice/nginx.service├─4075 "nginx: master process /usr/local/sbin/nginx"└─4076 "nginx: worker process"
?
?
nginx全局配置:高并發
?
#進入/usr/local/nginx/conf
vim nginx.confuser nginx;
worker_cpu_affinity 0001 0010; #cup綁定進程
worker_processes 2; #設置開機自啟進程為2,通常與內核個數匹配
worker_rlimit_nofile 100000; #每個工作進程最大文件描述符限制
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;pid logs/nginx.pid;events {worker_connections 100000; #設置每個工作進程同時處理的最大連接數use epoll; #設置i/o模型為異步非阻塞
}[root@webserver conf]# cd /etc/security/
[root@webserver security]# vim limits.conf
#@student - maxlogins 4
* - nofile 100000
* - nproc 100000#* - nofile 100000
含義:對所有用戶(* 通配符),將單個進程的最大打開文件數(nofile 即 "number of open files")限制為 100000。
作用:防止因文件描述符耗盡導致服務崩潰(如高并發 Web 服務、數據庫等)。# * - nproc 100000
含義:對所有用戶,將最大進程數(nproc 即 "max user processes")限制為 100000。
作用:防止用戶創建過多進程拖垮系統(如 fork 炸彈攻擊)。
?
創建一個簡單的pc站點
#添加策略指向子配置文件
include "/usr/local/nginx/conf.d/*.conf";
#創建目錄存放子配置目錄
mkdir -p /usr/local/nginx/conf.d#編寫子配置目錄
[root@webserver conf.d]# vim /usr/local/nginx/conf.d/vhosts.conf
server{listen 80; #端口server_name www.test.org; #訪問域名root /web/html; #默認發布目錄index index.html; #默認發布文件
}#重啟nginx服務
systemctl restart nginx#編寫本地解析
[root@webserver conf.d]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.75.150 rh9
192.168.75.10 webserver
192.168.75.10 www.test.org #寫入域名
測試
[root@webserver conf.d]# curl 192.168.75.10 #訪問IP
192.168.75.10
[root@webserver conf.d]# curl www.test.org #訪問域名=
web_html
nginx賬戶認證功能
#創建nginx所需要的認證文件 當認證文件不存在時需要-c建立
[root@webserver conf.d]# htpasswd -cm /usr/local/nginx/.htpasswd admin
New password: #輸入密碼
Re-type new password: #再次輸入密碼
Adding password for user admin#創建新用戶 當認證文件存在時使用-c會覆蓋原文件內容
[root@webserver conf.d]# htpasswd -m /usr/local/nginx/.htpasswd yna
New password:
Re-type new password:
Adding password for user yna[root@webserver conf.d]# cat /usr/local/nginx/.htpasswd
admin:$apr1$QZW0/k7N$RPsbM9A80xtD5t9WASs4h1
yna:$apr1$mMCbF9jP$T0g/Pi52ZKK0j8Egg8KBS1#創建文件放置認證成功的內容
[root@webserver ~]# mkdir -p /web/nginx/test.org/yna/login
[root@webserver ~]#
[root@webserver ~]# echo login > /web/nginx/test.org/yna/login/index.html#編輯配置文件
server{listen 80;server_name www.test.org;root /web/html;index index.html;error_page 500 502 503 404 403 /errorpage/error.html;error_log /usr/local/nginx/logs/test.org.err;access_log /usr/local/nginx/logs/test.org.access;location = /test {return 200 "= \n";}location ^~ /test {return 200 "^~ \n";}location ~* /test {return 200 "~* \n";}location /login/{root /web/nginx/test.org/yna; #訪問目錄index index.html;auth_basic "PLIEASE input username and password"; # 是 Nginx 提供的指令,用來“打開” Basic 認證。auth_basic_user_file /usr/local/nginx/.htpasswd;#指定保存“賬號/密碼”的文件路徑}location /errorpage {root /web/;}
}#重啟并測試
[root@webserver conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@webserver conf.d]# nginx -s reload[root@webserver conf.d]# curl www.test.org/login/ -u yna:123456
login
?
location參數
#alias與root的區別root:路徑末尾是否加 / 都可以。
alias:末尾必須加 /,否則可能導致路徑錯誤location /static/ {root /var/www/html;
}
#請求/static/images/logo.png 實際路徑/var/www/html/static/images/logo.pnglocation /static/ {alias /var/www/assets/;
}
#請求 /static/images/logo.png 實際路徑/var/www/assets/images/logo.png
自定義錯誤界面
#創建錯誤存放錯誤界面內容的文件目錄
[root@webserver conf.d]# mkdir /web/errorpage
[root@webserver conf.d]# echo "bad" > /web/errorpage/error.html#編輯配置文件
server{listen 80;server_name www.test.org;root /web/html;index index.html;error_page 500 502 503 404 403 /errorpage/error.html; #寫入錯誤碼以及發生這些錯誤時應指向的文件#重啟并測試
[root@webserver conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@webserver conf.d]# nginx -s reload
[root@webserver conf.d]# curl www.test.org
web_html
[root@webserver conf.d]# curl www.test.org/ab
bad
自定義錯誤日志
#編寫配置文件
server{listen 80;server_name www.test.org;root /web/html;index index.html;error_page 500 502 503 404 403 /errorpage/error.html;error_log /usr/local/nginx/logs/test.org.err;access_log /usr/local/nginx/logs/test.org.access;#測試
檢測文件是否存在
vim /usr/loacl/nginx/conf/conf.d/vhosts.conf
echo default > /web/errorpage/default.html #寫入默認發布頁try_files $uri $uri.html $uri/index.html /errorpage/default.html; #在子配置文件寫入,先檢查磁盤上是否恰好存在與請求 URI 同名的文件。如果上一步沒找到,再嘗試給 URI 加 .html 后綴。如果前兩步都沒命中,再把它當成目錄,看目錄內有沒有 index.html。前三步都失敗時,Nginx 會內部重寫到這個回退 URI(相當于重新發起一次子請求)。#測試
[root@webserver conf.d]# curl www.test.org/a
default
長鏈接配置
[root@webserver conf.d]# cd /usr/local/nginx/conf
[root@webserver conf]# vim nginx.confhttp {include mime.types;default_type application/octet-stream;# default_type test/html;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 60 50; #設置用戶訪問超時時間,超時自動斷開 60為實際超時時間,50為顯示給用戶的超時時間keepalive_requests 120; #設置最大并發量Features: alt-svc AsynchDNS brotli GSS-API HTTP2 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM NTLM_WB PSL SPNEGO SSL TLS-SRP UnixSockets
[root@webserver conf]# curl -v 192.168.75.10
* Trying 192.168.75.10:80...
* Connected to 192.168.75.10 (192.168.75.10) port 80 (#0)
> GET / HTTP/1.1
> Host: 192.168.75.10
> User-Agent: curl/7.76.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: nginx/1.26.1
< Date: Sat, 26 Jul 2025 03:41:51 GMT
< Content-Type: text/html
< Content-Length: 14
< Last-Modified: Thu, 24 Jul 2025 02:12:36 GMT
< Connection: keep-alive
< Keep-Alive: timeout=50 #最大超時時間
< ETag: "68819694-e"
< Accept-Ranges: bytes
<
192.168.75.10
* Connection #0 to host 192.168.75.10 left intact[root@webserver ~]# dnf install telnet -y #安裝測試工具[root@webserver ~]# telnet 192.168.75.10 80
Trying 192.168.75.10...
Connected to 192.168.75.10.
Escape character is '^]'.
GET / HTTP/1.1 #手動寫入
Host: 192.168.75.10 #手動寫入HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Sat, 26 Jul 2025 03:03:30 GMT
Content-Type: text/html
Content-Length: 14
Last-Modified: Thu, 24 Jul 2025 02:12:36 GMT
Connection: keep-alive
Keep-Alive: timeout=50 #超時時間
ETag: "68819694-e"
Accept-Ranges: bytes192.168.75.10 #表示成功請求一次
?超時顯示:
作為下載服務器的配置
vim /etc/local/nginx/conf/conf.d/vhosts.conflocation /download{root /web/;autoindex on;autoindex_localtime on; #自動文件索引功能,默為offautoindex_exact_size off;#計算文件確切大小(單位bytes),off 顯示大概大小(單位K、M),默認onautoindex_format html;#顯示索引的頁面文件風格,默認htmlset $limit_rate 1024k;#限制響應客戶端傳輸速率(除GET和HEAD以外的所有方法),單位B/s,bytes/second,}[root@webserver conf.d]# curl www.test.org/download/
<html>
<head><title>Index of /download/</title></head>
<body>
<h1>Index of /download/</h1><hr><pre><a href="../">../</a>
<a href="yna">yna</a> 26-Jul-2025 11:05 200M
</pre><hr></body>
</html>[[root@webserver conf]# wget www.test.org/download/yna #嘗試下載
--2025-07-26 11:39:47-- http://www.test.org/download/yna
Resolving www.test.org (www.test.org)... 192.168.75.10
Connecting to www.test.org (www.test.org)|192.168.75.10|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 209715200 (200M) [application/octet-stream]
Saving to: ‘yna’yna 3%[=> ] 6.00M 1.01MB/s eta 3m 13s ^C #發現限速1mb左右
????????