Nginx基礎

簡介

Nginx: engine X,2002年開始開發,2004年開源,2019年3月11日,Nginx公司被F5 Network以6.7億美元收購。
Nginx是免費的、開源的、高性能的HTTP和反向代理服務器、郵件代理服務器,以及TCP/UDP代理服務器,解決了C10K問題(10K Connections),http://www.ideawu.net/blog/archives/740.html
Nginx官網:http://nginx.org
Nginx 商業版爲Nginx Plus:https://www.nginx.com/products/nginx/
nginx的其它的二次發行版:
  Tengine:由淘寶網發起的Web服務器項⽬。它在Nginx的基礎上,針對⼤訪問量⽹
站的需求,添加了很多⾼級功能和特性。Tengine的性能和穩定性已經在⼤型的網站如淘寶網,天貓商城等得到了很好的檢驗。它的最終⽬標是打造⼀個高效、穩定、安全、易⽤的Web平臺,從2011年12⽉開始,Tengine成爲⼀個開源項⽬,官⽹ http://tengine.taobao.org/
  OpenResty:基於 Nginx 與 Lua 語言的高性能 Web 平臺, 章亦春團隊開發,官⽹:http://openresty.org/cn/

1 Nginx功能介紹

靜態的web資源服務器html,圖⽚,js,css,txt等靜態資源 結合FastCGI/uWSGI/SCGI等協議反向代理動態資源請求 http/https協議的反向代理 imap4/pop3協議的反向代理 tcp/udp協議的請求轉發(反向代理)

1.1 基礎特性

特性:
模塊化設計,較好的擴展性
⾼可靠性
⽀持熱部署:不停機更新配置⽂件,升級版本,更換⽇志⽂件
低內存消耗:10000個keep-alive連接模式下的⾮活動連接,僅需2.5M內存
event-driven,aio,mmap,sendfile

基本功能:
靜態資源的web服務器
http協議反向代理服務器
pop3/imap4協議反向代理服務器
FastCGI(LNMP),uWSGI(python)等協議
模塊化(⾮DSO),如zip,SSL模塊

1.2 和web服務相關的功能

虛擬主機(server)
⽀持 keep-alive 和管道連接(利⽤⼀個連接做多次請求)
訪問⽇志(⽀持基於⽇志緩衝提⾼其性能)
url rewirte
路徑別名
基於IP及⽤⼾的訪問控制
⽀持速率限制及併發數限制
重新配置和在線升級⽽⽆須中斷客⼾的⼯作進程

2 Nginx組織結構

web請求處理機制: 
1、多進程⽅式:服務器每接收到⼀個客⼾端請求就有服務器的主進程⽣成⼀個⼦進程響應客戶端,直到⽤戶關閉連接,這樣的優勢是處理速度快,各⼦進程之間相互獨⽴,但是如果訪問過⼤會導致服務器資源耗盡⽽⽆法提供請求。 
2、多線程⽅式:與多進程⽅式類似,但是每收到⼀個客⼾端請求會有服務進程派⽣出⼀
個線程來跟客戶⽅進⾏交互,⼀個線程的開銷遠遠⼩於⼀個進程,因此多線程⽅式在很⼤程度減輕了web服務器對系統資源的要求,但是多線程也有⾃⼰的缺點,即當多個線程位於同⼀個進程內⼯作的時候,可以相互訪問同樣的內存地址空間,所以他們相互影響,另外⼀旦主進程掛掉則所有⼦線程都不能⼯作了,IIS服務器使⽤了多線程的⽅式,需要間隔⼀段時間就重啓⼀次才能穩定。

2.1 組織模型

Nginx是多進程組織模型,由Master主進程和Worker工作進程組成,如下圖:

Nginx基礎

主進程(master process)的功能:

讀取Nginx 配置⽂件並驗證其有效性和正確性
建⽴、綁定和關閉socket連接
按照配置⽣成、管理和結束⼯作進程
接受外界指令,⽐如重啓、升級及退出服務器等指令
不中斷服務,實現平滑升級,重啓服務並應⽤新的配置
開啓⽇志⽂件,獲取⽂件描述符
不中斷服務,實現平滑升級,升級失敗進⾏回滾處理
編譯和處理perl腳本

