一、Nginx简介
- Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
- 其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
- Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
二、Nginx优点
- 轻量化:安装简单,配置文件简洁(还能够支持perl语法)、Bug非常少
- 高并发:相较于Apache拥有更高的单台并发量
- 多平台:Nginx代码完全用C语言从头写成,已经移植到许多体系结构和操作系统,可以在大多数 Unix、Linux上编译运行,并有 Windows 移植版
- 函数库:Nginx有自己的函数库,并且除了zlib、PCRE和OpenSSL之外,标准模块只使用系统C库函数。而且,如果不需要或者考虑到潜在的授权冲突,可以不使用这些第三方库。
- 模块化结构:包括 gzipping, byte ranges, chunked responses,以及 SSI-filter 等 filter。如果由 FastCGI或其它代理服务器处理单页中存在的多个 SSI,则这项处理可以并行运行,而不需要相互等待。FastCGI,拥有简单的负载均衡和容错。
- 邮件代理服务器:Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last.fm 描述了成功并且美妙的使用经验
- 负载均衡服务器(Tomcat+Nginx):Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务,也可以支持作为 HTTP代理服务器对外进行服务。Nginx采用C进行编写,不论是系统资源开销还是CPU使用效率都比 Perlbal 要好很多
- 支持 SSL 和 TLSSNI
- Nginx支持的平台
FreeBSD 3— 10 / i386; FreeBSD 5— 10 / amd64;
Linux 2.2— 4 / i386; Linux 2.6— 4 / amd64; Linux 3— 4 / armv6l, armv7l, aarch64;
Solaris 9 / i386, sun4u; Solaris 10 / i386, amd64, sun4v;
AIX 7.1 / powerpc;
HP-UX 11.31 / ia64;
Mac OS X / ppc, i386;
Windows XP, Windows Server 2003.
参考百度百科:https://baike.baidu.com/item/nginx/3817705?fr=aladdin#1_2
三、Nginx部署
1.安装软件包
nginx:http://nginx.org/en/download.html
yum -y install pcre-devel //支持地址重写功能(防盗链)
useradd -M -s /sbin/nologin nginx //新建运行用户
tar -zxvf nginx-1.6.0.tar.gz -C /usr/src/ //解压nginx源码包
cd /usr/src/nginx-1.6.0/ //进入nginx源码解压目录
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
详解 --user:指定运行用户 --group:指定运行组 --with-http_stub_status_module:启用状态统计模块支持
make && make install
2.修改配置文件
vim /usr/local/nginx/conf/nginx.conf //编辑nginx主配置文件
user nobody nginx; //指定Nginx运行用户和组
worker_processes 1; //启动进程数(根据物理CPU个数设置)
error_log logs/error.log info; //定义错误日志,记录级别为info(信息)
pid logs/nginx.pid; //指定PID文件(存储程序进程号)位置
events {
use epoll; //使用epoll网络I/O模型,优化Nginx
worker_connections 1024; //每个工作进程允许最大的同时连接数
}
http {
include mime.types;
//额外加载该文件(mime.types内定义各文件类型映像,如image/png png;png格式文件为图片类型;主要用于识别文件类型,什么类型使浏览器用什么方式呈现)
default_type application/octet-stream; //默认响应为文件流
access_log logs/access.log main; //指定所有站点访问日志存放路径
sendfile on; //打开系统函数sendfile()提高性能
tcp_nopush on; //sendfile开启后才生效,调用tcp_cork方法
#keepalive_timeout 0;
keepalive_timeout 65; //会话保持时间,指定时间内客户端无访问请求,断开连接,需连接时重新请求
gzip on; //网页压缩
server {
listen 80; //定义服务器监听端口
server_name localhost; //定义服务器名及监听IP
charset utf-8; //网站的字符编码
access_log logs/host.access.log main; //指定当前站点访问日志存放路径
location / { ////匹配客户端所有请求,执行如下操作
root html; //网页存放目录
index index.html index.htm; //Nginx首页支持页面
}
error_page 500 502 503 504 /50x.html;
location = /50x.html { //错误页面
}
}
}
3.启动服务
ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ 或echo "PATH=$PATH:/usr/local/nginx/sbin/" >>/etc/profile && source /etc/profile
//将命令做软链接或加入到PATH环境变量,方便命令执行
vim /etc/init.d/nginx //编写服务启动脚本
#!/bin/bash
#chkconfig: - 99 20
#description: Nginx Server Control Script
NP="/usr/local/nginx/sbin/nginx"
NPF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$NP;
if [ $? -eq 0 ]
then
echo "nginx is starting!! "
fi
;;
stop)
kill -s QUIT $(cat $NPF)
if [ $? -eq 0 ]
then
echo "nginx is stopping!! "
fi
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $NPF)
if [ $? -eq 0 ]
then
echo "nginx config file is reload! "
fi
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
chmod +x /etc/init.d/nginx
/etc/init.d/nginx start && chkconfig --level 35 nginx on
nginx -t //检查配置文件是否有误
4.增加状态统计支持
vim /usr/local/nginx/conf/nginx.conf
location /status { //在server下添加如下行
stub_status on;
access_log off;
}
/etc/init.d/nginx restart
客户端访问:http://192.168.1.10/status
5.awstats状态统计页面
(1)awstats部署
tar -zxvf awstats-7.3.tar.gz //解压
mv awstats-7.3 /usr/local/awstats //移动并重命名为/usr/local/awstats目录
chown -R root:root /usr/local/awstats //设置目录所有者及所有组为root用户
chmod -R 755 /usr/local/awstats/ //给予所有者完整权限
chmod +x /usr/local/awstats/tools/*.pl //给予所有以.pl结尾的文件所有人拥有执行权限
chmod +x /usr/local/awstats/wwwroot/cgi-bin/*.pl //给予所有以.pl结尾的文件所有人拥有执行权限
(2)awstats配置
cd /usr/local/awstats/tools/
./awstats_configure.pl //生成配置文件及目录(y-->none-->y-->主机名-->回车-->回车)
vim /etc/awstats/awstats.www.hiahia.com.conf //编辑生成的配置文件
50 LogFile="/usr/local/nginx/logs/access.log" //修改Nginx访问日志路径
mkdir /var/lib/awstats //创建图表存放目录
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl --update --config=www.hiahia.com //根据日志生成图表
(3)生成html静态页面
mkdir /usr/local/nginx/html/awstats //创建静态页面存放目录
./awstats_buildstaticpages.pl --update --config=www.xueluo.org --lang=cn --dir=/usr/local/nginx/html/awstats/
//根据配置文件生成中文的html静态文件到/usr/local/nginx/html/awstats/
vim /usr/local/nginx/conf/nginx.conf
39 location ~ ^/awstats {
40 root /usr/local/nginx/html/awstats;
41 index index.html;
42 }
43
44 location ~ ^/icon|/css|/js|/classess {
45 root /usr/local/awstats/wwwroot/;
46 }
crontab -e //新建计划任务,每隔5分钟生成图表并转换为html文件
*/5 * * * * /usr/local/awstats/wwwroot/cgi-bin/awstats.pl --update --config=www.hiahia.com && /usr/local/awstats/tools/awstats_buildstaticpages.pl --update --config=www.hiahia.com --lang=cn --dir=/usr/local/nginx/html/awstats/
(4)访问 http://<IP>/awstats.www.hiahia.com.html