nginx簡單介紹:
(學習視頻分享:編程入門)
nginx (engine x) 是一款輕量級的 Web 服務器 、反向代理服務器及電子郵件(IMAP/POP3)代理服務器。它是來自俄羅斯的Igor Sysoev在為Rambler Media工作期間,使用C語言開發(fā)的。
Igor Sysoev將Nginx的代碼開源,并且賦予其最自由的2-clause BSD-like license許可證。由于Nginx使用基于事件驅(qū)動的架構能夠并發(fā)處理百萬級別的TCP連接,高度模塊化的設計和自由的許可證使得擴展Nginx功能的第三方模塊層出不窮,而且優(yōu)秀的設計帶來了極佳的穩(wěn)定性,因此其作為Web服務器被廣泛應用到大流量的網(wǎng)站上。
所謂反向代理(Reverse Proxy)方式是指以代理服務器來接受 internet 上的連接請求,然后將請求轉(zhuǎn)發(fā)給內(nèi)部網(wǎng)絡上的服務器,并將從服務器上得到的結(jié)果返回給 internet 上請求連接的客戶端,此時代理服務器對外就表現(xiàn)為一個反向代理服務器。
既然有反向代理,那么也就有正向代理。正向代理是一個位于客戶端和原始服務器之間的服務器,為了從原始服務器取得內(nèi)容,客戶端向代理發(fā)送一個請求并指定目標,然后代理向原始服務器轉(zhuǎn)交請求并將獲得的內(nèi)容返回給客戶端。
可以說正向代理代理的是客戶端,反向代理代理的是服務器。
使用Nginx有如下優(yōu)勢:
依賴庫
現(xiàn)在服務器一般都使用Linux操作系統(tǒng),在編譯和安裝Nginx之前,你需要先安裝其依賴的庫。
下面列舉幾個完成Web服務器最基本功能所必需的庫。
GCC
GCC(GNU Compiler Collection)可用來編譯C語言程序。
Nginx通常不會直接提供二進制可執(zhí)行程序,因此我們需要編譯其源碼。
而且我們可能會使用C++來編寫Nginx HTTP模塊,這時就需要用到G++編譯器了。
用yum安裝G++編譯器:
yum?install?-y?gcc-c++
PCRE
PCRE庫PCRE(Perl Compatible Regular Expressions,Perl兼容正則表達式)是由Philip Hazel開發(fā)的函數(shù)庫,目前為很多軟件所使用,該庫支持正則表達式。它由RegEx演化而來,實際上, Perl正則表達式也是源自于Henry Spencer寫的RegEx。
如果我們在配置文件nginx.conf里使用了正則表達式,那么在編譯Nginx時就必須把PCRE庫編譯進Nginx,因為Nginx的HTTP模塊要靠它來解析正則表達式。
當然,如果你確認不會使用正則表達式,就不必安裝它。
其yum安裝方式如下:
yum?install?-y?pcre?pcre-devel
pcre-devel是使用PCRE做二次開發(fā)時所需要的開發(fā)庫,包括頭文件等,這也是編譯Nginx所必須使用的。
zlib庫
zlib庫用于對HTTP包的內(nèi)容做gzip格式的壓縮,如果我們在nginx.conf里配置了gzip on, 并指定對于某些類型(content-type)的HTTP響應使用gzip來進行壓縮以減少網(wǎng)絡傳輸量,那么,在編譯時就必須把zlib編譯進Nginx。
其yum安裝方式如下
yum?install?-y?zlib?zlib-devel
同理,zlib是直接使用的庫,zlib-devel是二次開發(fā)所需要的庫。
OpenSSL開發(fā)庫
如果我們的服務器不只是要支持HTTP,還需要在更安全的SSL協(xié)議上傳輸HTTP,那么就需要擁有OpenSSL了。
另外,如果我們想使用MD5、SHA1等散列函數(shù),那么也需要安裝它。
其yum安裝方式如下:
yum?install?-y?openssl?openssl-devel
下載源碼包
進入Nginx官方站點的下載界面,選擇最新的穩(wěn)定版本。
然后使用 wget 命令下載:
[root@host?nginx]#?wget?http://nginx.org/download/nginx-1.16.0.tar.gz --2019-05-23?03:28:52--??http://nginx.org/download/nginx-1.16.0.tar.gz Resolving?nginx.org...?62.210.92.35,?95.211.80.227,?2001:1af8:4060:a004:21::e3 Connecting?to?nginx.org|62.210.92.35|:80...?connected. HTTP?request?sent,?awaiting?response...?200?OK Length:?1032345?(1008K)?[application/octet-stream] Saving?to:?“nginx-1.16.0.tar.gz” 100%[==========================================================================================================================================>]?1,032,345????715K/s???in?1.4s???? 2019-05-23?03:28:53?(715?KB/s)?-?“nginx-1.16.0.tar.gz”?saved?[1032345/1032345]
解壓文件:
[root@host?nginx]#?tar?xf?nginx-1.16.0.tar.gz? [root@host?nginx]#?ls nginx-1.16.0??nginx-1.16.0.tar.gz [root@host?nginx]#?cd?nginx-1.16.0 [root@host?nginx-1.16.0]#?ls auto??CHANGES??CHANGES.ru??conf??configure??contrib??html??LICENSE??man??README??src
編譯安裝
編譯并安裝Nginx使用下面三條命令:
./configure?make?make?install
如果你依賴的庫找不到的話,在執(zhí)行./configure命令的時候會報錯,例如找不到PCRE庫:
./configure:?error:?the?HTTP?rewrite?module?requires?the?PCRE?library. You?can?either?disable?the?module?by?using?--without-http_rewrite_module option,?or?install?the?PCRE?library?into?the?system,?or?build?the?PCRE?library statically?from?the?source?with?nginx?by?using?--with-pcre=<path>?option.</path>
正常的輸出應該是下面這樣,并且生成了Makefile:
[root@host?nginx-1.16.0]#?./configure checking?for?OS ?+?Linux?4.10.4-1.el6.elrepo.i686?i686 checking?for?C?compiler?...?found ?+?using?GNU?C?compiler ?+?gcc?version:?4.4.7?20120313?(Red?Hat?4.4.7-23)?(GCC)? checking?for?gcc?-pipe?switch?...?found checking?for?-Wl,-E?switch?...?found checking?for?gcc?builtin?atomic?operations?...?found checking?for?C99?variadic?macros?...?found checking?for?gcc?variadic?macros?...?found checking?for?gcc?builtin?64?bit?byteswap?...?found checking?for?unistd.h?...?found checking?for?inttypes.h?...?found checking?for?limits.h?...?found checking?for?sys/filio.h?...?not?found checking?for?sys/param.h?...?found checking?for?sys/mount.h?...?found checking?for?sys/statvfs.h?...?found checking?for?crypt.h?...?found checking?for?Linux?specific?features checking?for?epoll?...?found checking?for?EPOLLRDHUP?...?found checking?for?EPOLLEXCLUSIVE?...?not?found checking?for?O_PATH?...?not?found checking?for?sendfile()?...?found checking?for?sendfile64()?...?found checking?for?sys/prctl.h?...?found checking?for?prctl(PR_SET_DUMPABLE)?...?found checking?for?prctl(PR_SET_KEEPCAPS)?...?found checking?for?capabilities?...?found checking?for?crypt_r()?...?found checking?for?sys/vfs.h?...?found checking?for?nobody?group?...?found checking?for?poll()?...?found checking?for?/dev/poll?...?not?found checking?for?kqueue?...?not?found checking?for?crypt()?...?not?found checking?for?crypt()?in?libcrypt?...?found checking?for?F_READAHEAD?...?not?found checking?for?posix_fadvise()?...?found checking?for?O_DIRECT?...?found checking?for?F_NOCACHE?...?not?found checking?for?directio()?...?not?found checking?for?statfs()?...?found checking?for?statvfs()?...?found checking?for?dlopen()?...?not?found checking?for?dlopen()?in?libdl?...?found checking?for?sched_yield()?...?found checking?for?sched_setaffinity()?...?found checking?for?SO_SETFIB?...?not?found checking?for?SO_REUSEPORT?...?found checking?for?SO_ACCEPTFILTER?...?not?found checking?for?SO_BINDANY?...?not?found checking?for?IP_TRANSPARENT?...?found checking?for?IP_BINDANY?...?not?found checking?for?IP_BIND_ADDRESS_NO_PORT?...?not?found checking?for?IP_RECVDSTADDR?...?not?found checking?for?IP_SENDSRCADDR?...?not?found checking?for?IP_PKTINFO?...?found checking?for?IPV6_RECVPKTINFO?...?found checking?for?TCP_DEFER_ACCEPT?...?found checking?for?TCP_KEEPIDLE?...?found checking?for?TCP_FASTOPEN?...?not?found checking?for?TCP_INFO?...?found checking?for?accept4()?...?found checking?for?eventfd()?...?found checking?for?int?size?...?4?bytes checking?for?long?size?...?4?bytes checking?for?long?long?size?...?8?bytes checking?for?void?*?size?...?4?bytes checking?for?uint32_t?...?found checking?for?uint64_t?...?found checking?for?sig_atomic_t?...?found checking?for?sig_atomic_t?size?...?4?bytes checking?for?socklen_t?...?found checking?for?in_addr_t?...?found checking?for?in_port_t?...?found checking?for?rlim_t?...?found checking?for?uintptr_t?...?uintptr_t?found checking?for?system?byte?ordering?...?little?endian checking?for?size_t?size?...?4?bytes checking?for?off_t?size?...?8?bytes checking?for?time_t?size?...?4?bytes checking?for?AF_INET6?...?found checking?for?setproctitle()?...?not?found checking?for?pread()?...?found checking?for?pwrite()?...?found checking?for?pwritev()?...?found checking?for?sys_nerr?...?found checking?for?localtime_r()?...?found checking?for?clock_gettime(CLOCK_MONOTONIC)?...?not?found checking?for?clock_gettime(CLOCK_MONOTONIC)?in?librt?...?found checking?for?posix_memalign()?...?found checking?for?memalign()?...?found checking?for?mmap(MAP_ANON|MAP_SHARED)?...?found checking?for?mmap("/dev/zero",?MAP_SHARED)?...?found checking?for?System?V?shared?memory?...?found checking?for?POSIX?semaphores?...?not?found checking?for?POSIX?semaphores?in?libpthread?...?found checking?for?struct?msghdr.msg_control?...?found checking?for?ioctl(FIONBIO)?...?found checking?for?struct?tm.tm_gmtoff?...?found checking?for?struct?dirent.d_namlen?...?not?found checking?for?struct?dirent.d_type?...?found checking?for?sysconf(_SC_NPROCESSORS_ONLN)?...?found checking?for?sysconf(_SC_LEVEL1_DCACHE_LINESIZE)?...?found checking?for?openat(),?fstatat()?...?found checking?for?getaddrinfo()?...?found checking?for?PCRE?library?...?found checking?for?PCRE?JIT?support?...?not?found checking?for?zlib?library?...?found creating?objs/Makefile Configuration?summary ??+?using?system?PCRE?library ??+?OpenSSL?library?is?not?used ??+?using?system?zlib?library ??nginx?path?prefix:?"/usr/local/nginx" ??nginx?binary?file:?"/usr/local/nginx/sbin/nginx" ??nginx?modules?path:?"/usr/local/nginx/modules" ??nginx?configuration?prefix:?"/usr/local/nginx/conf" ??nginx?configuration?file:?"/usr/local/nginx/conf/nginx.conf" ??nginx?pid?file:?"/usr/local/nginx/logs/nginx.pid" ??nginx?error?log?file:?"/usr/local/nginx/logs/error.log" ??nginx?http?access?log?file:?"/usr/local/nginx/logs/access.log" ??nginx?http?client?request?body?temporary?files:?"client_body_temp" ??nginx?http?proxy?temporary?files:?"proxy_temp" ??nginx?http?fastcgi?temporary?files:?"fastcgi_temp" ??nginx?http?uwsgi?temporary?files:?"uwsgi_temp" ??nginx?http?scgi?temporary?files:?"scgi_temp"
查看Nginx版本
安裝成功以后,可以通過-v參數(shù)查看Nginx版本。
[root@host?sbin]#?/usr/local/nginx/sbin/nginx?-v?nginx?version:?nginx/1.16.0
啟動
Nginx支持直接啟動,也支持帶參數(shù)啟動,下面分別演示一下。
端口占用
Nginx需要使用80端口,如果80端口被占用,啟動會有如下報錯:
nginx:?[emerg]?bind()?to?0.0.0.0:80?failed?(98:?Address?already?in?use) nginx:?[emerg]?bind()?to?0.0.0.0:80?failed?(98:?Address?already?in?use) nginx:?[emerg]?bind()?to?0.0.0.0:80?failed?(98:?Address?already?in?use) nginx:?[emerg]?bind()?to?0.0.0.0:80?failed?(98:?Address?already?in?use) nginx:?[emerg]?bind()?to?0.0.0.0:80?failed?(98:?Address?already?in?use)
可以使用lsof工具查看端口占用情況,如果你沒有裝,可以使用如下命令安裝:
yum?install?-y?lsof
查看本機80端口的占用情況,并殺掉占用的進程:
[root@host?sbin]#?lsof?-i?:80 COMMAND??PID?USER???FD???TYPE?DEVICE?SIZE/OFF?NODE?NAME java????1765?root???53u??IPv6??15062??????0t0??TCP?*:http?(LISTEN) [root@host?sbin]#?killall?-9?java [root@host?sbin]#?lsof?-i?:80 [root@host?sbin]#
默認啟動
使用whereis命令查看nginx的安裝目錄:
[root@host?nginx-1.16.0]#?whereis?nginx nginx:?/usr/local/nginx
如果不加任何參數(shù)啟動,會使用默認的nginx.conf配置文件啟動Nginx:
/usr/local/nginx/sbin/nginx
啟動成功以后,再請求服務器的時候可以看到包含下面內(nèi)容的網(wǎng)頁:
Welcome?to?nginx! If?you?see?this?page,?the?nginx?web?server?is?successfully?installed?and?working.?Further?configuration?is?required. For?online?documentation?and?support?please?refer?to?nginx.org. Commercial?support?is?available?at?nginx.com. Thank?you?for?using?nginx.
帶參數(shù)啟動
-c參數(shù)指定配置文件的啟動方式:
./nginx?-c?mynginx.conf
-p參數(shù)指定Nginx的安裝目錄:
./nginx?-p?mydir/nginx
-g參數(shù)臨時指定一些全局配置項
./nginx?-g?"pid?varnginx/test.pid;"
上面這行命令意味著會把pid文件寫到varnginx/test.pid中。
-g參數(shù)的約束條件是指定的配置項不能與默認路徑下的nginx.conf中的配置項相沖突,否則無法啟動。
就像上例那樣,類似這樣的配置項:pid logs/nginx.pid,是不能存在于默認的nginx.conf中的。
另一個約束條件是,以-g方式啟動的Nginx服務執(zhí)行其他命令行時,需要把-g參數(shù)也帶上,否則可能出現(xiàn)配置項不匹配的情形。
在不啟動Nginx的情況下,使用-t參數(shù)僅測試配置文件是否有錯誤。 例如:
./nginx?-t
執(zhí)行結(jié)果中顯示配置是否正確。
[root@host?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
測試配置選項時,使用-q參數(shù)可以不把error級別以下的信息輸出到屏幕。 例如:
./nginx?-t?-q
停止服務
停止Nginx的服務主要有兩種方式。
一種是快速停止,即立即停止Nginx服務正在處理的所有網(wǎng)絡請求,馬上丟棄連接停止服務。
另外一種是平緩地停止,即允許Nginx處理完當前的請求,但不再接收新的請求,之后再關閉連接,停止工作。
快速停止服務
/usr/local/nginx/sbin/nginx?-s?stop
kill服務
kill -s SIGTERM 進程ID或kill -s SIGINT 進程ID與上面./nginx -s stop命令的效果是一樣的。
[root@host?sbin]#?ps?-ef|grep?nginx? root?????10568?????1??0?04:22??????????00:00:00?nginx:?master?process?./nginx nobody???10569?10568??0?04:22??????????00:00:00?nginx:?worker?process root?????10571??5440??0?04:23?pts/1????00:00:00?grep?nginx [root@host?sbin]#?kill?-s?SIGINT?10568 [root@host?sbin]#?ps?-ef|grep?nginx? root?????10574??5440??0?04:24?pts/1????00:00:00?grep?nginx [root@host?sbin]#
優(yōu)雅地停止服務
如果希望Nginx服務可以正常地處理完當前所有請求再停止服務,那么可以使用-s quit參數(shù)來停止服務。
例如:
./nginx?-s?quit
該命令與快速停止Nginx服務是有區(qū)別的。
當快速停止服務時,worker進程與master進程在收到信號后會立刻跳出循環(huán),退出進程。
而“優(yōu)雅”地停止服務時,首先會關閉監(jiān)聽端口,停止接收新的連接,然后把當前正在處理的連接全部處理完,最后再退出進程。
與快速停止服務相似,可以直接發(fā)送QUIT信號給master進程來停止服務,其效果與執(zhí)行-s quit命令是一樣的。
例如:
kill?-s?SIGQUIT?<nginx></nginx>
如果希望“優(yōu)雅”地停止某個worker進程,那么可以通過向該進程發(fā)送WINCH信號來停止服務 。
例如:
kill?-s?SIGWINCH?<nginx></nginx>
發(fā)送信號
./nginx?-g?TERM?|?INT?|?QUIT
TERM 和 INT 信號用于快速停止,QUIT 信號用于平滑停止。
Nginx重新加載配置
使運行中的Nginx重讀配置項并生效
使用-s reload參數(shù)可以使運行中的Nginx服務重新加載nginx.conf文件。 例如:
usrlocal/nginx/sbin/nginx?-s?reload
日志文件回滾
使用-s reopen參數(shù)可以重新打開日志文件,這樣可以先把當前日志文件改名或轉(zhuǎn)移到其他目錄中進行備份,再重新打開時就會生成新的日志文件。
這個功能使得日志文件不至于過大。 例如:
./nginx?-s?reopen
這與使用kill命令發(fā)送USR1信號效果相同。
kill?-s?SIGUSR1?<nginx></nginx>
相關推薦:編程入門