二、.Nginx 架構和安裝
2.1 Nginx 概述
2.1.1 Nginx 介紹
Nginx:engine X ,2002年開發,分為社區版和商業版(nginx plus ) 2019年3月11日 F5 Networks 6.7億美元的價格收購 Nginx是免費的、開源的、高性能的HTTP和反向代理服務器、郵件代理服務器、以及TCP/UDP代理服務 器
解決C10K問題(10K Connections)
Nginx官網:http://nginx.org
nginx的其它的二次發行版:
- Tengine:由淘寶網發起的Web服務器項目。它在Nginx的基礎上,針對大訪問量網站的需求,添加 了很多高級功能和特性。Tengine的性能和穩定性已經在大型的網站如淘寶網,天貓商城等得到了 很好的檢驗。它的最終目標是打造一個高效、穩定、安全、易用的Web平臺。從2011年12月開始, Tengine成為一個開源項目官網: http://tengine.taobao.org/
- OpenResty:基于 Nginx 與 Lua 語言的高性能 Web 平臺, 章亦春團隊開發,官網:http://openr esty.org/cn/
2.1.2 Nginx 功能介紹
- 靜態的web資源服務器html,圖片,js,css,txt等靜態資源
- http/https協議的反向代理
- 結合FastCGI/uWSGI/SCGI等協議反向代理動態資源請求
- tcp/udp協議的請求轉發(反向代理)
- imap4/pop3協議的反向代理
2.1.3 基礎特性
- 模塊化設計,較好的擴展性
- 高可靠性
- 支持熱部署:不停機更新配置文件,升級版本,更換日志文件
- 低內存消耗:10000個keep-alive連接模式下的非活動連接,僅需2.5M內存
- event-driven,aio,mmap,sendfile
2.1.4Web 服務相關的功能
- 虛擬主機(server)
- 支持 keep-alive 和管道連接(利用一個連接做多次請求)
- 訪問日志(支持基于日志緩沖提高其性能)
- url rewirte
- 路徑別名
- 基于IP及用戶的訪問控制
- 支持速率限制及并發數限制
- 重新配置和在線升級而無須中斷客戶的工作進程
2.2 Nginx 架構和進程
2.2.1 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調用,獲取響應數據
- 與后端服務器通信,接收后端服務器的處理結果
- 緩存數據,訪問緩存索引,查詢和調用緩存數據
- 發送請求結果,響應客戶的請求
- 接收主程序指令,比如重啟、升級和退出等
2.2.2 Nginx 進程間通信
- 工作進程是由主進程生成的,主進程使用fork()函數,在Nginx服務器啟動過程中主進程根據配置文件決 定啟動工作進程的數量,然后建立一張全局的工作表用于存放當前未退出的所有的工作進程,主進程生 成工作進程后會將新生成的工作進程加入到工作進程表中,并建立一個單向的管道并將其傳遞給工作進 程,該管道與普通的管道不同,它是由主進程指向工作進程的單向通道,包含了主進程向工作進程發出 的指令、工作進程ID、工作進程在工作進程表中的索引和必要的文件描述符等信息。
- 主進程與外界通過信號機制進行通信,當接收到需要處理的信號時,它通過管道向相關的工作進程發送 正確的指令,每個工作進程都有能力捕獲管道中的可讀事件,當管道中有可讀事件的時候,工作進程就 會從管道中讀取并解析指令,然后采取相應的執行動作,這樣就完成了主進程與工作進程的交互。
worker進程之間的通信原理基本上和主進程與worker進程之間的通信是一樣的,只要worker進程之間能夠
取得彼此的信息,建立管道即可通信,但是由于worker進程之間是完全隔離的,因此一個進程想要知道另外一
個進程的狀態信息,就只能通過主進程來實現。
為了實現worker進程之間的交互,master進程在生成worker進程之后,在worker進程表中進行遍歷,將該
新進程的PID以及針對該進程建立的管道句柄傳遞給worker進程中的其他進程,為worker進程之間的通信做
準備,當worker進程1向worker進程2發送指令的時候,首先在master進程給它的其他worker進程工作信息
中找到2的進程PID,然后將正確的指令寫入指向進程2的管道,worker進程2捕獲到管道中的事件后,解析指
令并進行相關操作,這樣就完成了worker進程之間的通信。
另worker進程可以通過共享內存來通訊的,比如upstream中的zone,或者limit_req、limit_conn中的
zone等。操作系統提供了共享內存機制
2.2.3Nginx 啟動和 HTTP 連接建立
- Nginx 啟動時,Master 進程,加載配置文件
- Master 進程,初始化監聽的 socket
- Master 進程,fork 出多個 Worker 進程
- Worker 進程,競爭新的連接,獲勝方通過三次握手,建立 Socket 連接,并處理請求
2.2.4HTTP 處理過程
2.3Nginx 模塊介紹
nginx 有多種模塊
- 核心模塊:是 Nginx 服務器正常運行必不可少的模塊,提供錯誤日志記錄 、配置文件解析 、事件 驅動機制 、進程管理等核心功能 、
- 標準HTTP模塊:提供 HTTP 協議解析相關的功能,比如: 端口配置 、 網頁編碼設置 、 HTTP響應 頭設置 等等 可選HTTP模塊:主要用于擴展標準的 HTTP 功能,讓 Nginx 能處理一些特殊的服務,比如: Flash
- 多媒體傳輸 、解析 GeoIP 請求、 網絡傳輸壓縮 、 安全協議 SSL 支持等
- 郵件服務模塊:主要用于支持 Nginx 的 郵件服務 ,包括對 POP3 協議、 IMAP 協議和 SMTP協議的 支持 Stream服務模塊: 實現反向代理功能,包括TCP協議代理
- 第三方模塊:是為了擴展 Nginx 服務器應用,完成開發者自定義功能,比如: Json 支持、 Lua 支 持等
nginx高度模塊化,但其模塊早期不支持DSO機制;1.9.11 版本支持動態裝載和卸載
模塊分類:
核心模塊:core module
標準模塊:HTTP 模塊: ngx_http_*HTTP Core modules #默認功能HTTP Optional modules #需編譯時指定Mail 模塊: ngx_mail_*Stream 模塊 ngx_stream
2.4 Nginx 安裝
2.4.1 Nginx版本和安裝方式
Nginx版本
- Mainline version 主要開發版本,一般為奇數版本號,比如1.19
- Stable version 當前最新穩定版,一般為偶數版本,如:1.20
- Legacy versions 舊的穩定版,一般為偶數版本,如:1.18
Nginx安裝可以使用yum或源碼安裝,但是推薦使用源碼編譯安裝
- yum的版本比較舊
- 編譯安裝可以更方便自定義相關路徑
- 使用源碼編譯可以自定義相關功能,更方便業務的上的使用
2.4.2.Nginx 編譯安裝
編譯器介紹
源碼安裝需要提前準備標準的編譯器,GCC的全稱是(GNU Compiler collection),其有GNU開發,并以GPL即LGPL許可,是自由的類UNIX即蘋果電腦Mac OS X操作系統的標準編譯器,因為GCC原本只能處理C語言,所以原名為GNU C語言編譯器,后來得到快速發展,可以處理C++,Fortran,pascal,objective
2.4.2.1 編譯安裝 Nginx
官方源碼包下載地址:https://nginx.org/en/download.html
編譯安裝示例:
[root@Nginx ~]# dnf install gcc pcre-devel zlib-devel openssl-devel -y[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 srcCHANGES 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 nginx-1.24.0]# ls /usr/local/nginx/
conf html logs sbinconf:保存nginx所有的配置文件,其中nginx.conf是nginx服務器的最核心最主要的配置文件,其他的.conf則是用來配置nginx相關的功能的,例如fastcgi功能使用的是fastcgi.conf和fastcgi_params兩個文件,配置文件一般都有一個樣板配置文件,是以.default為后綴,使用時可將其復制并將default后綴去掉即可。html目錄中保存了nginx服務器的web文件,但是可以更改為其他目錄保存web文件,另外還有一個50x的web文件是默認的錯誤頁面提示頁面。logs:用來保存nginx服務器的訪問日志錯誤日志等日志,logs目錄可以放在其他路徑,比如/var/logs/nginx里面。sbin:保存nginx二進制啟動腳本,可以接受不同的參數以實現不同的功能。
2.4.2.2 驗證版本及編譯參數
[root@Nginx ~]# vim ~/.bash_profile #系統環境變量
export PATH=$PATH:/usr/local/nginx/sbin[root@Nginx ~]# source ~/.bash_profile[root@nginx conf]# 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 --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
2.4.2.3使用安裝完成的二進制文件nginx
[root@Nginx ~]# nginx -v
nginx version: nginx/1.18.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit #顯示版本和編譯參數
-t : test configuration and exit #測試配置文件是否異
-T : test configuration, dump it and exit #測試并打印
-q : suppress non-error messages during configuration testing #靜默模式
-s signal : send signal to a master process: stop, quit, reopen, reload #發送信號,reload信號 會生成新的worker,但master不會重新生成
-p prefix : set prefix path (default: /etc/nginx/) #指定Nginx 目錄
-c filename : set configuration file (default: /etc/nginx/nginx.conf) #配置文件路徑
-g directives : set global directives out of configuration file #設置全局指令,注意和配置文件不要同時配置,否則沖突
2.6 平滑升級和回滾
有時候我們需要對Nginx版本進行升級以滿足對其功能的需求,例如添加新模塊,需要新功能,而此時Nginx又在跑著業務無法停掉,這時我們就可能選擇平滑升級
2.6.1 平滑升級流程
- 將舊Nginx二進制文件換成新Nginx程序文件(注意先備份)
- 向master進程發送USR2信號
- master進程修改pid文件名加上后綴.oldbin,成為nginx.pid.oldbin
- master進程用新Nginx文件啟動新master進程成為舊master的子進程,系統中將有新舊兩個Nginx主進程共同提供Web服務,當前新的請求仍然由舊Nginx的worker進程進行處理,將新生成的master進程的PID存放至新生成的pid文件nginx.pid
- 向舊的Nginx服務進程發送WINCH信號,使舊的Nginx worker進程平滑停止
- 向舊master進程發送QUIT信號,關閉老master,并刪除Nginx.pid.oldbin文件
- 如果發現升級有問題,可以回滾∶向老master發送HUP,向新master發送QUIT
2.6.2 平滑升級和回滾案例
#隱藏程序的版本信息保護服務器安全
[root@nginx ~]# cd nginx-1.24.0/
[root@nginx nginx-1.24.0]# cd src/
[root@nginx src]# cd core/
[root@nginx core]# vim nginx.h [root@Nginx nginx]# tar zxf nginx-1.26.1.tar.gz
[root@Nginx nginx]# cd nginx-1.26.1/#開始編譯新版本
[root@Nginx nginx-1.26.1]# ./configure --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只要make無需要make installroot@Nginx nginx-1.26.1]# make
#查看兩個版本
[root@Nginx nginx-1.26.1]# ll objs/nginx /usr/local/nginx/sbin/nginx
-rwxr-xr-x 1 root root 1239416 Jul 18 15:08 objs/nginx
-rwxr-xr-x 1 root root 5671488 Jul 18 11:41 /usr/local/nginx/sbin/nginx#把之前的舊版的nginx命令備份
[root@Nginx ~]# cd /usr/local/nginx/sbin/、
[root@Nginx sbin]# cp nginx nginx.24#把新版本的nginx命令復制過去
[root@Nginx sbin]# /bin/cp -f /root/nginx-1.26.1/objs/nginx /usr/local/nginx/sbin#檢測一下有沒有問題
[root@Nginx sbin]# 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@Nginx sbin]# kill -USR2 48732 #舊版本nginx master ID 激活新版本程序
#USR2 平滑升級可執行程序,將存儲有舊版本主進程PID的文件重命名為nginx.pid.oldbin,并啟動新的nginx
#此時兩個master的進程都在運行,只是舊的master不在監聽,由新的master監聽80
#此時Nginx開啟一個新的master進程,這個master進程會生成新的worker進程,這就是升級后的Nginx進程,此時老的進程不會自動退出,但是當接收到新的請求不作處理而是交給新的進程處理。[root@Nginx sbin]# ps aux | grep nginx
root 48732 0.0 0.1 9868 2436 ? Ss 14:17 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 48733 0.0 0.2 14200 4868 ? S 14:17 0:00 nginx: worker process
root 52075 0.0 0.3 9876 6528 ? S 15:41 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 52076 0.0 0.2 14208 4868 ? S 15:41 0:00 nginx: worker process[root@Nginx sbin]# curl -I localhost
HTTP/1.1 200 OKServer: nginx/1.24.0 ##依舊是舊版本生生效
Date: Thu, 18 Jul 2024 07:45:58 GMT
Content-Type: text/html
Content-Length: 615Last-Modified: Thu, 18 Jul 2024 03:41:13 GMT
Connection: keep-alive
ETag: "66988ed9-267"
Accept-Ranges: bytes#回收舊版本
[root@Nginx sbin]# kill -WINCH 48732 #回收舊版本的master
[root@Nginx sbin]# ps aux | grep nginx
root 48732 0.0 0.1 9868 2436 ? Ss 14:17 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 52075 0.0 0.3 9876 6528 ? S 15:41 0:00 nginx: master process /usr/local/nginx/sbin/nginxnobody 52076 0.0 0.2 14208 4868 ? S 15:41 0:00 nginx: worker process#檢測版本信息
[root@Nginx sbin]# curl -I localhost
HTTP/1.1 200 OKServer: nginx/1.26.1 #新版本生效
Date: Thu, 18 Jul 2024 07:59:45 GMT
Content-Type: text/html
Content-Length: 615Last-Modified: Thu, 18 Jul 2024 03:41:13 GMT
Connection: keep-alive
ETag: "66988ed9-267"
Accept-Ranges: bytes#回滾
#如果升級的版本發現問題需要回滾,可以重新拉起舊版本的worker
[root@Nginx sbin]# cp nginx nginx.26 #備份新版本
[root@Nginx sbin]# ls
nginx nginx.24 nginx.26
[root@Nginx sbin]# mv nginx.24 nginxmv: overwrite 'nginx'? y[root@Nginx sbin]# kill -HUP 48732
[root@Nginx sbin]# ps aux | grep nginx
root 48732 0.0 0.1 9868 2436 ? Ss 14:17 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 52075 0.0 0.3 9876 6528 ? S 15:41 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 52076 0.0 0.2 14208 5124 ? S 15:41 0:00 nginx: worker process
nobody 52130 0.0 0.2 14200 4868 ? S 16:30 0:00 nginx: worker process[root@Nginx sbin]# kill -WINCH 52075
[root@Nginx sbin]# ps aux | grep nginx
root 48732 0.0 0.1 9868 2436 ? Ss 14:17 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 52075 0.0 0.3 9876 6528 ? S 15:41 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 52130 0.0 0.2 14200 4868 ? S 16:30 0:00 nginx: worker process
root 52137 0.0 0.1 221664 2176 pts/0 S+ 16:31 0:00 grep --color=auto nginx[root@Nginx sbin]# curl -I localhost
HTTP/1.1 200 OKServer: nginx/1.24.0 ##版本回滾完成
Date: Thu, 18 Jul 2024 08:31:51 GMT
Content-Type: text/html
Content-Length: 615Last-Modified: Thu, 18 Jul 2024 03:41:13 GMT
Connection: keep-alive
ETag: "66988ed9-267"
Accept-Ranges: bytes
查看版本信息
回滾老版本
2.7編寫啟動腳本
systemd site:nginx.org
把該配置方到 /lib/ssytemd/ssytem/
[root@nginx sbin]# cd /lib/systemd/system
[root@nginx system]# vim 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=/usr/local/nginx/logs/nginx.pid #自定義目錄下的pid文件
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true[Install]
WantedBy=multi-user.target
[root@nginx system]# systemctl daemon-reload #重新加載配置文件[root@nginx logs]# systemctl enable --now nginx
[root@nginx logs]# systemctl status nginx
三、Nginx 核心配置詳解
3.1 配置文件說明
nginx 官方幫助文檔:http://nginx.org/en/docs/
Nginx的配置文件的組成部分:
- 主配置文件:nginx.conf
- 子配置文件: include conf.d/*.conf
- fastcgi, uwsgi,scgi 等協議相關的配置文件{LNMP–P(php,python,perl)}
- mime.types:支持的mime類型,MIME(Multipurpose Internet Mail Extensions)多用途互聯網郵件擴展類型,MIME消息能包含文本、圖像、音頻、視頻以及其他應用程序專用的數據,是設定某種擴展名的文件用一種應用程序來打開的方式類型,當該擴展名文件被訪問的時候,瀏覽器會自動
- 使用指定應用程序來打開。多用于指定一些客戶端自定義的文件名,以及一些媒體文件打開方式。
nginx 配置文件格式說明
配置文件由指令與指令塊構成
每條指令以;分號結尾,指令與值之間以空格符號分隔
可以將多條指令放在同一行,用分號分隔即可,但可讀性差,不推薦
指令塊以{ }大括號將多條指令組織在一起,且可以嵌套指令塊
include語句允許組合多個配置文件以提升可維護性
使用#符號添加注釋,提高可讀性
使用$符號使用變量
部分指令的參數支持正則表達式
Nginx 主配置文件的配置指令方式:
directive value [value2 ...];#參數 加值注意
(1) 指令必須以分號結尾
(2) 支持使用配置變量 內建變量:由Nginx模塊引入,可直接引用 自定義變量:由用戶使用set命令定義,格式: set variable_name value; 引用變量:$variable_name
主配置文件結構:四部分
main block:主配置段,即全局配置段,對http,mail都有效#事件驅動相關的配置
event {
...
} #http/https 協議相關配置段
http {...} #默認配置文件不包括下面兩個塊
#mail 協議相關配置段
mail {
...}#stream 服務器相關配置段
stream {
...}
默認的nginx.conf 配置文件格式說明
#全局配置端,對全局生效,主要設置nginx的啟動用戶/組,啟動的工作進程數量,工作模式,Nginx的PID路徑,日志路徑等。
user nginx; #user noboby #沒有指定的話,默認noboby
worker_processes 1; #啟動工作進程數數量 #auto 開啟自動匹配 不建議,建議手動綁定,減小開銷
events {
#events #設置快,主要影響nginx服務器與用戶的網絡連接,比如是否允許同時接受多個網絡連接,使用哪種事件驅動模型
#處理請求,每個工作進程可以同時支持的最大連接數,是否開啟對多工作進程下的網絡連接進行序列化等。worker_connections 1024; #設置單個nginx工作進程可以接受的最大并發,作為web服務器的時候最大并發數為 #worker_connections * worker_processes,作為反向代理的時候為 #(worker_connections * worker_processes)/2}http {
#http塊是Nginx服務器配置中的重要部分,緩存、代理和日志格式定義等絕大多數功能和第三方模塊都
#可以在這設置,http塊可以包含多個server塊,而一個server塊中又可以包含多個location塊,
#server塊可以配置文件引入、MIME-Type定義、日志自定義、是否啟用sendfile、連接超時時間和
#單個鏈接的請求上限等。 include mime.types; default_type application/octet-stream; sendfile on; #作為web服務器的時候打開sendfile加快靜態文件傳輸,指定是否使用 #sendfile系統調用來傳輸文件 #sendfile系統調用在兩個文件描述符之間直接傳遞數據(完全在內核中操作) #從而避免了數據在內核緩沖區和用戶緩沖區之間的拷貝,操作效率很高,被稱之為零拷貝, #硬盤 >> kernel buffer (快速拷貝到kernelsocket buffer) >>協議棧。 keepalive_timeout 65; #長連接超時時間,單位是秒 server { #設置一個虛擬機主機,可以包含自己的全局快,同時也可以包含多個location模塊 #比如本虛擬機監聽的端口、本虛擬機的名稱和IP配置,多個server 可以使用一個端口比如都使用 #80端口提供web服務 listen 80; #配置server監聽的端口server_name localhost; #本server的名稱,當訪問此名稱的時候nginx會調用當前serevr內部的配置進程匹配。 location / { #location其實是server的一個指令,為nginx服務器提供比較多而且靈活的指令 #都是在location中體現的,主要是基于nginx接受到的請求字符串 #對用戶請求的UIL進行匹配,并對特定的指令進行處理 #包括地址重定向、數據緩存和應答控制等功能都是在這部分實現 #另外很多第三方模塊的配置也是在location模塊中配置。 root html; #相當于默認頁面的目錄名稱,默認是安裝目錄的相對路徑,可以使用絕對路徑配置。 index index.html index.htm; #默認的頁面文件名稱 } error_page 500 502 503 504 /50x.html; #錯誤頁面的文件名稱 location = /50x.html { #location處理對應的不同錯誤碼的頁面定義到/50x.html #這個跟對應其server中定義的目錄下。 root html; #定義默認頁面所在的目錄 } }#和郵件相關的配置
#mail {
# ...#
#} mail 協議相關配置段#tcp代理配置,1.9版本以上支持
#stream {# ...# } stream 服務器相關配置段
#導入其他路徑的配置文件
#include /apps/nginx/conf.d/*.conf}
3.2 全局配置
Main 全局配置段常見的配置指令分類正常運行必備的配置
- 優化性能相關的配置
- 用于調試及定位問題相關的配置
- 事件驅動相關的配置
全局配置說明:
user nginx nginx; #啟動Nginx工作進程的用戶和組
worker_processes [number | auto]; #啟動Nginx工作進程的數量,一般設為和CPU核心數相同worker_cpu_affinity 00000001 00000010 00000100 00001000 | auto ;#將Nginx工作進程綁定到指定的CPU核心,默認Nginx是不進行進程綁定的,綁定并不是意味著當前nginx進程獨占以一核心CPU,但是可以保證此進程不運行在其他核心上,這就極大減少了nginx的工作進程在不同的cpu核心上的來回跳轉,減少了CPU對進程的資源分配與回收以及內存管理等,因此可以有效的提升nginx服務器的性能。CPU MASK: 00000001:0號CPU 00000010:1號CPU 10000000:7號CPU
#示例
worker_cpu_affinity 0001 0010 0100 1000;第0號---第3號
CPUworker_cpu_affinity 0101 1010; #示例
worker_processes 4;
worker_cpu_affinity 00000010 00001000 00100000 10000000;[root@centos8 ~]# ps axo pid,cmd,psr | grep nginx
31093 nginx: master process /apps 1
34474 nginx: worker process 1
34475 nginx: worker process 3
34476 nginx: worker process 5
34477 nginx: worker process 7#錯誤日志記錄配置,語法:error_log file [debug | info | notice | warn | error | crit | alert | emerg]#error_log logs/error.log;
#error_log logs/error.log notice;
error_log /usr/local/nginx/logs/error.log error; #pid文件保存路徑
pid /usr/local/nginx/logs/nginx.pid;
worker_priority 0; #工作進程優先級,-20~20(19)
worker_rlimit_nofile 100000; #所有worker進程能打開的文件數量上限, #包括:Nginx的所有連接(例如與代理服務器的連接等) #而不僅僅是與客戶端的連接 #另一個考慮因素是實際的并發連接數不能超過系統級別的最大打開文件數的限制 #最好與ulimit -n 或者limits.conf的值保持一致,#修改pam限制
[root@Nginx ~]# sudo -u nginx ulimit -n1024[root@Nginx ~]# vim /etc/security/limits.conf*
- nofile 100000
[root@Nginx ~]# sudo -u nginx ulimit -n100000daemon off; #前臺運行Nginx服務用于測試、docker等環境。
master_process off|on; #是否開啟Nginx的master-worker工作模式,僅用于開發調試場景,默認為onevents {worker_connections 100000; #設置單個工作進程的最大并發連接數
use epoll; #使用epoll事件驅動, #Nginx支持眾多的事件驅動, #比如:select、poll、epoll,只能設置在events模塊中設置accept_mutex on; #on為同一時刻一個請求輪流由work進程處理, #而防止被同時喚醒所有worker #避免多個睡眠進程被喚醒的設置,默認為off #新請求會喚醒所有worker進程,此過程也稱為"驚群"-會增加負載 #因此nginx剛安裝完以后要進行適當的優化。建議設置為on multi_accept on; #on時Nginx服務器的每個工作進程可以同時接受多個新的網絡連接 #此指令默認為off, #即默認為一個工作進程只能一次接受一個新的網絡連接 #打開后幾個同接受多個。建議設置為on 打開可以接受多個后一起發送 }
示例: 實現 nginx 的高并發配置----優化策略
[root@nginx conf]# ulimit -n
1024
#默認最多打開1024個
[root@nginx conf]#ulimit -n 100000
#ab壓縮-httpd-tools提供 用來測試訪問壓力
[root@nginx conf]# ab -n100000 -c5000 http://172.25.250.62/index.html
This is ApacheBench, Version 2.3 <$Revision: 1903618 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 172.25.250.62 (be patient)
socket: Too many open files (24)
#抗壓失敗#默認配置不支持高并發,并報錯
[root@nginx nginx]# tail logs/error.log
2025/08/09 22:19:17 [crit] 178300#0: accept4() failed (24: Too many open files)
2025/08/09 22:19:17 [crit] 178300#0: *167970 open() "/usr/local/nginx/html/index.html" failed (24: Too many open files), client: 172.25.250.62, server: localhost, request: "GET /index.html HTTP/1.0", host: "172.25.250.62"
2025/08/09 22:19:17 [crit] 178300#0: *167970 open() "/usr/local/nginx/html/50x.html" failed (24: Too many open files), client: 172.25.250.62, server: localhost, request: "GET /index.html HTTP/1.0", host: "172.25.250.62"
2025/08/09 22:19:18 [crit] 178300#0: accept4() failed (24: Too many open files)
2025/08/09 22:19:18 [crit] 178300#0: *180766 open() "/usr/local/nginx/html/index.html" failed (24: Too many open files), client: 172.25.250.62, server: localhost, request: "GET /index.html HTTP/1.0", host: "172.25.250.62"
2025/08/09 22:19:18 [crit] 178300#0: *180766 open() "/usr/local/nginx/html/50x.html" failed (24: Too many open files), client: 172.25.250.62, server: localhost, request: "GET /index.html HTTP/1.0", host: "172.25.250.62"
2025/08/09 22:19:19 [crit] 178300#0: accept4() failed (24: Too many open files)
2025/08/09 22:19:19 [crit] 178300#0: *198780 open() "/usr/local/nginx/html/index.html" failed (24: Too many open files), client: 172.25.250.62, server: localhost, request: "GET /index.html HTTP/1.0", host: "172.25.250.62"
2025/08/09 22:19:19 [crit] 178300#0: *198780 open() "/usr/local/nginx/html/50x.html" failed (24: Too many open files), client: 172.25.250.62, server: localhost, request: "GET /index.html HTTP/1.0", host: "172.25.250.62"
2025/08/09 22:19:29 [crit] 178300#0: accept4() failed (24: Too many open files)#修改配置
[root@Nginx ~]# vim /etc/security/limits.conf #需要在nginx服務啟動前開,若服務已經起,則需ulimit -n 100000 讓配置文件生效* - nproc 100000* - nofile 100000#*所有人 -軟限制硬限制為同一值 程序可打開文件個數 100000
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
worker_rlimit_nofile 100000;
[root@Nginx ~]# systemctl restart nginx
測試:
3.3 http 配置塊
#在響應報文中將指定的文件擴展名映射至MIME對應的類型
include /etc/nginx/mime.types;
default_type application/octet-stream; #除mime.types中的類型外 #指定其它文件的默認MIME類型,瀏覽器一般會提示下載types { text/html html; image/gif gif; image/jpeg jpg;}
示例:識別php文件為text/html
[root@nginx nginx]# vim html/index.php<?phpphpinfo();
?>[root@nginx nginx]# curl -I 172.25.250.62/index.php
HTTP/1.1 200 OK
Server: nginx/******
Date: Sat, 09 Aug 2025 14:25:35 GMT
Content-Type: application/octet-stream
Content-Length: 22
Last-Modified: Sat, 09 Aug 2025 14:25:05 GMT
Connection: keep-alive
ETag: "68975a41-16"
Accept-Ranges: bytes
3.4 核心配置示例
基于不同的IP、不同的端口以及不用得域名實現不同的虛擬主機,依賴于核心模塊ngx_http_core_module實現。
3.4.1 新建一個 PC web 站點
#定義子配置文件路徑
[root@Nginx ~]# mkdir /usr/local/nginx/conf.d/
[root@centos8 ~]# vim /usr/local/nginx/conf/nginx.conf
http { ......
include /usr/local/nginx/conf.d/*.conf; #在配置文件的最后面添加此行 #注意不要放在最前面,會導致前面的命令無法生效
}#創建虛擬主機網站配置
[root@Nginx ~]# vim /usr/local/nginx/conf.d/vhosts.conf
server{listen 80;server_name tomcat.gee.org;root /web/html;index index.html;
}root@Nginx ~]# mkdir -p /we/html
[root@Nginx ~]# echo web_html > /web/html/index.html[root@Nginx ~]# nginx -s reload#訪問測試
[root@nginx conf.d]# curl tomcat.gee.org
web_html
3.4.2 root 與 alias
root:指定web的家目錄,在定義location的時候,文件的絕對路徑等于 root+location
root示例:
server{listen 80;server_name tomcat.gee.org;root /web/html;index index.html;location /test {root /mnt/; #必須建立 /mnt/test 這個文件}}[root@nginx conf.d]# mkdir -p /mnt/test[root@nginx conf.d]# echo web_mnt > /mnt/test/index.html[root@nginx conf.d]# nginx -s reload\#重啟Nginx并訪問測試[root@nginx conf.d]# curl tomcat.gee.org/test/
web_mnt
alias:定義路徑別名,會把訪問的路徑重新定義到其指定的路徑,文檔映射的另一種機制;僅能用于location上下文,此指令使用較少
alias示例:
server{listen 80;server_name tomcat.gee.org;root /web/html;index index.html;location /test {root /mnt/; #/mnt/test文件 才是真實訪問的文件}location /test1 {alias /mnt/test1; #真實訪問的文件路徑}}location /test { #注意test后不要加/? \#使用alias的時候uri后面如果加了斜杠,則下面的路徑配置必須加斜杠,否則403 ? alias /mnt/test1; #當訪問test的時候,會顯示alias定義的/mnt/test1里面的內容 #重啟Nginx并訪問測試
[root@node100 ~]# curl lee.timinglee.org/alias/
dirtest page
location中使用root指令和alias指令的意義不同
root #給定的路徑對應于location中的/uri左側的/
alias #給定的路徑對應于location中的/uri的完整路徑
3.4.3 location 的詳細使用
- 在一個server中location配置段可存在多個,用于實現從uri到文件系統的路徑映射;
- ngnix會根據用戶請求的URI來檢查定義的所有location,按一定的優先級找出一個最佳匹配,
- 而后應用其配置在沒有使用正則表達式的時候,nginx會先在server中的多個location選取匹配度最高的一個uri
- uri是用戶請求的字符串,即域名后面的web文件路徑
- 然后使用該location模塊中的正則url和字符串,如果匹配成功就結束搜索,并使用此location處理此請求。
\
#語法規則:location [ = | ~ | ~* | ^~ ] uri { ... }= #用于標準uri前,需要請求字串與uri精確匹配,大小敏感,如果匹配成功就停止向下匹配并立即處理請求^~ #用于標準uri前,表示包含正則表達式,并且匹配以指定的正則表達式開頭 #對uri的最左邊部分做匹配檢查,不區分字符大小寫? ~ #用于標準uri前,表示包含正則表達式,并且區分大小寫~* #用于標準uri前,表示包含正則表達式,并且不區分大寫不帶符號 #匹配起始于此uri的所有的uri\ #用于標準uri前,表示包含正則表達式并且轉義字符。可以將 . * ?等轉義為普通符號\#匹配優先級從高到低:=, ^~, ~/~*, 不帶符號
3.4.3.1 匹配案例-精確匹配
在server部分使用location配置一個web界面,例如:當訪問nginx 服務器的/logo.jpg的時候要顯示指定html文件的內容,精確匹配一般用于匹配組織的logo等相對固定的URL,匹配優先級最高
1.精確匹配 logo
[root@Nginx ~]# mkdir /webdata/nginx/timinglee.org/lee/images -p[root@Nginx ~]# ls /webdata/nginx/timinglee.org/lee/images[root@Nginx ~]# vim /usr/local/nginx/conf.d/vhosts.confserver {listen 80;server_name lee.timinglee.org;? location / {? root /webdata/nginx/timinglee.org/lee/html; ? }? location = /logo.png {? root /webdata/nginx/timinglee.org/lee/images;}}\#上傳logo.jpg圖片到/webdata/nginx/timinglee.org/lee/images,重啟Nginx并訪問測試#訪問測試:http://www.timinglee.org/logo.png
3.4.3.2 匹配案例-區分大小寫
~ 實現區分大小寫的模糊匹配. 以下范例中,
如果訪問uri中包含大寫字母的logo.PNG,則以下location匹配logo.png條件不成功
因為 ~ 區分大小寫,當用戶的請求被執行匹配時發現location中定義的是小寫的png,
本次訪問的uri匹配失敗,后續要么繼續往下匹配其他的location(如果有),要么報錯給客戶端
server {listen 80;server_name lee.timinglee.org;? location / {? root /webdata/nginx/timinglee.org/lee/html;}? location ~ /logo.PNG {? root /webdata/nginx/timinglee.org/lee/images; }}\#重啟Nginx并訪問測試\#http://www.timinglee.org/logo.PNG #訪問失敗,系統中沒有logo.PNG文件
3.4.3.3 匹配案例-不區分大小寫
~* 用來對用戶請求的uri做模糊匹配,uri中無論都是大寫、都是小寫或者大小寫混合,此模式也都會匹配,通常使用此模式匹配用戶request中的靜態資源并繼續做下一步操作,此方式使用較多
注意: 此方式中,對于Linux文件系統上的文件仍然是區分大小寫的,如果磁盤文件不存在,仍會提示404
server {listen 80;server_name lee.timinglee.org;? location / {? root /webdata/nginx/timinglee.org/lee/html; }? location ~* /logo.PNG { ? root /webdata/nginx/timinglee.org/lee/images; ? }}\#重啟Nginx并訪問測試\#http://www.timinglee.org/logo.png
3.4.3.4 匹配案例-URI開始
[root@Nginx ~]# mkdir /webdata/nginx/timinglee.org/lee/images/images{1,2} [root@Nginx ~]# echo image1 > /webdata/nginx/timinglee.org/lee/images/images1/index.html[root@Nginx ~]# echo image1 > /webdata/nginx/timinglee.org/lee/images/images2/index.html? server {listen 80;server_name lee.timinglee.org;? location / {? root /webdata/nginx/timinglee.org/lee/html; ? }? location ^~ /images {? root /webdata/nginx/timinglee.org/lee/images; index index.html;}? location /images1 {? root /webdata/nginx/timinglee.org/lee/images; ? }}\#重啟Nginx并訪問測試,實現效果是訪問/images1和/images2返回內容一樣[root@node100 ~]# curl 172.25.254.200/images1/image1[root@node100 ~]# curl 172.25.254.200/images2/image1
3.4.3.5 匹配案例-文件名后綴
[root@Nginx ~]# mkdir /webdata/nginx/timinglee.org/lee/images#上傳一個圖片到/webdata/nginx/timinglee.org/lee/imagesserver {listen 80;server_name lee.timinglee.org;? location / {? root /webdata/nginx/timinglee.org/lee/html; ? }? location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js|css)$ { root /webdata/nginx/timinglee.org/lee/images;? index index.html;}}\#重啟Nginx并訪問測試172.25.254.200/logo.png
3.4.3.6 匹配案例-優先級
server{listen 80;server_name tomcat.gee.org;root /web/html;index index.html;location = /test {return 200 "punct =\n"; #精準匹配}location ^~ /test {return 200 "punct ^~\n"; #匹配以/test 開頭的}location ~* /test {return 200 "punct ~*\n"; }location ~ /test {return 200 "punct ~\n"; #匹配包含/test且區分大小寫}location ~* \.(png|css|jpg)$ {return 200 "punct ~*\n #匹配以png|css|jpg結尾的且不區分大小寫}
}\#匹配優先級:=, ^~, ~/~*,/location優先級:(location =) > (location ^~ 路徑) > (location ~,~* 正則順序) > (location 完整路徑) > (location 部分起始路徑) > (/)
3.4.3.7 生產使用案例
\
#直接匹配網站根會加速Nginx訪問處理location = /index.html {......;}location / {......;}\#靜態資源配置方法1location ^~ /static/ {......;}\#靜態資源配置方法2,應用較多location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ { ......;}\#多應用配置location ~* /app1 { ......;}location ~* /app2 { ......;}
3.4.4 Nginx 賬戶認證功能
由 ngx_http_auth_basic_module 模塊提供此功能
示例:
[root@nginx conf.d]# dnf install httpd-tools-2.4.57-5.el9.x86_64 -y
[root@nginx conf.d]# htpasswd -cmd /usr/local/nginx/.htpasswd admin
New password:
Re-type new password:
Adding password for user admin
[root@Nginx ~]# htpasswd -cmb /usr/local/nginx/conf/.htpasswd admin lee #-b 表示非交互建立用戶認證 -c 創建 -m 用戶 Adding password for user admin#[root@Nginx ~]# htpasswd -mb /usr/local/nginx/conf/.htpasswd lee lee#Adding password for usr
[root@nginx conf.d]# cat /usr/local/nginx/.htpasswd
admin:cNhlBY32jOUHw[root@Nginx ~]# vim vhost.conf
server{listen 80;root /web/html;index index.html;
# location = /test {
# return 200 "punct =\n";
# }
#
# location ^~ /test {
# return 200 "punct ^~\n";
# }
#location /login/{root /web/;index index.html;auth_basic "password please";auth_basic_user_file /usr/local/nginx/.htpasswd; }
}\#重啟Nginx并訪問測試
[root@nginx conf.d]# curl tomcat.gee.org/login/ -u admin:123
login
3.4.5 自定義錯誤頁面
自定義錯誤頁,同時也可以用指定的響應狀態碼進行響應, 可用位置:http, server, location, if in location
error_page code ... [=[response]] uri;
示例:
listen 80;server_name www.timinglee.org;error_page 500 502 503 504 /error.html; #后面跟指定文件
location = /error.html {root /data/nginx/html;}\#重啟nginx并訪問不存在的頁面進行測試
示例:自定義錯誤頁面
[root@Nginx ~]# mkdir /web/errorpage[root@Nginx ~]# echo error page > /web/errorpage/error.htmlserver{listen 80;server_name tomcat.gee.org;root /web/html;index index.html;error_page 500 502 404 403 /errorpage/error.html; location /errorpage{root /web/;}
}
~測試:[root@nginx conf.d]# curl tomcat.gee.org/llll
error page
3.4.6 自定義錯誤日志
可以自定義錯誤日志,實現站日志獨立
Syntax: error_log file [level];Default:error_log logs/error.log error;Context: main, http, mail, stream, server, locationlevel: debug, info, notice, warn, error, crit, alert, emerg
示例:
[root@nginx ~]# cd /usr/local/nginx/conf.d/
[root@nginx conf.d]# vi vhost.confserver{listen 80;server_name tomcat.gee.org;root /web/html;index index.html;error_page 500 502 404 403 /errorpage/error.html;access_log /usr/local/nginx/gee.access.log;error_log /usr/local/nginx/gee.error.loglocation /errorpage{root /web/;}
}\#重啟nginx并訪問不存在的頁面進行測試并驗證是在指定目錄生成新的日志文件
3.4.7 檢測文件是否存在
try_files會按順序檢查文件是否存在,返回第一個找到的文件或文件夾(結尾加斜線表示為文件夾),如果所有文件或文件夾都找不到,會進行一個內部重定向到最后一個參數。只有最后一個參數可以引起一個內部重定向,之前的參數只設置內部URI的指向。最后一個參數是回退URI且必須存在,否則會出現內部500錯誤。
語法格式
Syntax: try_files file ... uri;try_files file ... =code;Default: —Context: server, location
示例: 如果不存在頁面, 就轉到default.html頁面
[root@Nginx ~]# echo default> /web/errorpage/default.html[root@Nginx ~]# vim /usr/local/nginx/conf.d/vhost.confserver{listen 80;server_name tomcat.gee.org;root /web/html;index index.html;error_page 500 502 404 403 /errorpage/error.html;access_log /usr/local/nginx/gee.access.log;error_log /usr/local/nginx/gee.error.log;try_files $uri $uri.html $uri/index.html /errorpage/default.html;#會遍歷該行所有uri當訪問文件都不存在時,會訪問/error/default.html? location = /40x.html {? root /webdata/nginx/timinglee/lee/errors; ? }}測試:
[root@nginx conf.d]# curl tomcat.gee.org/a/
default
3.4.8 長連接配置
一般配置在主配置文件下
keepalive_timeout timeout [header_timeout]; #設定保持連接超時時長,0表示禁止長連接,默認為75s? \#通常配置在http字段作為站點全局配置keepalive_requests 數字; #在一次長連接上所允許請求的資源的最大數量 #默認為100次,建議適當調大,比如:500
示例:
keepalive_requests 3;
keepalive_timeout 65 60;
#開啟長連接后,返回客戶端的會話保持時間為60s,單次長連接累計請求達到指定次數請求或65秒就會被斷
開,第二個數字60為發送給客戶端應答報文頭部中顯示的超時時間設置為60s:如不設置客戶端將不顯示超時時間。
Keep-Alive:timeout=60 #瀏覽器收到的服務器返回的報文
? #如果設置為0表示關閉會話保持功能,將如下顯示:
? #Connection:close 瀏覽器收到的服務器返回的報文
#使用命令測試:
[root@node100 ~]# telnet lee.timinglee.org 80Trying 172.25.254.200...Connected to lee.timinglee.org.Escape character is '^]'.GET / HTTP/1.1 ##輸入動作HOST: lee.timinglee.org ##輸入訪問HOST? \##輸入回車HTTP/1.1 200 OKServer: nginx/1.24.0Date: Sat, 20 Jul 2024 12:54:16 GMTContent-Type: text/htmlContent-Length: 15Last-Modified: Sat, 20 Jul 2024 08:49:12 GMTConnection: keep-aliveETag: "669b7a08-f"Accept-Ranges: bytes172.25.254.200GET / HTTP/1.1 #第二次操作HOST: lee.timinglee.org #第二次操作 ? #第二次操作HTTP/1.1 200 OKServer: nginx/1.24.0Date: Sat, 20 Jul 2024 12:54:25 GMTContent-Type: text/htmlContent-Length: 15Last-Modified: Sat, 20 Jul 2024 08:49:12 GMTConnection: closeETag: "669b7a08-f"Accept-Ranges: bytes172.25.254.200Connection closed by foreign host. #自動斷開鏈接
3.4.9 作為下載服務器配置
ngx_http_autoindex_module 模塊處理以斜杠字符 “/” 結尾的請求,并生成目錄列表,可以做為下載服務配置使用
相關指令:
autoindex on | off; #自動文件索引功能,默為offautoindex_exact_size on | off; #計算文件確切大小(單位bytes),off 顯示大概大小(單位K、M),默認onautoindex_localtime on | off ; #顯示本機時間而非GMT(格林威治)時間,默認offautoindex_format html | xml | json | jsonp; #顯示索引的頁面文件風格,默認htmllimit_rate rate; #限制響應客戶端傳輸速率(除GET和HEAD以外的所有方法),單位B/s,bytes/second, #默認值0,表示無限制,此指令由ngx_http_core_module提供set $limit_rate 4k; #也可以通變量限速,單位B/s,同時設置,此項優級高.
示例:實現下載站點
\
#注意:download不需要index.html文件[root@Nginx ~]# mkdir /web/download #建立下載目錄[root@Nginx ~]# vim /usr/local/nginx/conf.d/vhosts.confserver {isten 80;server_name tomcat.gee.org;? root /webdata/nginx/timinglee.org/lee;access_log /var/log/nginx/access.log;error_log /var/log/nginx/error.log;? try_files $uri $uri.html $uri/index.html /error/default.html;? location = /40x.html {? root /webdata/nginx/timinglee/lee/errors; ? } ? location /download {root /web/;
? autoindex on; #自動索引功能? autoindex_exact_size on; #計算文件確切大小(單位bytes),此為默認值,off只顯示大概大小(單位kb、mb、gb)? autoindex_localtime on; #on表示顯示本機時間而非GMT(格林威治)時間,默為為off顯示GMT時間? set $limit_rate 1024k; #限速,默認不限速 }}\#重啟Nginx并訪問測試下載頁面