nginx
- 1.nginx簡介
- 2.nginx的特性與優點
- 2.1 nginx的特性
- 2.2 nginx的優點
- 3.nginx的功能及應用類別
- 3.1 nginx的基本功能
- 3.2 nginx的擴展功能
- 3.3 nginx的應用類別
- 4.nginx的模塊與作業原理
- 4.1 nginx的模塊分類
- 4.2 nginx的作業原理
- 4.3 web服務器請求資源的程序
- 5.nginx的安裝與配置
- 5.1 nginx的安裝
- 5.2 nginx安裝后配置
- 6 nginx的組態檔詳解
- 6.1 nginx.conf配置詳解
- 6.2 用于除錯、定位問題的配置引數
- 6.3 正常運行必備的配置引數
- 6.4 優化性能的配置引數
- 6.5 事件相關的配置:event{}段中的配置引數
- 6.6 網路連接相關的配置引數
- 6.7 fastcgi的相關配置引數
- 6.8 常需要進行調整的引數
- 6.9 nginx作為web服務器時使用的配置:http{}段的配置引數
1.nginx簡介
Nginx (engine x) 是一個高性能的web和反向代理服務器,同時也提供了IMAP/POP3/SMTP服務,Nginx是由伊戈爾·賽索耶夫為俄羅斯訪問量第二的Rambler.ru站點(俄文:Рамблер)開發的,第一個公開版本0.1.0發布于2004年10月4日,
其將源代碼以類BSD許可證的形式發布,因它的穩定性、豐富的功能集、簡單的組態檔和低系統資源的消耗而聞名,2011年6月1日,nginx 1.0.4發布,
Nginx是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,在BSD-like 協議下發行,其特點是占有記憶體少,并發能力強,事實上nginx的并發能力在同型別的網頁服務器中表現較好,中國大陸使用nginx網站用戶有:百度、京東、新浪、網易、騰訊、淘寶等,
2.nginx的特性與優點
2.1 nginx的特性
nginx是一個高性能Web和反向代理服務器,它具有很多非常優越的特性:
- 在高連接并發的情況下,nginx是Apache服務器不錯的替代品,能夠支持高達50000個并發連接數的回應
- 使用epoll and kqueue作為開發模型
- nginx作為負載均衡服務器:nginx既可在內部直接支持和PHP程式對外進行服務,也可支持作為HTTP代理服務器對外進行服務
- nginx采用C進行撰寫,不論系統資源開銷還是CPU使用效率都比Perlbal要好很多
2.2 nginx的優點
- 高并發連接:官方測驗能夠支撐5萬并發連接,在實際生產環境中跑到2-3萬并發連接數
- 記憶體消耗少:在3萬并發連接下,開啟的10個nginx行程才消耗150M記憶體(15M*10=150M)
- 組態檔非常簡單:風格跟程式一樣通俗易懂
- 成本低廉:nginx為開源軟體,可以免費使用,而購買F5 BIG-IP、NetScaler等硬體負載均衡交換機則需要十多萬至幾十萬人民幣
- 支持Rewrite重寫規則:能夠根據域名、URL的不同,將HTTP請求分到不同的后端服務器群組
- 內置的健康檢查功能:如果Nginx Proxy后端的某臺Web服務器宕機了,不會影響前端訪問
- 節省帶寬:支持GZIP壓縮,可以添加瀏覽器本地快取的Header頭
- 穩定性高:用于反向代理,宕機的概率微乎其微
- 模塊化設計:模塊可以動態編譯
- 外圍支持好:檔案全,二次開發和模塊較多
- 支持熱部署:可以不停機多載組態檔
- 支持事件驅動、AIO(AsyncIO,異步IO)、mmap(Memory Map,記憶體映射)等性能優化
3.nginx的功能及應用類別
3.1 nginx的基本功能
- 靜態資源的web服務器,能快取打開的檔案描述符
- http、smtp、pop3協議的反向代理服務器
- 快取加速、負載均衡
- 支持FastCGI(fpm,LNMP),uWSGI(Python)等
- 模塊化(非DSO機制),過濾器zip、SSI及影像的大小調整
- 支持SSL
3.2 nginx的擴展功能
- 基于名稱和IP的虛擬主機
- 支持keepalive
- 支持平滑升級
- 定制訪問日志、支持使用日志緩沖區提高日志存盤性能
- 支持URL重寫
- 支持路徑別名
- 支持基于IP及用戶的訪問控制
- 支持速率限制,支持并發數限制
3.3 nginx的應用類別
- 使用nginx結合FastCGI運行PHP、JSP、Perl等程式
- 使用nginx作反向代理、負載均衡、規則過濾
- 使用nginx運行靜態HTML網頁、圖片
- nginx與其他新技術的結合應用
4.nginx的模塊與作業原理
nginx由內核和模塊組成,其中,內核的設計非常微小和簡潔,完成的作業也非常簡單,僅僅通過查找組態檔將客戶端請求映射到一個location block(location是nginx配置中的一個指令,用于URL匹配),而在這個location中所配置的每個指令將會啟動不同的模塊去完成相應的作業,
-
URI:Uniform Resource Indentifier,統一資源識別符號,用于定義全域范圍內(包括但不僅限于互聯網)去標識唯一的、定位一種資源訪問路徑的方式,或者命名方式,被稱作統一資源識別符號,這里的統一指的是路徑格式上的統一,
-
URL:Uniform Resource Location,統一資源定位符,是URI的一個子集,用于描述在互聯網上互聯網資源的統一表示格式(protocol://host:port/path/to/file)
-
PV:Page View,打開了多少頁面
-
UV:User View,獨立IP量(UV的數量多說明訪問的人多)
4.1 nginx的模塊分類
nginx的模塊從結構上分為核心模塊、基礎模塊和第三方模塊
- HTTP模塊、EVENT模塊和MAIL模塊等屬于核心模塊
- HTTP Access模塊、HTTP FastCGI模塊、HTTP Proxy模塊和HTTP Rewrite模塊屬于基礎模塊
- HTTP Upstream模塊、Request Hash模塊、Notice模塊和HTTP Access Key模塊屬于第三方模塊
用戶根據自己的需要開發的模塊都屬于第三方模塊,正是有了如此多模塊的支撐,nginx的功能才會如此強大
nginx模塊從功能上分為處理器模塊、過濾器模塊和代理器模塊
- Handlers(處理器模塊),此類模塊直接處理請求,并進行輸出內容和修改headers資訊等操作,handlers處理器模塊一般只能有一個
- Filters(過濾器模塊),此類模塊主要對其他處理器模塊輸出的內容進行修改操作,最后由nginx輸出
- Proxies(代理器模塊),就是nginx的HTTP Upstream之類的模塊,這些模塊主要與后端一些服務比如fastcgi等操作互動,實作服務代理和負載均衡等功能
nginx模塊分為:核心模塊、事件模塊、標準Http模塊、可選Http模塊、郵件模塊、第三方模塊和補丁等
- nginx基本模塊:所謂基本模塊,指的是nginx默認的功能模塊,它們提供的指令,允許你使用定義nginx基本功能的變數,在編譯時不能被禁用,包括:
- 核心模塊:基本功能和指令,如行程管理和安全,常見的核心模塊指令,大部分是放置在組態檔的頂部
- 事件模塊:在Nginx內配置網路使用的能力,常見的events(事件)模塊指令,大部分是放置在組態檔的頂部
- 配置模塊:提供包含機制
4.2 nginx的作業原理
nginx的模塊直接被編譯進nginx,因此屬于靜態編譯方式,
啟動nginx后,nginx的模塊被自動加載,與Apache不一樣,首先將模塊編譯為一個so檔案,然后在組態檔中指定是否進行加載,
在決議組態檔時,nginx的每個模塊都有可能去處理某個請求,但是同一個處理請求只能由一個模塊來完成,
Nginx默認采用多行程作業方式,Nginx啟動后,會運行一個master行程和多個worker行程,其中master充當整個行程組與用戶的互動介面,同時對行程進行監護,管理worker行程來實作重啟服務、平滑升級、更換日志檔案、組態檔實時生效等功能,worker用來處理基本的網路事件,worker之間是平等的,他們共同競爭來處理來自客戶端的請求,
nginx的行程架構:
啟動nginx時,會啟動一個Master行程,這個行程不處理任何客戶端的請求,主要用來產生worker執行緒,一個worker執行緒用來處理n個request,

web sever:web服務器的集群
application server : 應用服務器的集群
mencached: 快取服務器的集群
backend: 后端
advaced I/O: 同步
sendfile,AIO 異步
作業方式:
在作業方式上,Nginx分為單作業行程和多作業行程兩種模式,在單作業行程模式下,除主行程外,還有一個作業行程,作業行程是單執行緒的;在多作業行程模式下,每個作業行程包含多個執行緒,Nginx默認為單作業行程模式,
Nginx在啟動后,會有一個master行程和多個worker行程,
下圖展示了nginx模塊一次常規的HTTP請求和回應的程序

4.3 web服務器請求資源的程序

首先我們客戶端發送一個請求到Web服務器,請求首先是到網卡,
網卡將請求交由內核空間的內核處理,其實就是拆包了,發現請求的是80埠,
內核便將請求發給了在用戶空間的Web服務器,Web服務器解包發現客戶端
請求的index.html頁面、
Web服務器便進行系統呼叫將請求發給內核
內核發現在請求的是一頁面,便呼叫磁盤的驅動程式,連接磁盤
內核通過驅動呼叫磁盤取得的頁面檔案
內核將取得的頁面檔案保存在自己的快取區域中便通知Web行程或執行緒來取相應的頁面檔案
Web服務器通過系統呼叫將內核快取中的頁面檔案復制到行程快取區域中
Web服務器取得頁面檔案來回應用戶,再次通過系統呼叫將頁面檔案發給內核
內核行程頁面檔案的封裝并通過網卡發送出去,當報文到達網卡時通過網路回應給客戶端
5.nginx的安裝與配置
5.1 nginx的安裝
//創建用戶
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
//安裝依賴環境
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget
[root@localhost ~]# yum -y groups mark install 'Development Tools'
//創建日志存放目錄
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx: /var/log/nginx
//下載nginx
[root@localhost ~]# wget http://nginx.org/download/nginx-1.21.3.tar.gz
--2021-10-25 23:25:56-- http://nginx.org/download/nginx-1.21.3.tar.gz
正在決議主機 nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5704::6, ...
正在連接 nginx.org (nginx.org)|52.58.199.22|:80... 已連接,
已發出 HTTP 請求,正在等待回應... 200 OK
長度:1066609 (1.0M) [application/octet-stream]
正在保存至: “nginx-1.21.3.tar.gz”
nginx-1.21.3.tar.gz 100%[=====================================>] 1.02M 28.3KB/s 用時 32s
2021-10-25 23:26:30 (32.7 KB/s) - 已保存 “nginx-1.21.3.tar.gz” [1066609/1066609])
//編譯安裝
[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.21.3.tar.gz
[root@localhost ~]# tar xf nginx-1.21.3.tar.gz
[root@localhost ~]# cd nginx-1.21.3/
[root@localhost nginx-1.21.3]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@localhost nginx-1.21.3]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
5.2 nginx安裝后配置
//配置環境變數
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost ~]# . /etc/profile.d/nginx.sh
[root@localhost ~]# which nginx //成功找到nginx
/usr/local/nginx/sbin/nginx
//服務控制方式,使用nginx命令
-t //檢查組態檔語法
-v //輸出nginx的版本
-c //指定組態檔的路徑
-s //發送服務控制信號,可選值有{stop|quit|reopen|reload},一般只用到stop和reload
-V //顯示nginx服務器的版本號和nginx服務器的編譯情況
-h //列印二進制檔案nginx的用法
-t -q //一起使用,如果組態檔無錯誤,將無任何輸出
-p //用來改變nginx的安裝路徑,常用在平滑升級Nginx服務器的場合
-g //用來補充nginx組態檔,向nginx服務指定啟動時應用于全域的配置
//啟動nginx,無需-s指定
[root@localhost ~]# nginx
[root@localhost ~]# ss -anltu
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
tcp LISTEN 0 128 [::]:22 [::]:*
//-v輸出nginx的版本
[root@localhost ~]# nginx -v
nginx version: nginx/1.21.3
//-V輸出nginx的版本和編譯安裝nginx的命令,可以查看安裝了哪些模塊
[root@localhost ~]# nginx -V
nginx version: nginx/1.21.3
built by gcc 8.4.1 20200928 (Red Hat 8.4.1-1) (GCC)
built with OpenSSL 1.1.1g FIPS 21 Apr 2020
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
//-h列出幫助檔案
[root@localhost ~]# nginx -h
nginx version: nginx/1.21.3
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
[-e filename] [-c filename] [-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
-p prefix : set prefix path (default: /usr/local/nginx/)
-e filename : set error log file (default: /var/log/nginx/error.log)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
//-t檢查安裝目錄下/conf/nginx.conf檔案語法
[root@localhost ~]# 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@localhost ~]# cp /usr/local/nginx/conf/nginx.conf /opt/
[root@localhost ~]# cp /usr/local/nginx/conf/mime.types /opt/
[root@localhost ~]# ls /opt/
mime.types nginx.conf //修改拷貝到其他位置的組態檔nginx.conf
[root@localhost ~]# nginx -s stop;nginx -c /opt/nginx.conf //停止,然后馬上啟動,-c指定修改的組態檔nginx.conf
[root@localhost ~]# ss -anltu
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
tcp LISTEN 0 128 [::]:22 [::]:*
//如果組態檔修改有誤,取消-c,使用默認的組態檔
[root@localhost ~]# nginx -s stop;nginx
6 nginx的組態檔詳解
主組態檔:安裝目錄/conf/nginx.conf
- 默認啟動nginx時,使用的組態檔是:安裝路徑/conf/nginx.conf檔案
- 可以在啟動nginx時通過-c選項來指定要讀取的組態檔
nginx常見的組態檔及其作用
| 組態檔 | 作用 |
|---|---|
| nginx.conf | nginx的基本組態檔 |
| mime.types | MIME型別關聯的擴展檔案 |
| fastcgi.conf | 與fastcgi相關的配置 |
| proxy.conf | 與proxy相關的配置 |
| sites.conf | 配置nginx提供的網站,包括虛擬主機 |
6.1 nginx.conf配置詳解
nginx.conf的內容分為以下幾段:
- main配置段:全域配置段,其中main配置段中可能包含event配置段
- event {}:定義event模型作業特性
- http {}:定義http協議相關的配置
配置指令:要以分號結尾,語法格式如下:
derective value1 [value2 ...];
支持使用變數:
- 內置變數:模塊會提供內建變數定義
- 自定義變數:
set var_name value
6.2 用于除錯、定位問題的配置引數
daemon {on|off}; //是否以守護行程方式運行nginx,除錯時應設定為off
master_process {on|off}; //是否以master/worker模型來運行nginx,除錯時可以設定為off
error_log 位置 級別; //配置錯誤日志
error_log里的位置和級別能有以下可選項:
| 位置 | 級別(遞增) |
|---|---|
| file stderr syslog:server=address[,parameter=value] memory:size | debug:若要使用debug級別,需要在編譯nginx時使用–with-debug選項 info notice warn error crit alert emerg |
級別越低記錄的越詳細
位置一般用的是file檔案
級別一般默認的是error
6.3 正常運行必備的配置引數
user USERNAME [GROUPNAME]; //指定運行worker行程的用戶和組
pid /path/to/pid_file; //指定nginx守護行程的pid檔案
worker_rlimit_nofile number; //設定所有worker行程最大可以打開的檔案數,默認為1024
worker_rlimit_core size; //指明所有worker行程所能夠使用的總體的最大核心檔案大小,保持默認即可
引數user和group(可選)
//nginx安裝過后組態檔中的user默認是nobody,但是組態檔中user引數是注釋掉的,即使我們把注釋去掉,默認user也不是nobody,是nginx
[root@localhost ~]# head -3 /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 1;
[root@localhost ~]# ps -ef |grep nginx
root 43320 1 0 08:23 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 43321 43320 0 08:23 ? 00:00:00 nginx: worker process
root 43407 1434 0 08:23 pts/0 00:00:00 grep --color=auto nginx
引數worker_rlimit_nofile number
Linux是有檔案句柄限制的(open files),一般默認是1024,當超過這個數量便會報 Too many open files錯誤,修改nginx引數worker_rlimit_nofile number時,同時也需要修改系統的open file,使用命令ulimit -a可以查看句柄數
[root@localhost ~]# ulimit -a
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 3026
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024 //句柄數
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 3026
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
//修改nginx組態檔worker_rlimit_nofile number為65535
[root@localhost ~]# head -5 /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 1;
worker_rlimit_nofile 65535;
[root@localhost ~]# nginx -s stop;nginx
//修改系統允許打開的檔案數,修改系統組態檔
[root@localhost ~]# tail -3 /etc/security/limits.conf
# End of file //最后添加下面兩行
* soft nofile 65535
* hard nofile 65535
#重啟或重新打開終端
6.4 優化性能的配置引數
worker_processes n; //啟動n個worker行程,這里的n為了避免背景關系切換,通常設定為cpu總核心數-1或等于總核心數
worker_cpu_affinity cpumask ...; //將行程系結到某cpu中,避免頻繁重繪快取
//cpumask:使用8位二進制表示cpu核心,如:
0000 0001 //第一顆cpu核心
0000 0010 //第二顆cpu核心
0000 0100 //第三顆cpu核心
0000 1000 //第四顆cpu核心
0001 0000 //第五顆cpu核心
0010 0000 //第六顆cpu核心
0100 0000 //第七顆cpu核心
1000 0000 //第八顆cpu核心
timer_resolution interval; //計時器決議度,降低此值,可減少gettimeofday()系統呼叫的次數
worker_priority number; //指明worker行程的nice值
- 引數
worker_processes n和worker_cpu_affinity cpumask
//查看CPU核數
top - 08:47:49 up 36 min, 2 users, load average: 0.06, 0.07, 0.07
Tasks: 203 total, 1 running, 202 sleeping, 0 stopped, 0 zombie
%Cpu0 : 0.7 us, 1.3 sy, 0.0 ni, 97.7 id, 0.0 wa, 0.0 hi, 0.3 si, 0.0 st
%Cpu1 : 0.3 us, 2.0 sy, 0.0 ni, 97.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 791.5 total, 115.1 free, 271.1 used, 405.3 buff/cache
MiB Swap: 2048.0 total, 2040.7 free, 7.2 used. 383.2 avail Mem
//配置引數
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# head -4 /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 2;
worker_cpu_affinity 0001 0010;
[root@localhost ~]# nginx -s stop;nginx
使用top命令查看nginx運行
輸入top命令之后按L搜索nginx,然后再按f

按f之后,空格開啟下面這個功能,然后q退出

此時可以查看到nginx行程對應一個核心

- 引數
worker_priority
安裝后默認優先級是0,可以再組態檔中修改優先級(優先級越高獲得更多的CPU運行時間,更優先獲得CPU運行的機會)
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# head -6 /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 2;
worker_cpu_affinity 0001 0010;
worker_rlimit_nofile 65535;
worker_priority -20; //修改優先級為-20
[root@localhost ~]# nginx -s stop;nginx
#優先級有-20到19,分別對應100到139

6.5 事件相關的配置:event{}段中的配置引數
accept_mutex {off|on}; //master調度用戶請求至各worker行程時使用的負載均衡鎖;on表示能讓多個worker輪流地、序列化地去回應新請求
lock_file file; //accept_mutex用到的互斥鎖鎖檔案路徑
use [epoll | rtsig | select | poll]; //指明使用的事件模型,建議讓nginx自行選擇
worker_connections #; //每個行程能夠接受的最大連接數
一個行程最大并發量最大為50000,但是實際配置時一般設為30000
并發量=(worker_processes*worker_connections)/2
配置并發量為30000
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# head -16 /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 2;
worker_cpu_affinity 0001 0010;
worker_rlimit_nofile 65535;
worker_priority -20;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 30000;
}
[root@localhost ~]# nginx -s stop;nginx
6.6 網路連接相關的配置引數
keepalive_timeout number; //長連接的超時時長,默認為65s,超過設定時間無回應就斷開
keepalive_requests number; //在一個長連接上所能夠允許請求的最大資源數
keepalive_disable [msie6|safari|none]; //為指定型別的UserAgent禁用長連接
tcp_nodelay on|off; //是否對長連接使用TCP_NODELAY選項,為了提升用戶體驗,通常設為on
client_header_timeout number; //讀取http請求報文首部的超時時長
client_body_timeout number; //讀取http請求報文body部分的超時時長
send_timeout number; //發送回應報文的超時時長
6.7 fastcgi的相關配置引數
LNMP:php要啟用fpm模型
配置示例如下:
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;
}
6.8 常需要進行調整的引數
- worker_processes
- worker_connections
- worker_cpu_affinity
- worker_priority
6.9 nginx作為web服務器時使用的配置:http{}段的配置引數
http{…}:配置http相關,由ngx_http_core_module模塊引入,nginx的HTTP配置主要包括四個區塊,結構如下:
http {//協議級別
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
gzip on;
upstream {//負載均衡配置
...
}
server {//服務器級別,每個server類似于httpd中的一個<VirtualHost>
listen 80;
server_name localhost;
location / {//請求級別,類似于httpd中的<Location>,用于定義URL與本地檔案系統的映射關系
root html;
index index.html index.htm;
}
}
}
http{}段配置指令:
- server {}:定義一個虛擬主機,示例如下:
server {
listen 81;
server_name test.example.com;
location / {
root html/test;
index index.html;
}
}
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# mkdir test
[root@localhost html]# echo 'test' > test/index.html
[root@localhost html]# nginx -s stop;nginx
[root@localhost html]# ss -anltu
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
tcp LISTEN 0 128 0.0.0.0:81 0.0.0.0:*
tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
tcp LISTEN 0 128 [::]:22 [::]:*