⼯作進程(woker process)的功能:

接受處理客⼾的請求
將請求以此送⼊各個功能模塊進⾏處理
IO調⽤,獲取響應數據
與後端服務器通信,接收後端服務器的處理結果
緩存數據,訪問緩存索引,查詢和調⽤緩存數據
發送請求結果,響應客⼾的請求
接收主程序指令,⽐如重啓、升級和退出等

2.2 進程間通信

Nginx基礎

# 主進程與工作進程通信
⼯作進程是由主進程⽣成的,主進程使⽤fork()函數,在Nginx服務器啓動過程中主進程根據配置⽂件決定啓動⼯作進程的數量,然後建⽴⼀張全局的⼯作表⽤於存放當前未退出的所有的⼯作進程,主進程⽣成⼯作進程後會將新⽣成的⼯作進程加⼊到⼯作進程表中,並建⽴⼀個單向的管道並將其傳遞給⼯作進程,該管道與普通的管道不同,它是由主進程指向⼯作進程的單項通道,包含了主進程向⼯作進程發出的指令、⼯作進程ID、⼯作進程在⼯作進程表中的索引和必要的⽂件描述符等信息。 主進程與外界通過信號機制進⾏通信,當接收到需要處理的信號時,它通過管道向相關的⼯作進程發送正確的指令,每個⼯作進程都有能⼒捕獲管道中的可讀事件,當管道中有可讀事件的時候,⼯作進程就會從管道中讀取並解析指令,然後採取相應的執⾏動作,這樣就完成了主進程與⼯作進程的交互。
# 工作進程間通信
⼯作進程之間的通信原理基本上和主進程與⼯作進程之間的通信是⼀樣的,只要⼯作進程之 間能夠取得彼此的信息,建⽴管道即可通信,但是由於⼯作進程之間是完全隔離的,因此⼀個進程想要直到另外⼀個進程的狀態信息就只能通過主進程來設置了。
爲了實現⼯作進程之間的交互,主進程在⽣成⼯作進程只之後,在⼯作進程表中進⾏遍歷,將該新進程的ID以及針對該進程建⽴的管道句柄傳遞給⼯作進程中的其他進程,爲⼯作進程之間的通信做準備,當⼯作進程1向⼯作進程2發送指令的時候,⾸先在主進程給它的其他⼯作進程⼯作信息中找到2的進程ID,然後將正確的指令寫⼊指向進程2的管道,⼯作進程2捕獲到管道中的事件後,解析指令並進⾏相關操作,這樣就完成了⼯作進程之間的通信。

3 Nginx模塊介紹

名稱 功能
核⼼模塊 Nginx 服務器正常運⾏必不可少的模塊,提供錯誤⽇志記錄、配置⽂件解析、事件驅動機制 、進程管理等核⼼功能
標準HTTP模塊 提供 HTTP 協議解析相關的功能,⽐如:端⼝配置、⽹⻚編碼設置 、HTTP響應頭設置等等
可選HTTP模塊 主要⽤於擴展標準的 HTTP 功能,讓 Nginx 能處理⼀些特殊的服務,⽐如:Flash多媒體傳輸、解析GeoIP請求、⽹絡傳輸壓縮、安全協議SSL⽀持等
郵件服務模塊 主要⽤於⽀持Nginx 的 郵件服務 ,包括對 POP3 協議、 IMAP 協議和 SMTP協議的⽀持
第三⽅模塊 是爲了擴展Nginx服務器應⽤,完成開發者⾃定義功能,⽐如:Json⽀持、Lua⽀持等

4 Nginx安裝

