|
Nginx的介绍:
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler(俄文:Рамблер)使用。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,在中国使用nginx网站用户有:新浪、网易、腾讯等。
以上我们了解了nginx的由来,那么接下来我们研究研究nginx的基本架构、基本功能、扩展能能,nginx的特性以及模块的类型:
Nginx的基本架构:
一个master进程,生成一个或多个worker
事件驱动: epoll, kqueue, /dev/poll (event ports)
消息通知:select, poll, rt signals
支持sendfile, sendfile64
支持AIO
支持mmap
由此我们可以得出结论;nginx: 非阻塞、事件驱动、一个master生成一个或多个worker, 每个worker响应n个请求;
基本功能:
静态资源的web服务器,能缓存打开的文件 描述符
http, smtp, pop3协议的反向代理服务器,缓存、负载均衡;
支持FastCGI (fpm)
模块化,非DSO机制,过滤器zip,SSI及图像大小调整;
支持SSL
扩展功能:
基于名称和IP的虚拟主机;
支持keepalive
支持平滑升级
定制访问日志 ,支持使用日志缓冲区提高日志存储性能
支持url rewrite
支持路径别名
支持基于IP及用户的访问控制
支持速率限制,支持并发数限制
Nginx的特性:
模块化设计、较好扩展性
高可靠性; master-->worker
低内存消耗; 10000个keep-alive连接在Nginx仅消耗2.5MB
支持热部署;不停机而更新配置文件、更换日志文件、更新服务器程序版本
模块类型:
核心模块
Standard HTTP modules
Optional HTTP modules
Mail modules
3rd party modules
nginx的安装方式:
安装Nginx可以采用YUM方法,Nginx官方提供的源可以保证安装最新版本的稳定程序;但是在生产环境中,我们可以根据自己的需求,通过编译源码的方式来安装。
下面以编译安装nginx-1.6.2.tar.gz进行说明:
1、解决依赖关系:
# yum groupinstall "Development Tools" "Server Platform Deveopment" -y
# yum install openssl-devel pcre-devel -y
2、添加用户nginx,实现以之运行nginx服务进程:
# groupadd -r nginx
# useradd -r -g nginx nginx
3、创建编译安装存放的位置文件:
#mkdir -pv /var/tmp/nginx/{client,proxy,fastcgi}
4、编译、安装:
#./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --user=nginx --group=nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi
# make && make install
5、检查语法:#/usr/local/nginx/sbin/nginx -t
启动nginx服务:#/usr/local/nginx/sbin/nginx
查看80端口是否处于监听状态:#ss -tnl
打开浏览器输入当前主机的地址会出现nginx的默认页面
说明:
1、Nginx可以使用Tmalloc(快速、多线程的malloc库及优秀性能分析工具)来加速内存分配,使用此功能需要事先安装gperftools,而后在编译nginx添加--with-google_perftools_module选项即可。
2、如果想使用nginx的perl模块,可以通过为configure脚本添加--with-http_perl_module选项来实现,但目前此模块仍处于实验性使用阶段,可能会在运行中出现意外,因此,其实现方式这里不再介绍。如果想使用基于nginx的cgi功能,也可以基于FCGI来实现,具体实现方法请参照网上的文档。
3、为nginx提供SysV init脚本:
新建文件/etc/rc.d/init.d/nginx,内容如下:
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
附:./configure编译使用的选项说明:
1、--prefix=<path> - Nginx安装路径。如果没有指定,默认为 /usr/local/nginx;
2、--conf-path=<path> - 在没有给定-c选项下默认的nginx.conf的路径。如果没有指定,默认为<prefix>/conf/nginx.conf;
3、--user=<user> - 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的用户。如果没有指定,默认为 nobody;
4、--group=<group> - 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的组。如果没有指定,默认为 nobody;
5、--error-log-path=<path> - 在nginx.conf中没有指定error_log指令的情况下,默认的错误日志的路径。如果没有指定,默认为 <prefix>/logs/error.log;
6、--http-log-path=<path> - 在nginx.conf中没有指定access_log指令的情况下,默认的访问日志的路径。如果没有指定,默认为 <prefix>/logs/access.log;
7、--pid-path=<path> - 在nginx.conf中没有指定pid指令的情况下,默认的nginx.pid的路径。如果没有指定,默认为 <prefix>/logs/nginx.pid;
8、--lock-path=<path> - nginx.lock文件的路径;
9、--with-http_ssl_module -支持SSL
10、--with-http_stub_status_module - 启用 "server status" 页
11、--with-http_gzip_static_module -启动预压缩功能
12、--with-http_flv_module - 启用ngx_http_flv_module
13、--with-http_mp4_module -开启nginx对flv视频流的支持
14、--http-client-body-temp-path=/var/tmp/nginx/client-设置路径的HTTP客户端请求体的临时文件
15、--http-proxy-temp-path=/var/tmp/nginx/proxy-设置HTTP代理的临时文件的路径
16、--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi-设置临时文件的HTTP请求
以上只是个人对nginx的小总结,不足之处,望各位兄弟姐妹指出,感激不尽下篇将是对nginx的配置文件进行详细解析
|
|