Nginx (engine x) 是一款轻量级的 Web 服务器 、反向代理服务器及电子邮件(IMAP/POP3)代理服务器。它是来自俄罗斯的Igor Sysoev在为Rambler Media工做期间,使用C语言开发的。html
Igor Sysoev将Nginx的代码开源,而且赋予其最自由的2-clause BSD-like license许可证。因为Nginx使用基于事件驱动的架构可以并发处理百万级别的TCP链接,高度模块化的设计和自由的许可证使得扩展Nginx功能的第三方模块层出不穷,并且优秀的设计带来了极佳的稳定性,所以其做为Web服务器被普遍应用到大流量的网站上。java
所谓反向代理(Reverse Proxy)方式是指以代理服务器来接受 internet 上的链接请求,而后将请求转发给内部网络上的服务器,并将从服务器上获得的结果返回给 internet 上请求链接的客户端,此时代理服务器对外就表现为一个反向代理服务器。nginx
既然有反向代理,那么也就有正向代理。正向代理是一个位于客户端和原始服务器之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标,而后代理向原始服务器转交请求并将得到的内容返回给客户端。c++
能够说正向代理代理的是客户端,反向代理代理的是服务器。web
使用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服务器最基本功能所必需的库。bash
GCC(GNU Compiler Collection)可用来编译C语言程序。服务器
Nginx一般不会直接提供二进制可执行程序,所以咱们须要编译其源码。网络
并且咱们可能会使用C++来编写Nginx HTTP模块,这时就须要用到G++编译器了。
用yum安装G++编译器:
yum install -y gcc-c++
复制代码
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库用于对HTTP包的内容作gzip格式的压缩,若是咱们在nginx.conf里配置了gzip on, 并指定对于某些类型(content-type)的HTTP响应使用gzip来进行压缩以减小网络传输量,那么,在编译时就必须把zlib编译进Nginx。
其yum安装方式以下
yum install -y zlib zlib-devel
复制代码
同理,zlib是直接使用的库,zlib-devel是二次开发所须要的库。
若是咱们的服务器不仅是要支持HTTP,还须要在更安全的SSL协议上传输HTTP,那么就须要拥有OpenSSL了。
另外,若是咱们想使用MD五、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/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"
复制代码
安装成功之后,能够经过-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 -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 信号用于平滑中止。
使用-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,以及如何启动和关闭服务。