listen:指定監聽的地址和埠
listen address[:port];
listen port;
server_name NAME [...]; 后面可跟多個主機,名稱可使用正則運算式或通配符
當有多個server時,匹配順序如下:
- 1.先做精確匹配檢查
- 2.左側通配符匹配檢查,如*.idfsoft.com
- 3.右側通配符匹配檢查,如mail.*
- 4.正則運算式匹配檢查,如~ ^.*.idfsoft.com$
- 5.default_server
root path; 設定資源路徑映射,用于指明請求的URL所對應的資源所在的檔案系統上的起始路徑
alias path; 用于location配置段,定義路徑別名
[root@localhost ~]# mkdir -p /var/www/html
[root@localhost ~]# cd /var/www/html
[root@localhost html]# mv /usr/local/nginx/html/test/ .
[root@localhost html]# ls
test
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 81;
server_name test.example.com;
location / {
alias /var/www/html/; //寫絕對路徑
index index.html index.htm;
}
}
[root@localhost ~]# nginx -s stop;nginx
index file; 默認主頁面
index index.php index.html;
- 錯誤頁面配置
error_page code [...] [=code] URI | @name 根據http回應狀態碼來指定特用的錯誤頁面,例如 error_page 404 /404_customed.html
[=code]:以指定的回應碼進行回應,而不是默認的原來的回應,默認表示以新資源的回應碼為其回應碼,例如 error_page 404 =200 /404_customed.html
定義之前訪問不存在的資源

[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
#取消下面一行的注釋,當訪問的資源不存在,狀態碼為404時,用安裝目錄下html/404.html檔案來回應
49 error_page 404 /404.html;
[root@localhost ~]# cd /usr/local/nginx/html/ //在html目錄下定義404.html檔案
[root@localhost html]# ls
404.html 50x.html '第十二章 http協議_忘情OK_51CTO博客_files' index.html
//重啟nginx
[root@localhost html]# nginx -s stop;nginx
[root@localhost html]# ss -anltu
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
tcp LISTEN 0 128 [::]:22 [::]:*
定義完錯誤頁面,此時就會訪問到定義的404.html,但是狀態碼還是404

指定狀態碼
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
49 error_page 404 =200 /404.html; //加上 =(狀態碼)
[root@localhost ~]# nginx -s stop;nginx
此時訪問,狀態碼就變成你設定的狀態碼

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/339107.html
標籤:其他
上一篇:Docker+nginx部署SpringBoot+vue前后端分離專案
下一篇:計算機網路學習01
