快速上手Nginx
Nginx (engine x) 是一款輕量級(jí)的 Web 服務(wù)器 、反向代理服務(wù)器及電子郵件(IMAP/POP3)代理服務(wù)器。它是來自俄羅斯的Igor Sysoev在為Rambler Media工作期間,使用C語言開發(fā)的。
Igor Sysoev將Nginx的代碼開源,并且賦予其最自由的2-clause BSD-like license許可證。由于Nginx使用基于事件驅(qū)動(dòng)的架構(gòu)能夠并發(fā)處理百萬級(jí)別的TCP連接,高度模塊化的設(shè)計(jì)和自由的許可證使得擴(kuò)展Nginx功能的第三方模塊層出不窮,而且優(yōu)秀的設(shè)計(jì)帶來了極佳的穩(wěn)定性,因此其作為Web服務(wù)器被廣泛應(yīng)用到大流量的網(wǎng)站上。
所謂反向代理(Reverse Proxy)方式是指以代理服務(wù)器來接受 internet 上的連接請(qǐng)求,然后將請(qǐng)求轉(zhuǎn)發(fā)給內(nèi)部網(wǎng)絡(luò)上的服務(wù)器,并將從服務(wù)器上得到的結(jié)果返回給 internet 上請(qǐng)求連接的客戶端,此時(shí)代理服務(wù)器對(duì)外就表現(xiàn)為一個(gè)反向代理服務(wù)器。
既然有反向代理,那么也就有正向代理。正向代理是一個(gè)位于客戶端和原始服務(wù)器之間的服務(wù)器,為了從原始服務(wù)器取得內(nèi)容,客戶端向代理發(fā)送一個(gè)請(qǐng)求并指定目標(biāo),然后代理向原始服務(wù)器轉(zhuǎn)交請(qǐng)求并將獲得的內(nèi)容返回給客戶端。
可以說正向代理代理的是客戶端,反向代理代理的是服務(wù)器。
使用Nginx有如下優(yōu)勢(shì):
| 更快 | Nginx可以比其他web服務(wù)器更快地響應(yīng)請(qǐng)求 |
| 高擴(kuò)展性 | Nginx采用低耦合的設(shè)計(jì),擁有眾多優(yōu)秀的的第三方模塊 |
| 高穩(wěn)定性 | 每個(gè)worker進(jìn)程相對(duì)獨(dú)立,master進(jìn)程在1個(gè)worker進(jìn)程出錯(cuò)時(shí)可以快速“拉起”新的worker子進(jìn)程提供服務(wù) |
| 低內(nèi)存消耗 | 一般情況下,10000個(gè)非活躍的HTTP Keep-Alive連接在Nginx中僅消耗2.5MB的內(nèi)存,這是Nginx支持高并發(fā)連接的基礎(chǔ)。單機(jī)支持10萬以上的并發(fā)連接,這并不是極限,限制主要取決于內(nèi)存 |
| 熱部署 | aster管理進(jìn)程與worker工作進(jìn)程的分離設(shè)計(jì),使得Nginx能夠提供熱部署功能,即可以在7×24小時(shí)不間斷服務(wù)的前提下,升級(jí)Nginx的可執(zhí)行文件。當(dāng)然,它也支持不停止服務(wù)就更新配置項(xiàng)、更換日志文件等功能。 |
| 最自由的BSD許可協(xié)議 | 這是Nginx可以快速發(fā)展的強(qiáng)大動(dòng)力。BSD許可協(xié)議不只是允許用戶免費(fèi)使用Nginx,它還允許用戶在自己的項(xiàng)目中直接使用或修改Nginx源碼,然后發(fā)布。 |
| 豐富的模塊 | 擁有無數(shù)個(gè)官方功能模塊、第三方功能模塊使得Nginx能夠滿足絕大部分應(yīng)用場(chǎng)景,這些功能模塊間可以疊加以實(shí)現(xiàn)更加強(qiáng)大、復(fù)雜的功能,有些模塊還支持Nginx與Perl、Lua等腳本語言集成工作,大大提高了開發(fā)效率。 |
依賴庫
現(xiàn)在服務(wù)器一般都使用Linux操作系統(tǒng),在編譯和安裝Nginx之前,你需要先安裝其依賴的庫。
下面列舉幾個(gè)完成Web服務(wù)器最基本功能所必需的庫。
GCC
GCC(GNU Compiler Collection)可用來編譯C語言程序。
Nginx通常不會(huì)直接提供二進(jìn)制可執(zhí)行程序,因此我們需要編譯其源碼。
而且我們可能會(huì)使用C++來編寫Nginx HTTP模塊,這時(shí)就需要用到G++編譯器了。
用yum安裝G++編譯器:
yum install -y gcc-c++ 復(fù)制代碼PCRE
PCRE庫PCRE(Perl Compatible Regular Expressions,Perl兼容正則表達(dá)式)是由Philip Hazel開發(fā)的函數(shù)庫,目前為很多軟件所使用,該庫支持正則表達(dá)式。它由RegEx演化而來,實(shí)際上, Perl正則表達(dá)式也是源自于Henry Spencer寫的RegEx。
如果我們?cè)谂渲梦募ginx.conf里使用了正則表達(dá)式,那么在編譯Nginx時(shí)就必須把PCRE庫編譯進(jìn)Nginx,因?yàn)镹ginx的HTTP模塊要靠它來解析正則表達(dá)式。
當(dāng)然,如果你確認(rèn)不會(huì)使用正則表達(dá)式,就不必安裝它。
其yum安裝方式如下:
yum install -y pcre pcre-devel 復(fù)制代碼pcre-devel是使用PCRE做二次開發(fā)時(shí)所需要的開發(fā)庫,包括頭文件等,這也是編譯Nginx所必須使用的。
zlib庫
zlib庫用于對(duì)HTTP包的內(nèi)容做gzip格式的壓縮,如果我們?cè)趎ginx.conf里配置了gzip on, 并指定對(duì)于某些類型(content-type)的HTTP響應(yīng)使用gzip來進(jìn)行壓縮以減少網(wǎng)絡(luò)傳輸量,那么,在編譯時(shí)就必須把zlib編譯進(jìn)Nginx。
其yum安裝方式如下
yum install -y zlib zlib-devel 復(fù)制代碼同理,zlib是直接使用的庫,zlib-devel是二次開發(fā)所需要的庫。
OpenSSL開發(fā)庫
如果我們的服務(wù)器不只是要支持HTTP,還需要在更安全的SSL協(xié)議上傳輸HTTP,那么就需要擁有OpenSSL了。
另外,如果我們想使用MD5、SHA1等散列函數(shù),那么也需要安裝它。
其yum安裝方式如下:
yum install -y openssl openssl-devel 復(fù)制代碼下載源碼包
進(jìn)入Nginx官方站點(diǎn)的下載界面,選擇最新的穩(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] 復(fù)制代碼解壓文件:
[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復(fù)制代碼編譯安裝
編譯并安裝Nginx使用下面三條命令:
./configure make make install 復(fù)制代碼如果你依賴的庫找不到的話,在執(zhí)行./configure命令的時(shí)候會(huì)報(bào)錯(cuò),例如找不到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. 復(fù)制代碼正常的輸出應(yīng)該是下面這樣,并且生成了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/MakefileConfiguration summary+ using system PCRE library+ OpenSSL library is not used+ using system zlib librarynginx 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" 復(fù)制代碼查看Nginx版本
安裝成功以后,可以通過-v參數(shù)查看Nginx版本。
[root@host sbin]# /usr/local/nginx/sbin/nginx -v nginx version: nginx/1.16.0 復(fù)制代碼啟動(dòng)
Nginx支持直接啟動(dòng),也支持帶參數(shù)啟動(dòng),下面分別演示一下。
端口占用
Nginx需要使用80端口,如果80端口被占用,啟動(dòng)會(huì)有如下報(bào)錯(cuò):
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) 復(fù)制代碼可以使用lsof工具查看端口占用情況,如果你沒有裝,可以使用如下命令安裝:
yum install -y lsof 復(fù)制代碼查看本機(jī)80端口的占用情況,并殺掉占用的進(jìn)程:
[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]# 復(fù)制代碼默認(rèn)啟動(dòng)
使用whereis命令查看nginx的安裝目錄:
[root@host nginx-1.16.0]# whereis nginx nginx: /usr/local/nginx 復(fù)制代碼如果不加任何參數(shù)啟動(dòng),會(huì)使用默認(rèn)的nginx.conf配置文件啟動(dòng)Nginx:
/usr/local/nginx/sbin/nginx 復(fù)制代碼啟動(dòng)成功以后,再請(qǐng)求服務(wù)器的時(shí)候可以看到包含下面內(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. 復(fù)制代碼帶參數(shù)啟動(dòng)
-c參數(shù)指定配置文件的啟動(dòng)方式:
./nginx -c mynginx.conf 復(fù)制代碼-p參數(shù)指定Nginx的安裝目錄:
./nginx -p mydir/nginx 復(fù)制代碼-g參數(shù)臨時(shí)指定一些全局配置項(xiàng)
./nginx -g "pid varnginx/test.pid;" 復(fù)制代碼上面這行命令意味著會(huì)把pid文件寫到varnginx/test.pid中。
-g參數(shù)的約束條件是指定的配置項(xiàng)不能與默認(rèn)路徑下的nginx.conf中的配置項(xiàng)相沖突,否則無法啟動(dòng)。
就像上例那樣,類似這樣的配置項(xiàng):pid logs/nginx.pid,是不能存在于默認(rèn)的nginx.conf中的。
另一個(gè)約束條件是,以-g方式啟動(dòng)的Nginx服務(wù)執(zhí)行其他命令行時(shí),需要把-g參數(shù)也帶上,否則可能出現(xiàn)配置項(xiàng)不匹配的情形。
在不啟動(dòng)Nginx的情況下,使用-t參數(shù)僅測(cè)試配置文件是否有錯(cuò)誤。 例如:
./nginx -t 復(fù)制代碼執(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 復(fù)制代碼測(cè)試配置選項(xiàng)時(shí),使用-q參數(shù)可以不把error級(jí)別以下的信息輸出到屏幕。 例如:
./nginx -t -q 復(fù)制代碼停止服務(wù)
停止Nginx的服務(wù)主要有兩種方式。
一種是快速停止,即立即停止Nginx服務(wù)正在處理的所有網(wǎng)絡(luò)請(qǐng)求,馬上丟棄連接停止服務(wù)。
另外一種是平緩地停止,即允許Nginx處理完當(dāng)前的請(qǐng)求,但不再接收新的請(qǐng)求,之后再關(guān)閉連接,停止工作。
快速停止服務(wù)
/usr/local/nginx/sbin/nginx -s stop 復(fù)制代碼kill服務(wù)
kill -s SIGTERM 進(jìn)程ID或kill -s SIGINT 進(jìn)程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]# 復(fù)制代碼優(yōu)雅地停止服務(wù)
如果希望Nginx服務(wù)可以正常地處理完當(dāng)前所有請(qǐng)求再停止服務(wù),那么可以使用-s quit參數(shù)來停止服務(wù)。
例如:
./nginx -s quit 復(fù)制代碼該命令與快速停止Nginx服務(wù)是有區(qū)別的。
當(dāng)快速停止服務(wù)時(shí),worker進(jìn)程與master進(jìn)程在收到信號(hào)后會(huì)立刻跳出循環(huán),退出進(jìn)程。
而“優(yōu)雅”地停止服務(wù)時(shí),首先會(huì)關(guān)閉監(jiān)聽端口,停止接收新的連接,然后把當(dāng)前正在處理的連接全部處理完,最后再退出進(jìn)程。
與快速停止服務(wù)相似,可以直接發(fā)送QUIT信號(hào)給master進(jìn)程來停止服務(wù),其效果與執(zhí)行-s quit命令是一樣的。
例如:
kill -s SIGQUIT <nginx master pid> 復(fù)制代碼如果希望“優(yōu)雅”地停止某個(gè)worker進(jìn)程,那么可以通過向該進(jìn)程發(fā)送WINCH信號(hào)來停止服務(wù) 。
例如:
kill -s SIGWINCH <nginx worker pid> 復(fù)制代碼發(fā)送信號(hào)
./nginx -g TERM | INT | QUIT 復(fù)制代碼TERM 和 INT 信號(hào)用于快速停止,QUIT 信號(hào)用于平滑停止。
Nginx重新加載配置
使運(yùn)行中的Nginx重讀配置項(xiàng)并生效
使用-s reload參數(shù)可以使運(yùn)行中的Nginx服務(wù)重新加載nginx.conf文件。 例如:
usrlocal/nginx/sbin/nginx -s reload 復(fù)制代碼日志文件回滾
使用-s reopen參數(shù)可以重新打開日志文件,這樣可以先把當(dāng)前日志文件改名或轉(zhuǎn)移到其他目錄中進(jìn)行備份,再重新打開時(shí)就會(huì)生成新的日志文件。
這個(gè)功能使得日志文件不至于過大。 例如:
./nginx -s reopen 復(fù)制代碼這與使用kill命令發(fā)送USR1信號(hào)效果相同。
kill -s SIGUSR1 <nginx master pid> 復(fù)制代碼總結(jié)
本文簡單介紹了Nginx的作用和優(yōu)點(diǎn),然后演示了如何安裝Nginx,以及如何啟動(dòng)和關(guān)閉服務(wù)。
轉(zhuǎn)載于:https://juejin.im/post/5ce68c3c5188253386140fa9
總結(jié)
- 上一篇: 继承类,接口
- 下一篇: IBM AIX服务器分区之间迁移IO设备