Nginx的安裝版本分爲Mainline version(主要開發版本,其實就是還處於開發版)、Stable version(當前最新穩定版)和Legacy versions(舊的穩定版), Nginx安裝可以使⽤yum或源碼安裝,但是推薦使⽤源碼,⼀是yum的版本⽐較舊,⼆是編譯安裝可以更⽅便⾃定義相關路徑,三是使⽤源碼編譯可以⾃定義相關功能,更⽅便業務的上的使⽤,源碼安裝需要提前準備標準的編譯器,GCC的全稱是(GNU Compiler collection),其有GNU開發,並以GPL即LGPL許可,是⾃由的類UNIX即蘋果電腦Mac OS X操作系統的標準編譯器,因爲GCC原本只能處理C語⾔,所以原名爲GNU C語⾔編譯器,後來得到快速發展,可以處理C++,Fortran,pascal,objective-C,java以及Ada等其他語⾔,此外還需要Automake⼯具,以完成⾃動創建Makefile的⼯作,Nginx的⼀些模塊需要依賴第三⽅庫,⽐如pcre(⽀持rewrite),zlib(⽀持gzip模塊)和openssl(⽀持ssl模塊)等。

4.1 yum安裝

# 此方式需要提前裝備好epel源
[root@CentOS7-01 ~]#yum -y install epel-release
[root@CentOS7-01 ~]#yum -y install nginx
# 查看nginx軟件包的文件
[root@CentOS7-01 ~]#rpm -ql nginx
# 查看nginx軟件包的詳細信息
[root@CentOS7-01 ~]#rpm -qi nginx
# 獲取nginx使用幫助
[root@CentOS7-01 ~]#nginx -h
nginx version: nginx/1.16.1
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 #發送信號
-p prefix : set prefix path (default: /usr/share/nginx/) #指定Nginx ⽬錄
-c filename : set configuration file (default: /etc/nginx/nginx.conf) #配置⽂件路徑
-g directives : set global directives out of configuration file #設置全局指令
# 測試nginx配置文件是否正常
[root@CentOS7-01 ~]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# 顯示版本和編譯參數,該選項在需要安裝和現有版本一樣配置的nginx時很有用
[root@CentOS7-01 ~]#nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-stream_ssl_preread_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
# Nginx啓動腳本
[root@CentOS7-01 ~]#cat /usr/lib/systemd/system/nginx.service
# Nginx主配置文件
[root@CentOS7-01 ~]#cat /etc/nginx/nginx.conf
# 啓動Nginx
[root@CentOS7-01 ~]#systemctl enable --now nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@CentOS7-01 ~]#systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2020-01-01 22:10:43 CST; 4s ago
  Process: 10961 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 10958 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 10956 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 10963 (nginx)
   CGroup: /system.slice/nginx.service
           ├─10963 nginx: master process /usr/sbin/nginx
           ├─10964 nginx: worker process
           └─10965 nginx: worker process

Jan 01 22:10:43 CentOS7-01.localdomain systemd[1]: Starting The nginx HTTP and r....
Jan 01 22:10:43 CentOS7-01.localdomain nginx[10958]: nginx: the configuration fi...k
Jan 01 22:10:43 CentOS7-01.localdomain nginx[10958]: nginx: configuration file /...l
Jan 01 22:10:43 CentOS7-01.localdomain systemd[1]: Started The nginx HTTP and re....
Hint: Some lines were ellipsized, use -l to show in full.
# 查看nginx進程信息
[root@CentOS7-01 ~]#ps -ef |grep nginx|grep -v grep
root      10963      1  0 22:10 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx     10964  10963  0 22:10 ?        00:00:00 nginx: worker process
nginx     10965  10963  0 22:10 ?        00:00:00 nginx: worker process
# 訪問測試
[root@CentOS7-01 ~]#curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Wed, 01 Jan 2020 14:14:02 GMT
Content-Type: text/html
Content-Length: 4833
Last-Modified: Fri, 16 May 2014 15:12:48 GMT
Connection: keep-alive
ETag: "53762af0-12e1"
Accept-Ranges: bytes

4.2 編譯安裝

