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