# 準備編譯安裝的基礎環境
[root@CentOS7-01 ~]#yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools iotop bc zip unzip zlib-devel bash-completion nfs-utils automake libxml2 libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed GeoIP-devel gd-devel
# 安裝nginx
[root@CentOS7-01 ~]#cd /usr/local/src/
[root@CentOS7-01 src]#wget https://nginx.org/download/nginx-1.16.1.tar.gz
[root@CentOS7-01 src]#tar xf nginx-1.16.1.tar.gz
[root@CentOS7-01 src]#cd nginx-1.16.1
# 注:編譯是爲了檢查系統環境是否符合編譯安裝的要求,⽐如是否有gcc編譯⼯具,是否⽀持編譯參數當中的模塊,並根據開啓的參數等⽣成Makefile⽂件爲下⼀步做準備:
[root@CentOS7-01 src]#./configure --prefix=/apps/nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module  \
--with-http_image_filter_module \
--with-http_geoip_module \
--with-http_gunzip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module
[root@CentOS7-01 src]#make -j `lscpu |awk 'NR==4{print $2}'` && make install
[root@CentOS7-01 src]#useradd nginx -s /sbin/nologin -u 2000
[root@CentOS7-01 src]#chown nginx:nginx -R /apps/nginx/
注:nginx安裝完成後,有四個主要的目錄
conf:該⽬錄中保存了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⼆進制啓動腳本,可以接受不同的參數以實現不同的功能。
# 配置環境變量
[root@CentOS7-01 src]#echo "PATH=/apps/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh
[root@CentOS7-01 src]#source /etc/profile.d/nginx.sh
# 驗證版本及編譯參數
[root@CentOS7-01 src]#nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_image_filter_module --with-http_geoip_module --with-http_gunzip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
# 啓動nginx並測試訪問
[root@CentOS7-01 src]#nginx
[root@CentOS7-01 src]#curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Thu, 02 Jan 2020 03:37:58 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 02 Jan 2020 03:09:54 GMT
Connection: keep-alive
ETag: "5e0d5f02-264"
Accept-Ranges: bytes
# 創建nginx自啓動腳本
[root@CentOS7-01 src]#cat /usr/lib/systemd/system/nginx.service 
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/apps/nginx/sbin/nginx -t  #此處要寫nginx編譯安裝時候指定安裝的位置路徑                                            
ExecStart=/apps/nginx/sbin/nginx #同上
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
[root@CentOS7-01 src]#sed -i -e '/^#pid/a\pid \/run\/nginx.pid;' -e '/^#user/a\user nginx;' /apps/nginx/conf/nginx.conf
# 驗證nginx自啓動腳本
[root@CentOS7-01 src]#systemctl deamon-reload
[root@CentOS7-01 src]#systemctl enable --now nginx
[root@CentOS7-01 src]#systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2020-01-02 12:31:42 CST; 1s ago
  Process: 32753 ExecStart=/apps/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 32750 ExecStartPre=/apps/nginx/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 32748 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 32755 (nginx)
   CGroup: /system.slice/nginx.service
           ├─32755 nginx: master process /apps/nginx/sbin/nginx
           └─32756 nginx: worker process

Jan 02 12:31:42 CentOS7-01.localdomain systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jan 02 12:31:42 CentOS7-01.localdomain nginx[32750]: nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
Jan 02 12:31:42 CentOS7-01.localdomain nginx[32750]: nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
Jan 02 12:31:42 CentOS7-01.localdomain systemd[1]: Started The nginx HTTP and reverse proxy server.

# nginx默認主配置文件解釋
主配置文件路徑:/apps/nginx/conf/nginx.conf
主配置文件的配置指令方式:
directive value [value2 ...];
注意:
(1) 指令必須以分號結尾
(2) ⽀持使⽤配置變量
        內建變量:由Nginx模塊引⼊,可直接引⽤
        ⾃定義變量:由用戶使⽤set命令定義
                   set variable_name value;
        引⽤變量:$variable_name

[root@CentOS7-01 src]#egrep -v "^$|#" /apps/nginx/conf/nginx.conf
#全局配置段,對全局⽣效,主要設置nginx工作進程的啓動⽤戶/組、啓動的⼯作進程數量、⼯作模式、Nginx的PID文件路徑,⽇志路徑等。
user  nginx; #啓動Nginx⼯作進程的⽤戶,默認是nobody
worker_processes  1; #啓動⼯作進程數數量
pid /run/nginx.pid; #nginx的pid文件路徑
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; # 導入⽀持的mime類型,MIME(Multipurpose Internet Mail Extensions)多⽤途互聯⽹郵件擴展類型,MIME消息能包含⽂本、圖像、⾳頻、視頻以及其他應⽤程序專⽤的數據,是設定某種擴展名的⽂件⽤⼀種應⽤程序來打開的⽅式類型,當該擴展名⽂件被訪問的時候,瀏覽器會⾃動使⽤指定應⽤程序來打開。多⽤於指定⼀些客戶端⾃定義的⽂件名,以及⼀些媒體⽂件打開⽅式。MIME參考⽂檔:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/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會調⽤當前server內部的配置進程匹配。
        location / { #location其實是server的⼀個指令,爲nginx服務器提供⽐較多⽽且靈活的指令,都是在location中體現的,主要是基於nginx接收到的請求字符串,對⽤戶請求的URL進⾏匹配,並對特定的指令進⾏處理,包括地址重定向、數據緩存和應答控制等功能都是在這部分實現,另外很多第三⽅模塊的配置也是在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; #定義默認⻚⾯所在的⽬錄
        }
    }
}

4.3 編譯安裝後使用systemd管理nginx服務時遇到的問題及解決方法

# 在上面,編譯安裝完成後創建了一個nginx服務腳本,目的是爲了使用systemd來管理nginx,然而在啓動的時候報如下錯誤,導致nginx啓動失敗
[root@CentOS7-01 src]#systemctl restart nginx
Job for nginx.service failed because a timeout was exceeded. See "systemctl status nginx.service" and "journalctl -xe" for details.
[root@CentOS7-01 src]#tail -f /var/log/messages
Jan  2 14:37:09 CentOS7-01 systemd: Starting The nginx HTTP and reverse proxy server...
Jan  2 14:37:09 CentOS7-01 nginx: nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
Jan  2 14:37:09 CentOS7-01 nginx: nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
Jan  2 14:37:10 CentOS7-01 systemd: Can't open PID file /run/nginx.pid (yet?) after start: No such file or directory
Jan  2 14:38:40 CentOS7-01 systemd: nginx.service start operation timed out. Terminating.
Jan  2 14:38:40 CentOS7-01 systemd: Failed to start The nginx HTTP and reverse proxy server.
Jan  2 14:38:40 CentOS7-01 systemd: Unit nginx.service entered failed state.
Jan  2 14:38:40 CentOS7-01 systemd: nginx.service failed.

# 問題排查
根據報錯提示是由於找不到/run/nginx.pid文件導致的nginx啓動失敗,來看一下nginx服務的service文件和主配置文件中定義的PID文件路徑
[root@CentOS7-01 src]#grep "^PID" /usr/lib/systemd/system/nginx.service 
PIDFile=/run/nginx.pid
[root@CentOS7-01 src]#grep -w "pid" /apps/nginx/conf/nginx.conf
#pid        logs/nginx.pid;
可以發現它倆的路徑並不是同一個,而使用systemctl來啓動nginx時,會去找service文件中定義的PID文件路徑,但在加載主配置文件時發現默認定義的PID文件路徑是logs/nginx.pid,並且也沒有具體定義PID文件的路徑和service文件中定義的是同一個,故報此錯誤。

# 解決方法
a.在主配置文件中定義pid文件的絕對路徑
[root@CentOS7-01 src]#vim /apps/nginx/conf/nginx.conf
pid /run/nginx.pid;

或

b.在nginx服務的service文件中指定默認定義的pid文件的絕對路徑
[root@CentOS7-01 src]#vim /usr/lib/systemd/system/nginx.service 
PIDFile=/apps/nginx/logs/nginx.pid

然後再次啓動服務即可
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章